Let lint_forgetting_copy_types give the suggestion if possible.

This commit is contained in:
surechen 2024-05-25 11:04:13 +08:00
parent ca68c93135
commit d7f0d1f564
7 changed files with 113 additions and 5 deletions

View File

@ -274,6 +274,8 @@ lint_for_loops_over_fallibles =
lint_forgetting_copy_types = calls to `std::mem::forget` with a value that implements `Copy` does nothing
.label = argument has type `{$arg_ty}`
.note = use `let _ = ...` to ignore the expression or result
.suggestion = use `let _ = ...` to ignore the expression or result
lint_forgetting_references = calls to `std::mem::forget` with a reference instead of an owned value does nothing
.label = argument has type `{$arg_ty}`
.note = use `let _ = ...` to ignore the expression or result

View File

@ -199,7 +199,7 @@ fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
cx.emit_span_lint(
FORGETTING_COPY_TYPES,
expr.span,
ForgetCopyDiag { arg_ty, label: arg.span },
ForgetCopyDiag { arg_ty, label: arg.span, sugg },
);
}
sym::mem_drop

View File

@ -714,11 +714,12 @@ pub struct ForgetRefDiag<'a> {
#[derive(LintDiagnostic)]
#[diag(lint_forgetting_copy_types)]
#[note]
pub struct ForgetCopyDiag<'a> {
pub arg_ty: Ty<'a>,
#[label]
pub label: Span,
#[subdiagnostic]
pub sugg: IgnoreDropSuggestion,
}
#[derive(LintDiagnostic)]

View File

@ -0,0 +1,22 @@
//@ check-fail
//@ run-rustfix
#![deny(forgetting_copy_types)]
#![allow(unused_mut)]
#![allow(unused_imports)]
use std::vec::Vec;
use std::mem::forget;
#[derive(Copy, Clone)]
struct SomeStruct;
fn main() {
let s1 = SomeStruct {};
let s2 = s1;
let mut s3 = s1;
let _ = s1; //~ ERROR calls to `std::mem::forget`
let _ = s2; //~ ERROR calls to `std::mem::forget`
let _ = s3; //~ ERROR calls to `std::mem::forget`
}

View File

@ -0,0 +1,22 @@
//@ check-fail
//@ run-rustfix
#![deny(forgetting_copy_types)]
#![allow(unused_mut)]
#![allow(unused_imports)]
use std::vec::Vec;
use std::mem::forget;
#[derive(Copy, Clone)]
struct SomeStruct;
fn main() {
let s1 = SomeStruct {};
let s2 = s1;
let mut s3 = s1;
forget(s1); //~ ERROR calls to `std::mem::forget`
forget(s2); //~ ERROR calls to `std::mem::forget`
forget(s3); //~ ERROR calls to `std::mem::forget`
}

View File

@ -0,0 +1,49 @@
error: calls to `std::mem::forget` with a value that implements `Copy` does nothing
--> $DIR/forgetting_copy_types-can-fixed.rs:19:5
|
LL | forget(s1);
| ^^^^^^^--^
| |
| argument has type `SomeStruct`
|
note: the lint level is defined here
--> $DIR/forgetting_copy_types-can-fixed.rs:4:9
|
LL | #![deny(forgetting_copy_types)]
| ^^^^^^^^^^^^^^^^^^^^^
help: use `let _ = ...` to ignore the expression or result
|
LL - forget(s1);
LL + let _ = s1;
|
error: calls to `std::mem::forget` with a value that implements `Copy` does nothing
--> $DIR/forgetting_copy_types-can-fixed.rs:20:5
|
LL | forget(s2);
| ^^^^^^^--^
| |
| argument has type `SomeStruct`
|
help: use `let _ = ...` to ignore the expression or result
|
LL - forget(s2);
LL + let _ = s2;
|
error: calls to `std::mem::forget` with a value that implements `Copy` does nothing
--> $DIR/forgetting_copy_types-can-fixed.rs:21:5
|
LL | forget(s3);
| ^^^^^^^--^
| |
| argument has type `SomeStruct`
|
help: use `let _ = ...` to ignore the expression or result
|
LL - forget(s3);
LL + let _ = s3;
|
error: aborting due to 3 previous errors

View File

@ -6,12 +6,16 @@ LL | forget(s1);
| |
| argument has type `SomeStruct`
|
= note: use `let _ = ...` to ignore the expression or result
note: the lint level is defined here
--> $DIR/forgetting_copy_types.rs:3:9
|
LL | #![warn(forgetting_copy_types)]
| ^^^^^^^^^^^^^^^^^^^^^
help: use `let _ = ...` to ignore the expression or result
|
LL - forget(s1);
LL + let _ = s1;
|
warning: calls to `std::mem::forget` with a value that implements `Copy` does nothing
--> $DIR/forgetting_copy_types.rs:35:5
@ -21,7 +25,11 @@ LL | forget(s2);
| |
| argument has type `SomeStruct`
|
= note: use `let _ = ...` to ignore the expression or result
help: use `let _ = ...` to ignore the expression or result
|
LL - forget(s2);
LL + let _ = s2;
|
warning: calls to `std::mem::forget` with a reference instead of an owned value does nothing
--> $DIR/forgetting_copy_types.rs:36:5
@ -42,7 +50,11 @@ LL | forget(s4);
| |
| argument has type `SomeStruct`
|
= note: use `let _ = ...` to ignore the expression or result
help: use `let _ = ...` to ignore the expression or result
|
LL - forget(s4);
LL + let _ = s4;
|
warning: calls to `std::mem::forget` with a reference instead of an owned value does nothing
--> $DIR/forgetting_copy_types.rs:38:5