Skip to content

Commit

Permalink
Lcargo_add::no_default_features::et lint_forgetting_references give …
Browse files Browse the repository at this point in the history
…the suggestion if possible
  • Loading branch information
surechen committed May 25, 2024
1 parent d0d0a45 commit 066c5e9
Show file tree
Hide file tree
Showing 10 changed files with 383 additions and 46 deletions.
7 changes: 2 additions & 5 deletions compiler/rustc_lint/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,6 @@ lint_dropping_copy_types = calls to `std::mem::drop` with a value that implement
lint_dropping_references = calls to `std::mem::drop` 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
.suggestion = use `let _ = ...` to ignore the expression or result
lint_enum_intrinsics_mem_discriminant =
the return value of `mem::discriminant` is unspecified when called with a non-enum type
Expand All @@ -224,12 +222,9 @@ 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
lint_hidden_unicode_codepoints = unicode codepoint changing visible direction of text present in {$label}
.label = this {$label} contains {$count ->
Expand Down Expand Up @@ -642,5 +637,7 @@ lint_unused_op = unused {$op} that must be used
lint_unused_result = unused result of type `{$ty}`
lint_use_let_underscore_ignore_suggestion = use `let _ = ...` to ignore the expression or result
lint_variant_size_differences =
enum variant is more than three times larger ({$largest} bytes) than the next largest
40 changes: 25 additions & 15 deletions compiler/rustc_lint/src/drop_forget_useless.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use rustc_span::sym;

use crate::{
lints::{
DropCopyDiag, DropRefDiag, ForgetCopyDiag, ForgetRefDiag, IgnoreDropSuggestion,
UndroppedManuallyDropsDiag, UndroppedManuallyDropsSuggestion,
DropCopyDiag, DropRefDiag, ForgetCopyDiag, ForgetRefDiag, UndroppedManuallyDropsDiag,
UndroppedManuallyDropsSuggestion, UseLetUnderscoreIgnoreSuggestion,
},
LateContext, LateLintPass, LintContext,
};
Expand Down Expand Up @@ -147,31 +147,37 @@ impl<'tcx> LateLintPass<'tcx> for DropForgetUseless {
let arg_ty = cx.typeck_results().expr_ty(arg);
let is_copy = arg_ty.is_copy_modulo_regions(cx.tcx, cx.param_env);
let drop_is_single_call_in_arm = is_single_call_in_arm(cx, arg, expr);
let sugg = if let Some((_, node)) = cx.tcx.hir().parent_iter(expr.hir_id).nth(0)
&& let Node::Stmt(stmt) = node
&& let StmtKind::Semi(e) = stmt.kind
&& e.hir_id == expr.hir_id
{
IgnoreDropSuggestion::Suggestion {
start_span: expr.span.shrink_to_lo().until(arg.span),
end_span: arg.span.shrink_to_hi().until(expr.span.shrink_to_hi()),
let let_underscore_ignore_sugg = || {
if let Some((_, node)) = cx.tcx.hir().parent_iter(expr.hir_id).nth(0)
&& let Node::Stmt(stmt) = node
&& let StmtKind::Semi(e) = stmt.kind
&& e.hir_id == expr.hir_id
{
UseLetUnderscoreIgnoreSuggestion::Suggestion {
start_span: expr.span.shrink_to_lo().until(arg.span),
end_span: arg.span.shrink_to_hi().until(expr.span.shrink_to_hi()),
}
} else {
UseLetUnderscoreIgnoreSuggestion::Note
}
} else {
IgnoreDropSuggestion::Note
};
match fn_name {
sym::mem_drop if arg_ty.is_ref() && !drop_is_single_call_in_arm => {
cx.emit_span_lint(
DROPPING_REFERENCES,
expr.span,
DropRefDiag { arg_ty, label: arg.span, sugg },
DropRefDiag { arg_ty, label: arg.span, sugg: let_underscore_ignore_sugg() },
);
}
sym::mem_forget if arg_ty.is_ref() => {
cx.emit_span_lint(
FORGETTING_REFERENCES,
expr.span,
ForgetRefDiag { arg_ty, label: arg.span },
ForgetRefDiag {
arg_ty,
label: arg.span,
sugg: let_underscore_ignore_sugg(),
},
);
}
sym::mem_drop if is_copy && !drop_is_single_call_in_arm => {
Expand All @@ -185,7 +191,11 @@ impl<'tcx> LateLintPass<'tcx> for DropForgetUseless {
cx.emit_span_lint(
FORGETTING_COPY_TYPES,
expr.span,
ForgetCopyDiag { arg_ty, label: arg.span, sugg },
ForgetCopyDiag {
arg_ty,
label: arg.span,
sugg: let_underscore_ignore_sugg(),
},
);
}
sym::mem_drop
Expand Down
17 changes: 11 additions & 6 deletions compiler/rustc_lint/src/lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -658,10 +658,14 @@ pub struct ForLoopsOverFalliblesSuggestion<'a> {
}

#[derive(Subdiagnostic)]
pub enum IgnoreDropSuggestion {
#[note(lint_note)]
pub enum UseLetUnderscoreIgnoreSuggestion {
#[note(lint_use_let_underscore_ignore_suggestion)]
Note,
#[multipart_suggestion(lint_suggestion, style = "verbose", applicability = "maybe-incorrect")]
#[multipart_suggestion(
lint_use_let_underscore_ignore_suggestion,
style = "verbose",
applicability = "maybe-incorrect"
)]
Suggestion {
#[suggestion_part(code = "let _ = ")]
start_span: Span,
Expand All @@ -678,7 +682,7 @@ pub struct DropRefDiag<'a> {
#[label]
pub label: Span,
#[subdiagnostic]
pub sugg: IgnoreDropSuggestion,
pub sugg: UseLetUnderscoreIgnoreSuggestion,
}

#[derive(LintDiagnostic)]
Expand All @@ -692,11 +696,12 @@ pub struct DropCopyDiag<'a> {

#[derive(LintDiagnostic)]
#[diag(lint_forgetting_references)]
#[note]
pub struct ForgetRefDiag<'a> {
pub arg_ty: Ty<'a>,
#[label]
pub label: Span,
#[subdiagnostic]
pub sugg: UseLetUnderscoreIgnoreSuggestion,
}

#[derive(LintDiagnostic)]
Expand All @@ -706,7 +711,7 @@ pub struct ForgetCopyDiag<'a> {
#[label]
pub label: Span,
#[subdiagnostic]
pub sugg: IgnoreDropSuggestion,
pub sugg: UseLetUnderscoreIgnoreSuggestion,
}

#[derive(LintDiagnostic)]
Expand Down
24 changes: 20 additions & 4 deletions tests/ui/lint/dropping_copy_types.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,12 @@ LL | drop(s3);
| |
| argument has type `&SomeStruct`
|
= note: use `let _ = ...` to ignore the expression or result
= note: `#[warn(dropping_references)]` on by default
help: use `let _ = ...` to ignore the expression or result
|
LL - drop(s3);
LL + let _ = s3;
|

warning: calls to `std::mem::drop` with a value that implements `Copy` does nothing
--> $DIR/dropping_copy_types.rs:37:5
Expand All @@ -52,7 +56,11 @@ LL | drop(s5);
| |
| argument has type `&SomeStruct`
|
= note: use `let _ = ...` to ignore the expression or result
help: use `let _ = ...` to ignore the expression or result
|
LL - drop(s5);
LL + let _ = s5;
|

warning: calls to `std::mem::drop` with a reference instead of an owned value does nothing
--> $DIR/dropping_copy_types.rs:50:5
Expand All @@ -62,7 +70,11 @@ LL | drop(a2);
| |
| argument has type `&AnotherStruct`
|
= note: use `let _ = ...` to ignore the expression or result
help: use `let _ = ...` to ignore the expression or result
|
LL - drop(a2);
LL + let _ = a2;
|

warning: calls to `std::mem::drop` with a reference instead of an owned value does nothing
--> $DIR/dropping_copy_types.rs:52:5
Expand All @@ -72,7 +84,11 @@ LL | drop(a4);
| |
| argument has type `&AnotherStruct`
|
= note: use `let _ = ...` to ignore the expression or result
help: use `let _ = ...` to ignore the expression or result
|
LL - drop(a4);
LL + let _ = a4;
|

warning: calls to `std::mem::drop` with a value that implements `Copy` does nothing
--> $DIR/dropping_copy_types.rs:71:13
Expand Down
30 changes: 25 additions & 5 deletions tests/ui/lint/forgetting_copy_types.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,12 @@ LL | forget(s3);
| |
| argument has type `&SomeStruct`
|
= note: use `let _ = ...` to ignore the expression or result
= note: `#[warn(forgetting_references)]` on by default
help: use `let _ = ...` to ignore the expression or result
|
LL - forget(s3);
LL + let _ = s3;
|

warning: calls to `std::mem::forget` with a value that implements `Copy` does nothing
--> $DIR/forgetting_copy_types.rs:37:5
Expand All @@ -64,7 +68,11 @@ LL | forget(s5);
| |
| argument has type `&SomeStruct`
|
= note: use `let _ = ...` to ignore the expression or result
help: use `let _ = ...` to ignore the expression or result
|
LL - forget(s5);
LL + let _ = s5;
|

warning: calls to `std::mem::forget` with a reference instead of an owned value does nothing
--> $DIR/forgetting_copy_types.rs:50:5
Expand All @@ -74,7 +82,11 @@ LL | forget(a2);
| |
| argument has type `&AnotherStruct`
|
= note: use `let _ = ...` to ignore the expression or result
help: use `let _ = ...` to ignore the expression or result
|
LL - forget(a2);
LL + let _ = a2;
|

warning: calls to `std::mem::forget` with a reference instead of an owned value does nothing
--> $DIR/forgetting_copy_types.rs:52:5
Expand All @@ -84,7 +96,11 @@ LL | forget(a3);
| |
| argument has type `&AnotherStruct`
|
= note: use `let _ = ...` to ignore the expression or result
help: use `let _ = ...` to ignore the expression or result
|
LL - forget(a3);
LL + let _ = a3;
|

warning: calls to `std::mem::forget` with a reference instead of an owned value does nothing
--> $DIR/forgetting_copy_types.rs:53:5
Expand All @@ -94,7 +110,11 @@ LL | forget(a4);
| |
| argument has type `&AnotherStruct`
|
= note: use `let _ = ...` to ignore the expression or result
help: use `let _ = ...` to ignore the expression or result
|
LL - forget(a4);
LL + let _ = a4;
|

warning: 8 warnings emitted

40 changes: 40 additions & 0 deletions tests/ui/lint/forgetting_references-can-fixed.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//@ check-fail
//@ run-rustfix

#![deny(forgetting_references)]

use std::mem::forget;

struct SomeStruct;

fn main() {
let _ = &SomeStruct; //~ ERROR calls to `std::mem::forget`

let mut owned = SomeStruct;
let _ = &owned; //~ ERROR calls to `std::mem::forget`
let _ = &&owned; //~ ERROR calls to `std::mem::forget`
let _ = &mut owned; //~ ERROR calls to `std::mem::forget`
forget(owned);

let reference1 = &SomeStruct;
let _ = &*reference1; //~ ERROR calls to `std::mem::forget`

let reference2 = &mut SomeStruct;
let _ = reference2; //~ ERROR calls to `std::mem::forget`

let ref reference3 = SomeStruct;
let _ = reference3; //~ ERROR calls to `std::mem::forget`
}

#[allow(dead_code)]
fn test_generic_fn_forget<T>(val: T) {
let _ = &val; //~ ERROR calls to `std::mem::forget`
forget(val);
}

#[allow(dead_code)]
fn test_similarly_named_function() {
fn forget<T>(_val: T) {}
forget(&SomeStruct); //OK; call to unrelated function which happens to have the same name
let _ = &SomeStruct; //~ ERROR calls to `std::mem::forget`
}
40 changes: 40 additions & 0 deletions tests/ui/lint/forgetting_references-can-fixed.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//@ check-fail
//@ run-rustfix

#![deny(forgetting_references)]

use std::mem::forget;

struct SomeStruct;

fn main() {
forget(&SomeStruct); //~ ERROR calls to `std::mem::forget`

let mut owned = SomeStruct;
forget(&owned); //~ ERROR calls to `std::mem::forget`
forget(&&owned); //~ ERROR calls to `std::mem::forget`
forget(&mut owned); //~ ERROR calls to `std::mem::forget`
forget(owned);

let reference1 = &SomeStruct;
forget(&*reference1); //~ ERROR calls to `std::mem::forget`

let reference2 = &mut SomeStruct;
forget(reference2); //~ ERROR calls to `std::mem::forget`

let ref reference3 = SomeStruct;
forget(reference3); //~ ERROR calls to `std::mem::forget`
}

#[allow(dead_code)]
fn test_generic_fn_forget<T>(val: T) {
forget(&val); //~ ERROR calls to `std::mem::forget`
forget(val);
}

#[allow(dead_code)]
fn test_similarly_named_function() {
fn forget<T>(_val: T) {}
forget(&SomeStruct); //OK; call to unrelated function which happens to have the same name
std::mem::forget(&SomeStruct); //~ ERROR calls to `std::mem::forget`
}
Loading

0 comments on commit 066c5e9

Please sign in to comment.