Skip to content

Commit b437069

Browse files
committed
Auto merge of #11603 - koka831:fix/11599, r=y21
Fix: avoid changing drop order Fixes rust-lang/rust-clippy#11599 changelog: [`redundant_locals`] No longer lints which implements Drop trait to avoid reordering
2 parents 29958f0 + c715267 commit b437069

File tree

3 files changed

+51
-13
lines changed

3 files changed

+51
-13
lines changed

clippy_lints/src/redundant_locals.rs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use clippy_utils::is_from_proc_macro;
33
use clippy_utils::ty::needs_ordered_drop;
44
use rustc_ast::Mutability;
55
use rustc_hir::def::Res;
6-
use rustc_hir::{BindingAnnotation, ByRef, Expr, ExprKind, HirId, Local, Node, Pat, PatKind, QPath};
6+
use rustc_hir::{BindingAnnotation, ByRef, ExprKind, HirId, Local, Node, Pat, PatKind, QPath};
77
use rustc_lint::{LateContext, LateLintPass, LintContext};
88
use rustc_middle::lint::in_external_macro;
99
use rustc_session::{declare_lint_pass, declare_tool_lint};
@@ -66,7 +66,7 @@ impl<'tcx> LateLintPass<'tcx> for RedundantLocals {
6666
// the local does not change the effect of assignments to the binding. see #11290
6767
if !affects_assignments(cx, mutability, binding_id, local.hir_id);
6868
// the local does not affect the code's drop behavior
69-
if !affects_drop_behavior(cx, binding_id, local.hir_id, expr);
69+
if !needs_ordered_drop(cx, cx.typeck_results().expr_ty(expr));
7070
// the local is user-controlled
7171
if !in_external_macro(cx.sess(), local.span);
7272
if !is_from_proc_macro(cx, expr);
@@ -104,13 +104,3 @@ fn affects_assignments(cx: &LateContext<'_>, mutability: Mutability, bind: HirId
104104
// the binding is mutable and the rebinding is in a different scope than the original binding
105105
mutability == Mutability::Mut && hir.get_enclosing_scope(bind) != hir.get_enclosing_scope(rebind)
106106
}
107-
108-
/// Check if a rebinding of a local affects the code's drop behavior.
109-
fn affects_drop_behavior<'tcx>(cx: &LateContext<'tcx>, bind: HirId, rebind: HirId, rebind_expr: &Expr<'tcx>) -> bool {
110-
let hir = cx.tcx.hir();
111-
112-
// the rebinding is in a different scope than the original binding
113-
// and the type of the binding cares about drop order
114-
hir.get_enclosing_scope(bind) != hir.get_enclosing_scope(rebind)
115-
&& needs_ordered_drop(cx, cx.typeck_results().expr_ty(rebind_expr))
116-
}

tests/ui/redundant_locals.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,40 @@ 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 InnerDrop(WithDrop);
128+
129+
struct ComposeDrop {
130+
d: WithDrop,
131+
}
132+
133+
struct WithoutDrop(usize);
134+
135+
fn drop_trait() {
136+
let a = WithDrop(1);
137+
let b = WithDrop(2);
138+
let a = a;
139+
}
140+
141+
fn without_drop() {
142+
let a = WithoutDrop(1);
143+
let b = WithoutDrop(2);
144+
let a = a;
145+
}
146+
147+
fn drop_inner() {
148+
let a = InnerDrop(WithDrop(1));
149+
let b = InnerDrop(WithDrop(2));
150+
let a = a;
151+
}
152+
153+
fn drop_compose() {
154+
let a = ComposeDrop { d: WithDrop(1) };
155+
let b = ComposeDrop { d: WithDrop(1) };
156+
let a = a;
157+
}

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:142:9
138+
|
139+
LL | let a = WithoutDrop(1);
140+
| ^
141+
LL | let b = WithoutDrop(2);
142+
LL | let a = a;
143+
| ^^^^^^^^^^
144+
|
145+
= help: remove the redefinition of `a`
146+
147+
error: aborting due to 14 previous errors
137148

0 commit comments

Comments
 (0)