Skip to content

Commit

Permalink
A small diagnostic improvement for dropping_copy_types
Browse files Browse the repository at this point in the history
fixes #125189
  • Loading branch information
surechen committed May 23, 2024
1 parent 9f432d7 commit 38c8bc3
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 11 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_lint/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ lint_drop_trait_constraints =
lint_dropping_copy_types = calls to `std::mem::drop` with a value that implements `Copy` does nothing
.label = argument has type `{$arg_ty}`
.note = use `let _ = ...` to ignore the expression or result
.note = use `{$replace}` to ignore the value instead of dropping directly
lint_dropping_references = calls to `std::mem::drop` with a reference instead of an owned value does nothing
.label = argument has type `{$arg_ty}`
Expand Down
19 changes: 17 additions & 2 deletions compiler/rustc_lint/src/drop_forget_useless.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use rustc_hir::{Arm, Expr, ExprKind, Node};
use rustc_hir::{Arm, Expr, ExprKind, Node, StmtKind};
use rustc_middle::ty;
use rustc_span::sym;

Expand Down Expand Up @@ -163,10 +163,25 @@ impl<'tcx> LateLintPass<'tcx> for DropForgetUseless {
);
}
sym::mem_drop if is_copy && !drop_is_single_call_in_arm => {
let replace = format!(
"let _ = {}",
if let Some((_, node)) = cx.tcx.hir().parent_iter(expr.hir_id).nth(0)
&& let Node::Stmt(stmt) = node
&& let StmtKind::Semi(e) = stmt.kind
&& e.hir_id == expr.hir_id
{
cx.sess()
.source_map()
.span_to_snippet(arg.span)
.unwrap_or("...".to_string())
} else {
"...".to_string()
}
);
cx.emit_span_lint(
DROPPING_COPY_TYPES,
expr.span,
DropCopyDiag { arg_ty, label: arg.span },
DropCopyDiag { arg_ty, label: arg.span, replace },
);
}
sym::mem_forget if is_copy => {
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_lint/src/lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,7 @@ pub struct DropCopyDiag<'a> {
pub arg_ty: Ty<'a>,
#[label]
pub label: Span,
pub replace: String,
}

#[derive(LintDiagnostic)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ LL | drop(origin);
| |
| argument has type `<T as UncheckedCopy>::Output`
|
= note: use `let _ = ...` to ignore the expression or result
= note: use `let _ = origin` to ignore the value instead of dropping directly
= note: `#[warn(dropping_copy_types)]` on by default

warning: 1 warning emitted
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ LL | drop(origin);
| |
| argument has type `<T as UncheckedCopy>::Output`
|
= note: use `let _ = ...` to ignore the expression or result
= note: use `let _ = origin` to ignore the value instead of dropping directly
= note: `#[warn(dropping_copy_types)]` on by default

warning: 1 warning emitted
Expand Down
17 changes: 17 additions & 0 deletions tests/ui/lint/dropping_copy_types-issue-125189.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//@ check-fail

#![deny(dropping_copy_types)]

fn main() {
let y = 1;
let z = 2;
match y {
1 => drop(3), //~ ERROR calls to `std::mem::drop`
2 => drop(y), //~ ERROR calls to `std::mem::drop`
3 => if drop(z) == () {}, //~ ERROR calls to `std::mem::drop`
_ => (),
}

drop(3.2); //~ ERROR calls to `std::mem::drop`
drop(y); //~ ERROR calls to `std::mem::drop`
}
57 changes: 57 additions & 0 deletions tests/ui/lint/dropping_copy_types-issue-125189.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
error: calls to `std::mem::drop` with a value that implements `Copy` does nothing
--> $DIR/dropping_copy_types-issue-125189.rs:9:14
|
LL | 1 => drop(3),
| ^^^^^-^
| |
| argument has type `i32`
|
= note: use `let _ = ...` to ignore the value instead of dropping directly
note: the lint level is defined here
--> $DIR/dropping_copy_types-issue-125189.rs:3:9
|
LL | #![deny(dropping_copy_types)]
| ^^^^^^^^^^^^^^^^^^^

error: calls to `std::mem::drop` with a value that implements `Copy` does nothing
--> $DIR/dropping_copy_types-issue-125189.rs:10:14
|
LL | 2 => drop(y),
| ^^^^^-^
| |
| argument has type `i32`
|
= note: use `let _ = ...` to ignore the value instead of dropping directly

error: calls to `std::mem::drop` with a value that implements `Copy` does nothing
--> $DIR/dropping_copy_types-issue-125189.rs:11:17
|
LL | 3 => if drop(z) == () {},
| ^^^^^-^
| |
| argument has type `i32`
|
= note: use `let _ = ...` to ignore the value instead of dropping directly

error: calls to `std::mem::drop` with a value that implements `Copy` does nothing
--> $DIR/dropping_copy_types-issue-125189.rs:15:5
|
LL | drop(3.2);
| ^^^^^---^
| |
| argument has type `f64`
|
= note: use `let _ = 3.2` to ignore the value instead of dropping directly

error: calls to `std::mem::drop` with a value that implements `Copy` does nothing
--> $DIR/dropping_copy_types-issue-125189.rs:16:5
|
LL | drop(y);
| ^^^^^-^
| |
| argument has type `i32`
|
= note: use `let _ = y` to ignore the value instead of dropping directly

error: aborting due to 5 previous errors

12 changes: 6 additions & 6 deletions tests/ui/lint/dropping_copy_types.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ LL | drop(s1);
| |
| argument has type `SomeStruct`
|
= note: use `let _ = ...` to ignore the expression or result
= note: use `let _ = s1` to ignore the value instead of dropping directly
note: the lint level is defined here
--> $DIR/dropping_copy_types.rs:3:9
|
Expand All @@ -21,7 +21,7 @@ LL | drop(s2);
| |
| argument has type `SomeStruct`
|
= note: use `let _ = ...` to ignore the expression or result
= note: use `let _ = s2` to ignore the value instead of dropping directly

warning: calls to `std::mem::drop` with a reference instead of an owned value does nothing
--> $DIR/dropping_copy_types.rs:36:5
Expand All @@ -42,7 +42,7 @@ LL | drop(s4);
| |
| argument has type `SomeStruct`
|
= note: use `let _ = ...` to ignore the expression or result
= note: use `let _ = s4` to ignore the value instead of dropping directly

warning: calls to `std::mem::drop` with a reference instead of an owned value does nothing
--> $DIR/dropping_copy_types.rs:38:5
Expand Down Expand Up @@ -82,7 +82,7 @@ LL | drop(println_and(13));
| |
| argument has type `i32`
|
= note: use `let _ = ...` to ignore the expression or result
= note: use `let _ = println_and(13)` to ignore the value instead of dropping directly

warning: calls to `std::mem::drop` with a value that implements `Copy` does nothing
--> $DIR/dropping_copy_types.rs:74:14
Expand All @@ -92,7 +92,7 @@ LL | 3 if drop(println_and(14)) == () => (),
| |
| argument has type `i32`
|
= note: use `let _ = ...` to ignore the expression or result
= note: use `let _ = ...` to ignore the value instead of dropping directly

warning: calls to `std::mem::drop` with a value that implements `Copy` does nothing
--> $DIR/dropping_copy_types.rs:76:14
Expand All @@ -102,7 +102,7 @@ LL | 4 => drop(2),
| |
| argument has type `i32`
|
= note: use `let _ = ...` to ignore the expression or result
= note: use `let _ = ...` to ignore the value instead of dropping directly

warning: 10 warnings emitted

0 comments on commit 38c8bc3

Please sign in to comment.