Skip to content

Commit f7bb714

Browse files
committed
Auto merge of #125433 - surechen:fix_125189, r=Urgau
A small diagnostic improvement for dropping_copy_types For a value `m` which implements `Copy` trait, `drop(m);` does nothing. We now suggest user to ignore it by a abstract and general note: `let _ = ...`. I think we can give a clearer note here: `let _ = m;` fixes #125189 <!-- If this PR is related to an unstable feature or an otherwise tracked effort, please link to the relevant tracking issue here. If you don't know of a related tracking issue or there are none, feel free to ignore this. This PR will get automatically assigned to a reviewer. In case you would like a specific user to review your work, you can assign it to them by using r​? <reviewer name> -->
2 parents cdc509f + 09c8e39 commit f7bb714

11 files changed

+169
-11
lines changed

compiler/rustc_lint/messages.ftl

+1
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ lint_drop_trait_constraints =
233233
lint_dropping_copy_types = calls to `std::mem::drop` with a value that implements `Copy` does nothing
234234
.label = argument has type `{$arg_ty}`
235235
.note = use `let _ = ...` to ignore the expression or result
236+
.suggestion = use `let _ = ...` to ignore the expression or result
236237
237238
lint_dropping_references = calls to `std::mem::drop` with a reference instead of an owned value does nothing
238239
.label = argument has type `{$arg_ty}`

compiler/rustc_lint/src/drop_forget_useless.rs

+17-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
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_session::{declare_lint, declare_lint_pass};
44
use rustc_span::sym;
55

66
use crate::{
77
lints::{
8-
DropCopyDiag, DropRefDiag, ForgetCopyDiag, ForgetRefDiag, UndroppedManuallyDropsDiag,
9-
UndroppedManuallyDropsSuggestion,
8+
DropCopyDiag, DropCopySuggestion, DropRefDiag, ForgetCopyDiag, ForgetRefDiag,
9+
UndroppedManuallyDropsDiag, UndroppedManuallyDropsSuggestion,
1010
},
1111
LateContext, LateLintPass, LintContext,
1212
};
@@ -164,10 +164,23 @@ impl<'tcx> LateLintPass<'tcx> for DropForgetUseless {
164164
);
165165
}
166166
sym::mem_drop if is_copy && !drop_is_single_call_in_arm => {
167+
let sugg = if let Some((_, node)) = 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+
{
172+
DropCopySuggestion::Suggestion {
173+
start_span: expr.span.shrink_to_lo().until(arg.span),
174+
end_span: arg.span.shrink_to_hi().until(expr.span.shrink_to_hi()),
175+
}
176+
} else {
177+
DropCopySuggestion::Note
178+
};
179+
167180
cx.emit_span_lint(
168181
DROPPING_COPY_TYPES,
169182
expr.span,
170-
DropCopyDiag { arg_ty, label: arg.span },
183+
DropCopyDiag { arg_ty, label: arg.span, sugg },
171184
);
172185
}
173186
sym::mem_forget if is_copy => {

compiler/rustc_lint/src/lints.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -677,11 +677,25 @@ pub struct DropRefDiag<'a> {
677677

678678
#[derive(LintDiagnostic)]
679679
#[diag(lint_dropping_copy_types)]
680-
#[note]
681680
pub struct DropCopyDiag<'a> {
682681
pub arg_ty: Ty<'a>,
683682
#[label]
684683
pub label: Span,
684+
#[subdiagnostic]
685+
pub sugg: DropCopySuggestion,
686+
}
687+
688+
#[derive(Subdiagnostic)]
689+
pub enum DropCopySuggestion {
690+
#[note(lint_note)]
691+
Note,
692+
#[multipart_suggestion(lint_suggestion, style = "verbose", applicability = "maybe-incorrect")]
693+
Suggestion {
694+
#[suggestion_part(code = "let _ = ")]
695+
start_span: Span,
696+
#[suggestion_part(code = "")]
697+
end_span: Span,
698+
},
685699
}
686700

687701
#[derive(LintDiagnostic)]

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@ LL | drop(origin);
66
| |
77
| argument has type `<T as UncheckedCopy>::Output`
88
|
9-
= note: use `let _ = ...` to ignore the expression or result
109
= note: `#[warn(dropping_copy_types)]` on by default
10+
help: use `let _ = ...` to ignore the expression or result
11+
|
12+
LL - drop(origin);
13+
LL + let _ = origin;
14+
|
1115

