Skip to content

Commit 9672368

Browse files
committed
A small diagnostic improvement for dropping_copy_types
fixes #125189
1 parent 9f432d7 commit 9672368

8 files changed

+124
-2
lines changed

compiler/rustc_lint/messages.ftl

+1
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ lint_drop_trait_constraints =
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}`
199199
.note = use `let _ = ...` to ignore the expression or result
200+
.suggestion = ignore the value
200201
201202
lint_dropping_references = calls to `std::mem::drop` with a reference instead of an owned value does nothing
202203
.label = argument has type `{$arg_ty}`

compiler/rustc_lint/src/drop_forget_useless.rs

+14-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,22 @@ impl<'tcx> LateLintPass<'tcx> for DropForgetUseless {
163163
);
164164
}
165165
sym::mem_drop if is_copy && !drop_is_single_call_in_arm => {
166+
let (sugg, replace) = if let Some((_, node)) =
167+
cx.tcx.hir().parent_iter(expr.hir_id).nth(0)
168+
&& let Node::Stmt(stmt) = node
169+
&& let StmtKind::Semi(e) = stmt.kind
170+
&& e.hir_id == expr.hir_id
171+
&& let Ok(value) = cx.sess().source_map().span_to_snippet(arg.span)
172+
{
173+
(Some(expr.span), format!("let _ = {}", value))
174+
} else {
175+
(None, "".to_string())
176+
};
177+
166178
cx.emit_span_lint(
167179
DROPPING_COPY_TYPES,
168180
expr.span,
169-
DropCopyDiag { arg_ty, label: arg.span },
181+
DropCopyDiag { arg_ty, label: arg.span, sugg, replace },
170182
);
171183
}
172184
sym::mem_forget if is_copy => {

compiler/rustc_lint/src/lints.rs

+3
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,9 @@ pub struct DropCopyDiag<'a> {
674674
pub arg_ty: Ty<'a>,
675675
#[label]
676676
pub label: Span,
677+
#[suggestion(style = "verbose", code = "{replace}", applicability = "maybe-incorrect")]
678+
pub sugg: Option<Span>,
679+
pub replace: String,
677680
}
678681

679682
#[derive(LintDiagnostic)]

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

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ LL | drop(origin);
88
|
99
= note: use `let _ = ...` to ignore the expression or result
1010
= note: `#[warn(dropping_copy_types)]` on by default
11+
help: ignore the value
12+
|
13+
LL | let _ = origin;
14+
| ~~~~~~~~~~~~~~
1115

1216
warning: 1 warning emitted
1317

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

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ LL | drop(origin);
88
|
99
= note: use `let _ = ...` to ignore the expression or result
1010
= note: `#[warn(dropping_copy_types)]` on by default
11+
help: ignore the value
12+
|
13+
LL | let _ = origin;
14+
| ~~~~~~~~~~~~~~
1115

1216
warning: 1 warning emitted
1317

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,65 @@
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 expression or result
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 expression or result
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 expression or result
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 _ = ...` to ignore the expression or result
45+
help: ignore the value
46+
|
47+
LL | let _ = 3.2;
48+
| ~~~~~~~~~~~
49+
50+
error: calls to `std::mem::drop` with a value that implements `Copy` does nothing
51+
--> $DIR/dropping_copy_types-issue-125189.rs:16:5
52+
|
53+
LL | drop(y);
54+
| ^^^^^-^
55+
| |
56+
| argument has type `i32`
57+
|
58+
= note: use `let _ = ...` to ignore the expression or result
59+
help: ignore the value
60+
|
61+
LL | let _ = y;
62+
| ~~~~~~~~~
63+
64+
error: aborting due to 5 previous errors
65+

tests/ui/lint/dropping_copy_types.stderr

+16
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ note: the lint level is defined here
1212
|
1313
LL | #![warn(dropping_copy_types)]
1414
| ^^^^^^^^^^^^^^^^^^^
15+
help: ignore the value
16+
|
17+
LL | let _ = s1;
18+
| ~~~~~~~~~~
1519

1620
warning: calls to `std::mem::drop` with a value that implements `Copy` does nothing
1721
--> $DIR/dropping_copy_types.rs:35:5
@@ -22,6 +26,10 @@ LL | drop(s2);
2226
| argument has type `SomeStruct`
2327
|
2428
= note: use `let _ = ...` to ignore the expression or result
29+
help: ignore the value
30+
|
31+
LL | let _ = s2;
32+
| ~~~~~~~~~~
2533

2634
warning: calls to `std::mem::drop` with a reference instead of an owned value does nothing
2735
--> $DIR/dropping_copy_types.rs:36:5
@@ -43,6 +51,10 @@ LL | drop(s4);
4351
| argument has type `SomeStruct`
4452
|
4553
= note: use `let _ = ...` to ignore the expression or result
54+
help: ignore the value
55+
|
56+
LL | let _ = s4;
57+
| ~~~~~~~~~~
4658

4759
warning: calls to `std::mem::drop` with a reference instead of an owned value does nothing
4860
--> $DIR/dropping_copy_types.rs:38:5
@@ -83,6 +95,10 @@ LL | drop(println_and(13));
8395
| argument has type `i32`
8496
|
8597
= note: use `let _ = ...` to ignore the expression or result
98+
help: ignore the value
99+
|
100+
LL | let _ = println_and(13);
101+
| ~~~~~~~~~~~~~~~~~~~~~~~
86102

87103
warning: calls to `std::mem::drop` with a value that implements `Copy` does nothing
88104
--> $DIR/dropping_copy_types.rs:74:14

0 commit comments

Comments
 (0)