Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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