Skip to content

Commit

Permalink
Rollup merge of rust-lang#75056 - Veykril:path_statements_lint, r=oli…
Browse files Browse the repository at this point in the history
…-obk

Lint path statements to suggest using drop when the type needs drop

Fixes rust-lang#48852. With this change the current lint description doesn't really fit entirely anymore I think.
  • Loading branch information
Manishearth authored Aug 3, 2020
2 parents f7b7fa5 + 35d6a2e commit 65b0d13
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
20 changes: 18 additions & 2 deletions src/librustc_lint/unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,10 +275,26 @@ declare_lint_pass!(PathStatements => [PATH_STATEMENTS]);

impl<'tcx> LateLintPass<'tcx> for PathStatements {
fn check_stmt(&mut self, cx: &LateContext<'_>, s: &hir::Stmt<'_>) {
if let hir::StmtKind::Semi(ref expr) = s.kind {
if let hir::StmtKind::Semi(expr) = s.kind {
if let hir::ExprKind::Path(_) = expr.kind {
cx.struct_span_lint(PATH_STATEMENTS, s.span, |lint| {
lint.build("path statement with no effect").emit()
let ty = cx.typeck_results().expr_ty(expr);
if ty.needs_drop(cx.tcx, cx.param_env) {
let mut lint = lint.build("path statement drops value");
if let Ok(snippet) = cx.sess().source_map().span_to_snippet(expr.span) {
lint.span_suggestion(
s.span,
"use `drop` to clarify the intent",
format!("drop({});", snippet),
Applicability::MachineApplicable,
);
} else {
lint.span_help(s.span, "use `drop` to clarify the intent");
}
lint.emit()
} else {
lint.build("path statement with no effect").emit()
}
});
}
}
Expand Down
13 changes: 12 additions & 1 deletion src/test/ui/warn-path-statement.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
// compile-flags: -D path-statements
fn main() {
struct Droppy;

impl Drop for Droppy {
fn drop(&mut self) {}
}

fn main() {
let x = 10;
x; //~ ERROR path statement with no effect

let y = Droppy;
y; //~ ERROR path statement drops value

let z = (Droppy,);
z; //~ ERROR path statement drops value
}
16 changes: 14 additions & 2 deletions src/test/ui/warn-path-statement.stderr
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
error: path statement with no effect
--> $DIR/warn-path-statement.rs:5:5
--> $DIR/warn-path-statement.rs:10:5
|
LL | x;
| ^^
|
= note: requested on the command line with `-D path-statements`

error: aborting due to previous error
error: path statement drops value
--> $DIR/warn-path-statement.rs:13:5
|
LL | y;
| ^^ help: use `drop` to clarify the intent: `drop(y);`

error: path statement drops value
--> $DIR/warn-path-statement.rs:16:5
|
LL | z;
| ^^ help: use `drop` to clarify the intent: `drop(z);`

error: aborting due to 3 previous errors

0 comments on commit 65b0d13

Please sign in to comment.