Skip to content

Commit a84f56a

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

9 files changed

+113
-11
lines changed

Diff for: 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 = use `let _ = ...` to ignore the expression or result
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}`

Diff for: compiler/rustc_lint/src/drop_forget_useless.rs

+18-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
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

55
use crate::{
66
lints::{
7-
DropCopyDiag, DropRefDiag, ForgetCopyDiag, ForgetRefDiag, UndroppedManuallyDropsDiag,
8-
UndroppedManuallyDropsSuggestion,
7+
DropCopyDiag, DropCopySuggestion, DropRefDiag, ForgetCopyDiag, ForgetRefDiag,
8+
UndroppedManuallyDropsDiag, UndroppedManuallyDropsSuggestion,
99
},
1010
LateContext, LateLintPass, LintContext,
1111
};
@@ -163,10 +163,24 @@ impl<'tcx> LateLintPass<'tcx> for DropForgetUseless {
163163
);
164164
}
165165
sym::mem_drop if is_copy && !drop_is_single_call_in_arm => {
166+
let sugg = if let Some((_, node)) = cx.tcx.hir().parent_iter(expr.hir_id).nth(0)
167+
&& let Node::Stmt(stmt) = node
168+
&& let StmtKind::Semi(e) = stmt.kind
169+
&& e.hir_id == expr.hir_id
170+
&& let Ok(value) = cx.sess().source_map().span_to_snippet(arg.span)
171+
{
172+
DropCopySuggestion::Suggestion {
173+
sugg: expr.span,
174+
replace: format!("let _ = {}", value),
175+
}
176+
} else {
177+
DropCopySuggestion::Note
178+
};
179+
166180
cx.emit_span_lint(
167181
DROPPING_COPY_TYPES,
168182
expr.span,
169-
DropCopyDiag { arg_ty, label: arg.span },
183+
DropCopyDiag { arg_ty, label: arg.span, sugg },
170184
);
171185
}
172186
sym::mem_forget if is_copy => {

Diff for: compiler/rustc_lint/src/lints.rs

+19-1
Original file line numberDiff line numberDiff line change
@@ -669,11 +669,29 @@ pub struct DropRefDiag<'a> {
669669

670670
#[derive(LintDiagnostic)]
671671
#[diag(lint_dropping_copy_types)]
672-
#[note]
673672
pub struct DropCopyDiag<'a> {
674673
pub arg_ty: Ty<'a>,
675674
#[label]
676675
pub label: Span,
676+
#[subdiagnostic]
677+
pub sugg: DropCopySuggestion,
678+
}
679+
680+
#[derive(Subdiagnostic)]
681+
pub enum DropCopySuggestion {
682+
#[note(lint_note)]
683+
Note,
684+
#[suggestion(
685+
lint_suggestion,
686+
style = "verbose",
687+
code = "{replace}",
688+
applicability = "maybe-incorrect"
689+
)]
690+
Suggestion {
691+
#[primary_span]
692+
sugg: Span,
693+
replace: String,
694+
},
677695
}
678696

679697
#[derive(LintDiagnostic)]

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@ 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 | let _ = origin;
13+
| ~~~~~~~~~~~~~~
1114

1215
warning: 1 warning emitted
1316

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@ 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 | let _ = origin;
13+
| ~~~~~~~~~~~~~~
1114

1215
warning: 1 warning emitted
1316

Diff for: tests/ui/lint/dropping_copy_types-issue-125189.fixed

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//@ run-rustfix
2+
3+
#![deny(dropping_copy_types)]
4+
5+
fn main() {
6+
let y = 1;
7+
let _ = 3.2; //~ ERROR calls to `std::mem::drop`
8+
let _ = y; //~ ERROR calls to `std::mem::drop`
9+
}

Diff for: tests/ui/lint/dropping_copy_types-issue-125189.rs

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

Diff for: tests/ui/lint/dropping_copy_types.stderr

+16-4
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@ 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 | let _ = s1;
17+
| ~~~~~~~~~~
1518

1619
warning: calls to `std::mem::drop` with a value that implements `Copy` does nothing
1720
--> $DIR/dropping_copy_types.rs:35:5
@@ -21,7 +24,10 @@ LL | drop(s2);
2124
| |
2225
| argument has type `SomeStruct`
2326
|
24-
= note: use `let _ = ...` to ignore the expression or result
27+
help: use `let _ = ...` to ignore the expression or result
28+
|
29+
LL | let _ = s2;
30+
| ~~~~~~~~~~
2531

2632
warning: calls to `std::mem::drop` with a reference instead of an owned value does nothing
2733
--> $DIR/dropping_copy_types.rs:36:5
@@ -42,7 +48,10 @@ LL | drop(s4);
4248
| |
4349
| argument has type `SomeStruct`
4450
|
45-
= note: use `let _ = ...` to ignore the expression or result
51+
help: use `let _ = ...` to ignore the expression or result
52+
|
53+
LL | let _ = s4;
54+
| ~~~~~~~~~~
4655

4756
warning: calls to `std::mem::drop` with a reference instead of an owned value does nothing
4857
--> $DIR/dropping_copy_types.rs:38:5
@@ -82,7 +91,10 @@ LL | drop(println_and(13));
8291
| |
8392
| argument has type `i32`
8493
|
85-
= note: use `let _ = ...` to ignore the expression or result
94+
help: use `let _ = ...` to ignore the expression or result
95+
|
96+
LL | let _ = println_and(13);
97+
| ~~~~~~~~~~~~~~~~~~~~~~~
8698

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

0 commit comments

Comments
 (0)