Skip to content

Commit d0d0a45

Browse files
committed
Let lint_forgetting_copy_types give the suggestion if possible.
1 parent 771d8d4 commit d0d0a45

7 files changed

+113
-5
lines changed

compiler/rustc_lint/messages.ftl

+2
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,8 @@ lint_for_loops_over_fallibles =
225225
lint_forgetting_copy_types = calls to `std::mem::forget` with a value that implements `Copy` does nothing
226226
.label = argument has type `{$arg_ty}`
227227
.note = use `let _ = ...` to ignore the expression or result
228+
.suggestion = use `let _ = ...` to ignore the expression or result
229+
228230
lint_forgetting_references = calls to `std::mem::forget` with a reference instead of an owned value does nothing
229231
.label = argument has type `{$arg_ty}`
230232
.note = use `let _ = ...` to ignore the expression or result

compiler/rustc_lint/src/drop_forget_useless.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ impl<'tcx> LateLintPass<'tcx> for DropForgetUseless {
185185
cx.emit_span_lint(
186186
FORGETTING_COPY_TYPES,
187187
expr.span,
188-
ForgetCopyDiag { arg_ty, label: arg.span },
188+
ForgetCopyDiag { arg_ty, label: arg.span, sugg },
189189
);
190190
}
191191
sym::mem_drop

compiler/rustc_lint/src/lints.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -701,11 +701,12 @@ pub struct ForgetRefDiag<'a> {
701701

702702
#[derive(LintDiagnostic)]
703703
#[diag(lint_forgetting_copy_types)]
704-
#[note]
705704
pub struct ForgetCopyDiag<'a> {
706705
pub arg_ty: Ty<'a>,
707706
#[label]
708707
pub label: Span,
708+
#[subdiagnostic]
709+
pub sugg: IgnoreDropSuggestion,
709710
}
710711

711712
#[derive(LintDiagnostic)]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//@ check-fail
2+
//@ run-rustfix
3+
4+
#![deny(forgetting_copy_types)]
5+
#![allow(unused_mut)]
6+
#![allow(unused_imports)]
7+
8+
use std::vec::Vec;
9+
use std::mem::forget;
10+
11+
#[derive(Copy, Clone)]
12+
struct SomeStruct;
13+
14+
fn main() {
15+
let s1 = SomeStruct {};
16+
let s2 = s1;
17+
let mut s3 = s1;
18+
19+
let _ = s1; //~ ERROR calls to `std::mem::forget`
20+
let _ = s2; //~ ERROR calls to `std::mem::forget`
21+
let _ = s3; //~ ERROR calls to `std::mem::forget`
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//@ check-fail
2+
//@ run-rustfix
3+
4+
#![deny(forgetting_copy_types)]
5+
#![allow(unused_mut)]
6+
#![allow(unused_imports)]
7+
8+
use std::vec::Vec;
9+
use std::mem::forget;
10+
11+
#[derive(Copy, Clone)]
12+
struct SomeStruct;
13+
14+
fn main() {
15+
let s1 = SomeStruct {};
16+
let s2 = s1;
17+
let mut s3 = s1;
18+
19+
forget(s1); //~ ERROR calls to `std::mem::forget`
20+
forget(s2); //~ ERROR calls to `std::mem::forget`
21+
forget(s3); //~ ERROR calls to `std::mem::forget`
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
error: calls to `std::mem::forget` with a value that implements `Copy` does nothing
2+
--> $DIR/forgetting_copy_types-can-fixed.rs:19:5
3+
|
4+
LL | forget(s1);
5+
| ^^^^^^^--^
6+
| |
7+
| argument has type `SomeStruct`
8+
|
9+
note: the lint level is defined here
10+
--> $DIR/forgetting_copy_types-can-fixed.rs:4:9
11+
|
12+
LL | #![deny(forgetting_copy_types)]
13+
| ^^^^^^^^^^^^^^^^^^^^^
14+
help: use `let _ = ...` to ignore the expression or result
15+
|
16+
LL - forget(s1);
17+
LL + let _ = s1;
18+
|
19+
20+
error: calls to `std::mem::forget` with a value that implements `Copy` does nothing
21+
--> $DIR/forgetting_copy_types-can-fixed.rs:20:5
22+
|
23+
LL | forget(s2);
24+
| ^^^^^^^--^
25+
| |
26+
| argument has type `SomeStruct`
27+
|
28+
help: use `let _ = ...` to ignore the expression or result
29+
|
30+
LL - forget(s2);
31+
LL + let _ = s2;
32+
|
33+
34+
error: calls to `std::mem::forget` with a value that implements `Copy` does nothing
35+
--> $DIR/forgetting_copy_types-can-fixed.rs:21:5
36+
|
37+
LL | forget(s3);
38+
| ^^^^^^^--^
39+
| |
40+
| argument has type `SomeStruct`
41+
|
42+
help: use `let _ = ...` to ignore the expression or result
43+
|
44+
LL - forget(s3);
45+
LL + let _ = s3;
46+
|
47+
48+
error: aborting due to 3 previous errors
49+

tests/ui/lint/forgetting_copy_types.stderr

+15-3
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,16 @@ LL | forget(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/forgetting_copy_types.rs:3:9
1211
|
1312
LL | #![warn(forgetting_copy_types)]
1413
| ^^^^^^^^^^^^^^^^^^^^^
14+
help: use `let _ = ...` to ignore the expression or result
15+
|
16+
LL - forget(s1);
17+
LL + let _ = s1;
18+
|
1519

1620
warning: calls to `std::mem::forget` with a value that implements `Copy` does nothing
1721
--> $DIR/forgetting_copy_types.rs:35:5
@@ -21,7 +25,11 @@ LL | forget(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 - forget(s2);
31+
LL + let _ = s2;
32+
|
2533

2634
warning: calls to `std::mem::forget` with a reference instead of an owned value does nothing
2735
--> $DIR/forgetting_copy_types.rs:36:5
@@ -42,7 +50,11 @@ LL | forget(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 - forget(s4);
56+
LL + let _ = s4;
57+
|
4658

4759
warning: calls to `std::mem::forget` with a reference instead of an owned value does nothing
4860
--> $DIR/forgetting_copy_types.rs:38:5

0 commit comments

Comments
 (0)