Fix: avoid changing drop order

This commit is contained in:
koka 2023-10-03 21:22:54 +09:00
parent 81400e2db8
commit 1a56f90ee5
No known key found for this signature in database
GPG key ID: A5917A40697774CD
3 changed files with 39 additions and 1 deletions

View file

@ -4,11 +4,13 @@ use clippy_utils::ty::needs_ordered_drop;
use rustc_ast::Mutability;
use rustc_hir::def::Res;
use rustc_hir::{BindingAnnotation, ByRef, Expr, ExprKind, HirId, Local, Node, Pat, PatKind, QPath};
use rustc_infer::infer::TyCtxtInferExt;
use rustc_lint::{LateContext, LateLintPass, LintContext};
use rustc_middle::lint::in_external_macro;
use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::symbol::Ident;
use rustc_span::DesugaringKind;
use rustc_trait_selection::infer::InferCtxtExt as _;
declare_clippy_lint! {
/// ### What it does
@ -70,6 +72,14 @@ impl<'tcx> LateLintPass<'tcx> for RedundantLocals {
// the local is user-controlled
if !in_external_macro(cx.sess(), local.span);
if !is_from_proc_macro(cx, expr);
// the local does not impl Drop trait. see #11599
let local_ty = cx.typeck_results().node_type(local.hir_id);
if let Some(drop_trait_id) = cx.tcx.lang_items().drop_trait();
if !cx.tcx.infer_ctxt().build().type_implements_trait(
drop_trait_id,
[local_ty],
cx.param_env
).must_apply_modulo_regions();
then {
span_lint_and_help(
cx,

View file

@ -118,3 +118,20 @@ fn macros() {
let x = x;
}
}
struct WithDrop(usize);
impl Drop for WithDrop {
fn drop(&mut self) {}
}
struct WithoutDrop(usize);
fn drop_trait() {
let a = WithDrop(1);
let b = WithDrop(2);
let a = a;
let c = WithoutDrop(1);
let d = WithoutDrop(2);
let c = c;
}

View file

@ -133,5 +133,16 @@ LL | let x = x;
|
= help: remove the redefinition of `x`
error: aborting due to 13 previous errors
error: redundant redefinition of a binding
--> $DIR/redundant_locals.rs:134:9
|
LL | let c = WithoutDrop(1);
| ^
LL | let d = WithoutDrop(2);
LL | let c = c;
| ^^^^^^^^^^
|
= help: remove the redefinition of `c`
error: aborting due to 14 previous errors