Skip to content

Commit d7f0d1f

Browse files
committed
Let lint_forgetting_copy_types give the suggestion if possible.
1 parent ca68c93 commit d7f0d1f

7 files changed

+113
-5
lines changed

compiler/rustc_lint/messages.ftl

+2
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,8 @@ lint_for_loops_over_fallibles =
274274
lint_forgetting_copy_types = calls to `std::mem::forget` with a value that implements `Copy` does nothing
275275
.label = argument has type `{$arg_ty}`
276276
.note = use `let _ = ...` to ignore the expression or result
277+
.suggestion = use `let _ = ...` to ignore the expression or result
278+
277279
lint_forgetting_references = calls to `std::mem::forget` with a reference instead of an owned value does nothing
278280
.label = argument has type `{$arg_ty}`
279281
.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
@@ -199,7 +199,7 @@ impl<'tcx> LateLintPass<'tcx> for DropForgetUseless {
199199
cx.emit_span_lint(
200200
FORGETTING_COPY_TYPES,
201201
expr.span,
202-
ForgetCopyDiag { arg_ty, label: arg.span },
202+
ForgetCopyDiag { arg_ty, label: arg.span, sugg },
203203
);
204204
}
205205
sym::mem_drop

compiler/rustc_lint/src/lints.rs

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

715715
#[derive(LintDiagnostic)]
716716
#[diag(lint_forgetting_copy_types)]
717-
#[note]
718717
pub struct ForgetCopyDiag<'a> {
719718
pub arg_ty: Ty<'a>,
720719
#[label]
721720
pub label: Span,
721+
#[subdiagnostic]
722+
pub sugg: IgnoreDropSuggestion,
722723
}
723724

724725
#[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)