Skip to content

Commit 38c8bc3

Browse files
committed
A small diagnostic improvement for dropping_copy_types
fixes rust-lang#125189
1 parent 9f432d7 commit 38c8bc3

8 files changed

+101
-11
lines changed

compiler/rustc_lint/messages.ftl

+1-1
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ lint_drop_trait_constraints =
196196
197197
lint_dropping_copy_types = calls to `std::mem::drop` with a value that implements `Copy` does nothing
198198
.label = argument has type `{$arg_ty}`
199-
.note = use `let _ = ...` to ignore the expression or result
199+
.note = use `{$replace}` to ignore the value instead of dropping directly
200200
201201
lint_dropping_references = calls to `std::mem::drop` with a reference instead of an owned value does nothing
202202
.label = argument has type `{$arg_ty}`

compiler/rustc_lint/src/drop_forget_useless.rs

+17-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rustc_hir::{Arm, Expr, ExprKind, Node};
1+
use rustc_hir::{Arm, Expr, ExprKind, Node, StmtKind};
22
use rustc_middle::ty;
33
use rustc_span::sym;
44

@@ -163,10 +163,25 @@ impl<'tcx> LateLintPass<'tcx> for DropForgetUseless {
163163
);
164164
}
165165
sym::mem_drop if is_copy && !drop_is_single_call_in_arm => {
166+
let replace = format!(
167+
"let _ = {}",
168+
if let Some((_, node)) = cx.tcx.hir().parent_iter(expr.hir_id).nth(0)
169+
&& let Node::Stmt(stmt) = node
170+
&& let StmtKind::Semi(e) = stmt.kind
171+
&& e.hir_id == expr.hir_id
172+
{
173+
cx.sess()
174+
.source_map()
175+
.span_to_snippet(arg.span)
176+
.unwrap_or("...".to_string())
177+
} else {
178+
"...".to_string()
179+
}
180+
);
166181
cx.emit_span_lint(
167182
DROPPING_COPY_TYPES,
168183
expr.span,
169-
DropCopyDiag { arg_ty, label: arg.span },
184+
DropCopyDiag { arg_ty, label: arg.span, replace },
170185
);
171186
}
172187
sym::mem_forget if is_copy => {

compiler/rustc_lint/src/lints.rs

+1
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,7 @@ pub struct DropCopyDiag<'a> {
674674
pub arg_ty: Ty<'a>,
675675
#[label]
676676
pub label: Span,
677+
pub replace: String,
677678
}
678679

679680
#[derive(LintDiagnostic)]

tests/ui/associated-types/defaults-unsound-62211-1.next.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ LL | drop(origin);
66
| |
77
| argument has type `<T as UncheckedCopy>::Output`
88
|
9-
= note: use `let _ = ...` to ignore the expression or result
9+
= note: use `let _ = origin` to ignore the value instead of dropping directly
1010
= note: `#[warn(dropping_copy_types)]` on by default
1111

1212
warning: 1 warning emitted

tests/ui/associated-types/defaults-unsound-62211-2.next.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ LL | drop(origin);
66
| |
77
| argument has type `<T as UncheckedCopy>::Output`
88
|
9-
= note: use `let _ = ...` to ignore the expression or result
9+
= note: use `let _ = origin` to ignore the value instead of dropping directly
1010
= note: `#[warn(dropping_copy_types)]` on by default
1111

1212
warning: 1 warning emitted
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//@ check-fail
2+
3+
#![deny(dropping_copy_types)]
4+
5+
fn main() {
6+
let y = 1;
7+
let z = 2;
8+
match y {
9+
1 => drop(3), //~ ERROR calls to `std::mem::drop`
10+
2 => drop(y), //~ ERROR calls to `std::mem::drop`
11+
3 => if drop(z) == () {}, //~ ERROR calls to `std::mem::drop`
12+
_ => (),
13+
}
14+
15+
drop(3.2); //~ ERROR calls to `std::mem::drop`
16+
drop(y); //~ ERROR calls to `std::mem::drop`
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
error: calls to `std::mem::drop` with a value that implements `Copy` does nothing
2+
--> $DIR/dropping_copy_types-issue-125189.rs:9:14
3+
|
4+
LL | 1 => drop(3),
5+
| ^^^^^-^
6+
| |
7+
| argument has type `i32`
8+
|
9+
= note: use `let _ = ...` to ignore the value instead of dropping directly
10+
note: the lint level is defined here
11+
--> $DIR/dropping_copy_types-issue-125189.rs:3:9
12+
|
13+
LL | #![deny(dropping_copy_types)]
14+
| ^^^^^^^^^^^^^^^^^^^
15+
16+
error: calls to `std::mem::drop` with a value that implements `Copy` does nothing
17+
--> $DIR/dropping_copy_types-issue-125189.rs:10:14
18+
|
19+
LL | 2 => drop(y),
20+
| ^^^^^-^
21+
| |
22+
| argument has type `i32`
23+
|
24+
= note: use `let _ = ...` to ignore the value instead of dropping directly
25+
26+
error: calls to `std::mem::drop` with a value that implements `Copy` does nothing
27+
--> $DIR/dropping_copy_types-issue-125189.rs:11:17
28+
|
29+
LL | 3 => if drop(z) == () {},
30+
| ^^^^^-^
31+
| |
32+
| argument has type `i32`
33+
|
34+
= note: use `let _ = ...` to ignore the value instead of dropping directly
35+
36+
error: calls to `std::mem::drop` with a value that implements `Copy` does nothing
37+
--> $DIR/dropping_copy_types-issue-125189.rs:15:5
38+
|
39+
LL | drop(3.2);
40+
| ^^^^^---^
41+
| |
42+
| argument has type `f64`
43+
|
44+
= note: use `let _ = 3.2` to ignore the value instead of dropping directly
45+
46+
error: calls to `std::mem::drop` with a value that implements `Copy` does nothing
47+
--> $DIR/dropping_copy_types-issue-125189.rs:16:5
48+
|
49+
LL | drop(y);
50+
| ^^^^^-^
51+
| |
52+
| argument has type `i32`
53+
|
54+
= note: use `let _ = y` to ignore the value instead of dropping directly
55+
56+
error: aborting due to 5 previous errors
57+

tests/ui/lint/dropping_copy_types.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ LL | drop(s1);
66
| |
77
| argument has type `SomeStruct`
88
|
9-
= note: use `let _ = ...` to ignore the expression or result
9+
= note: use `let _ = s1` to ignore the value instead of dropping directly
1010
note: the lint level is defined here
1111
--> $DIR/dropping_copy_types.rs:3:9
1212
|
@@ -21,7 +21,7 @@ LL | drop(s2);
2121
| |
2222
| argument has type `SomeStruct`
2323
|
24-
= note: use `let _ = ...` to ignore the expression or result
24+
= note: use `let _ = s2` to ignore the value instead of dropping directly
2525

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

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

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

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

107107
warning: 10 warnings emitted
108108

0 commit comments

Comments
 (0)