Skip to content

Commit 1a56f90

Browse files
committed
Fix: avoid changing drop order
1 parent 81400e2 commit 1a56f90

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

clippy_lints/src/redundant_locals.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ use clippy_utils::ty::needs_ordered_drop;
44
use rustc_ast::Mutability;
55
use rustc_hir::def::Res;
66
use rustc_hir::{BindingAnnotation, ByRef, Expr, ExprKind, HirId, Local, Node, Pat, PatKind, QPath};
7+
use rustc_infer::infer::TyCtxtInferExt;
78
use rustc_lint::{LateContext, LateLintPass, LintContext};
89
use rustc_middle::lint::in_external_macro;
910
use rustc_session::{declare_lint_pass, declare_tool_lint};
1011
use rustc_span::symbol::Ident;
1112
use rustc_span::DesugaringKind;
13+
use rustc_trait_selection::infer::InferCtxtExt as _;
1214

1315
declare_clippy_lint! {
1416
/// ### What it does
@@ -70,6 +72,14 @@ impl<'tcx> LateLintPass<'tcx> for RedundantLocals {
7072
// the local is user-controlled
7173
if !in_external_macro(cx.sess(), local.span);
7274
if !is_from_proc_macro(cx, expr);
75+
// the local does not impl Drop trait. see #11599
76+
let local_ty = cx.typeck_results().node_type(local.hir_id);
77+
if let Some(drop_trait_id) = cx.tcx.lang_items().drop_trait();
78+
if !cx.tcx.infer_ctxt().build().type_implements_trait(
79+
drop_trait_id,
80+
[local_ty],
81+
cx.param_env
82+
).must_apply_modulo_regions();
7383
then {
7484
span_lint_and_help(
7585
cx,

tests/ui/redundant_locals.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,20 @@ fn macros() {
118118
let x = x;
119119
}
120120
}
121+
122+
struct WithDrop(usize);
123+
impl Drop for WithDrop {
124+
fn drop(&mut self) {}
125+
}
126+
127+
struct WithoutDrop(usize);
128+
129+
fn drop_trait() {
130+
let a = WithDrop(1);
131+
let b = WithDrop(2);
132+
let a = a;
133+
134+
let c = WithoutDrop(1);
135+
let d = WithoutDrop(2);
136+
let c = c;
137+
}

tests/ui/redundant_locals.stderr

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,5 +133,16 @@ LL | let x = x;
133133
|
134134
= help: remove the redefinition of `x`
135135

136-
error: aborting due to 13 previous errors
136+
error: redundant redefinition of a binding
137+
--> $DIR/redundant_locals.rs:134:9
138+
|
139+
LL | let c = WithoutDrop(1);
140+
| ^
141+
LL | let d = WithoutDrop(2);
142+
LL | let c = c;
143+
| ^^^^^^^^^^
144+
|
145+
= help: remove the redefinition of `c`
146+
147+
error: aborting due to 14 previous errors
137148

0 commit comments

Comments
 (0)