1216
warning: 1 warning emitted
1317

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@ LL | drop(origin);
66
| |
77
| argument has type `<T as UncheckedCopy>::Output`
88
|
9-
= note: use `let _ = ...` to ignore the expression or result
109
= note: `#[warn(dropping_copy_types)]` on by default
10+
help: use `let _ = ...` to ignore the expression or result
11+
|
12+
LL - drop(origin);
13+
LL + let _ = origin;
14+
|
1115

1216
warning: 1 warning emitted
1317

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
0 => drop(y), //~ ERROR calls to `std::mem::drop`
10+
1 => drop(z), //~ ERROR calls to `std::mem::drop`
11+
2 => drop(3), //~ ERROR calls to `std::mem::drop`
12+
_ => {},
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
error: calls to `std::mem::drop` with a value that implements `Copy` does nothing
2+
--> $DIR/dropping_copy_types-issue-125189-can-not-fixed.rs:9:14
3+
|
4+
LL | 0 => drop(y),
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-can-not-fixed.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-can-not-fixed.rs:10:14
18+
|
19+
LL | 1 => drop(z),
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-can-not-fixed.rs:11:14
28+
|
29+
LL | 2 => drop(3),
30+
| ^^^^^-^
31+
| |
32+
| argument has type `i32`
33+
|
34+
= note: use `let _ = ...` to ignore the expression or result
35+
36+
error: aborting due to 3 previous errors
37+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//@ check-fail
2+
//@ run-rustfix
3+
4+
#![deny(dropping_copy_types)]
5+
6+
fn main() {
7+
let y = 1;
8+
let _ = 3.2; //~ ERROR calls to `std::mem::drop`
9+
let _ = y; //~ ERROR calls to `std::mem::drop`
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//@ check-fail
2+
//@ run-rustfix
3+
4+
#![deny(dropping_copy_types)]
5+
6+
fn main() {
7+
let y = 1;
8+
drop(3.2); //~ ERROR calls to `std::mem::drop`
9+
drop(y); //~ ERROR calls to `std::mem::drop`
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
error: calls to `std::mem::drop` with a value that implements `Copy` does nothing
2+
--> $DIR/dropping_copy_types-issue-125189.rs:8:5
3+
|
4+
LL | drop(3.2);
5+
| ^^^^^---^
6+
| |
7+
| argument has type `f64`
8+
|
9+
note: the lint level is defined here
10+
--> $DIR/dropping_copy_types-issue-125189.rs:4:9
11+
|
12+
LL | #![deny(dropping_copy_types)]
13+
| ^^^^^^^^^^^^^^^^^^^
14+
help: use `let _ = ...` to ignore the expression or result
15+
|
16+
LL - drop(3.2);
17+
LL + let _ = 3.2;
18+
|
19+
20+
error: calls to `std::mem::drop` with a value that implements `Copy` does nothing
21+
--> $DIR/dropping_copy_types-issue-125189.rs:9:5
22+
|
23+
LL | drop(y);
24+
| ^^^^^-^
25+
| |
26+
| argument has type `i32`
27+
|
28+
help: use `let _ = ...` to ignore the expression or result
29+
|
30+
LL - drop(y);
31+
LL + let _ = y;
32+
|
33+
34+
error: aborting due to 2 previous errors
35+

tests/ui/lint/dropping_copy_types.stderr

+20-4
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,16 @@ LL | drop(s1);
66
| |
77
| argument has type `SomeStruct`
88
|
9-
= note: use `let _ = ...` to ignore the expression or result
109
note: the lint level is defined here
1110
--> $DIR/dropping_copy_types.rs:3:9
1211
|
1312
LL | #![warn(dropping_copy_types)]
1413
| ^^^^^^^^^^^^^^^^^^^
14+
help: use `let _ = ...` to ignore the expression or result
15+
|
16+
LL - drop(s1);
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
@@ -21,7 +25,11 @@ LL | drop(s2);
2125
| |
2226
| argument has type `SomeStruct`
2327
|
24-
= note: use `let _ = ...` to ignore the expression or result
28+
help: use `let _ = ...` to ignore the expression or result
29+
|
30+
LL - drop(s2);
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
@@ -42,7 +50,11 @@ LL | drop(s4);
4250
| |
4351
| argument has type `SomeStruct`
4452
|
45-
= note: use `let _ = ...` to ignore the expression or result
53+
help: use `let _ = ...` to ignore the expression or result
54+
|
55+
LL - drop(s4);
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
@@ -82,7 +94,11 @@ LL | drop(println_and(13));
8294
| |
8395
| argument has type `i32`
8496
|
85-
= note: use `let _ = ...` to ignore the expression or result
97+
help: use `let _ = ...` to ignore the expression or result
98+
|
99+
LL - drop(println_and(13));
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)