Let lint_dropping_references give the suggestion if possible.
This commit is contained in:
parent
5870f1ccbb
commit
ca68c93135
7 changed files with 261 additions and 13 deletions
|
@ -238,6 +238,7 @@ 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_duplicate_macro_attribute =
|
||||
duplicated attribute
|
||||
|
|
|
@ -5,7 +5,7 @@ use rustc_span::sym;
|
|||
|
||||
use crate::{
|
||||
lints::{
|
||||
DropCopyDiag, DropCopySuggestion, DropRefDiag, ForgetCopyDiag, ForgetRefDiag,
|
||||
DropCopyDiag, DropRefDiag, ForgetCopyDiag, ForgetRefDiag, IgnoreDropSuggestion,
|
||||
UndroppedManuallyDropsDiag, UndroppedManuallyDropsSuggestion,
|
||||
},
|
||||
LateContext, LateLintPass, LintContext,
|
||||
|
@ -148,12 +148,24 @@ 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()),
|
||||
}
|
||||
} 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 },
|
||||
DropRefDiag { arg_ty, label: arg.span, sugg },
|
||||
);
|
||||
}
|
||||
sym::mem_forget if arg_ty.is_ref() => {
|
||||
|
|
|
@ -656,14 +656,28 @@ pub struct ForLoopsOverFalliblesSuggestion<'a> {
|
|||
pub end_span: Span,
|
||||
}
|
||||
|
||||
#[derive(Subdiagnostic)]
|
||||
pub enum IgnoreDropSuggestion {
|
||||
#[note(lint_note)]
|
||||
Note,
|
||||
#[multipart_suggestion(lint_suggestion, style = "verbose", applicability = "maybe-incorrect")]
|
||||
Suggestion {
|
||||
#[suggestion_part(code = "let _ = ")]
|
||||
start_span: Span,
|
||||
#[suggestion_part(code = "")]
|
||||
end_span: Span,
|
||||
},
|
||||
}
|
||||
|
||||
// drop_forget_useless.rs
|
||||
#[derive(LintDiagnostic)]
|
||||
#[diag(lint_dropping_references)]
|
||||
#[note]
|
||||
pub struct DropRefDiag<'a> {
|
||||
pub arg_ty: Ty<'a>,
|
||||
#[label]
|
||||
pub label: Span,
|
||||
#[subdiagnostic]
|
||||
pub sugg: IgnoreDropSuggestion,
|
||||
}
|
||||
|
||||
#[derive(LintDiagnostic)]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue