Skip to content

Commit c715267

Browse files
committed
Apply review suggestions from @y21
1 parent 1a56f90 commit c715267

File tree

3 files changed

+30
-30
lines changed

3 files changed

+30
-30
lines changed

clippy_lints/src/redundant_locals.rs

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@ 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};
7-
use rustc_infer::infer::TyCtxtInferExt;
6+
use rustc_hir::{BindingAnnotation, ByRef, ExprKind, HirId, Local, Node, Pat, PatKind, QPath};
87
use rustc_lint::{LateContext, LateLintPass, LintContext};
98
use rustc_middle::lint::in_external_macro;
109
use rustc_session::{declare_lint_pass, declare_tool_lint};
1110
use rustc_span::symbol::Ident;
1211
use rustc_span::DesugaringKind;
13-
use rustc_trait_selection::infer::InferCtxtExt as _;
1412

1513
declare_clippy_lint! {
1614
/// ### What it does
@@ -68,18 +66,10 @@ impl<'tcx> LateLintPass<'tcx> for RedundantLocals {
6866
// the local does not change the effect of assignments to the binding. see #11290
6967
if !affects_assignments(cx, mutability, binding_id, local.hir_id);
7068
// the local does not affect the code's drop behavior
71-
if !affects_drop_behavior(cx, binding_id, local.hir_id, expr);
69+
if !needs_ordered_drop(cx, cx.typeck_results().expr_ty(expr));
7270
// the local is user-controlled
7371
if !in_external_macro(cx.sess(), local.span);
7472
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();
8373
then {
8474
span_lint_and_help(
8575
cx,
@@ -114,13 +104,3 @@ fn affects_assignments(cx: &LateContext<'_>, mutability: Mutability, bind: HirId
114104
// the binding is mutable and the rebinding is in a different scope than the original binding
115105
mutability == Mutability::Mut && hir.get_enclosing_scope(bind) != hir.get_enclosing_scope(rebind)
116106
}
117-
118-
/// Check if a rebinding of a local affects the code's drop behavior.
119-
fn affects_drop_behavior<'tcx>(cx: &LateContext<'tcx>, bind: HirId, rebind: HirId, rebind_expr: &Expr<'tcx>) -> bool {
120-
let hir = cx.tcx.hir();
121-
122-
// the rebinding is in a different scope than the original binding
123-
// and the type of the binding cares about drop order
124-
hir.get_enclosing_scope(bind) != hir.get_enclosing_scope(rebind)
125-
&& needs_ordered_drop(cx, cx.typeck_results().expr_ty(rebind_expr))
126-
}

tests/ui/redundant_locals.rs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,14 +124,34 @@ impl Drop for WithDrop {
124124
fn drop(&mut self) {}
125125
}
126126

127+
struct InnerDrop(WithDrop);
128+
129+
struct ComposeDrop {
130+
d: WithDrop,
131+
}
132+
127133
struct WithoutDrop(usize);
128134

129135
fn drop_trait() {
130136
let a = WithDrop(1);
131137
let b = WithDrop(2);
132138
let a = a;
139+
}
133140

134-
let c = WithoutDrop(1);
135-
let d = WithoutDrop(2);
136-
let c = c;
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;
137157
}

tests/ui/redundant_locals.stderr

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -134,15 +134,15 @@ LL | let x = x;
134134
= help: remove the redefinition of `x`
135135

136136
error: redundant redefinition of a binding
137-
--> $DIR/redundant_locals.rs:134:9
137+
--> $DIR/redundant_locals.rs:142:9
138138
|
139-
LL | let c = WithoutDrop(1);
139+
LL | let a = WithoutDrop(1);
140140
| ^
141-
LL | let d = WithoutDrop(2);
142-
LL | let c = c;
141+
LL | let b = WithoutDrop(2);
142+
LL | let a = a;
143143
| ^^^^^^^^^^
144144
|
145-
= help: remove the redefinition of `c`
145+
= help: remove the redefinition of `a`
146146

147147
error: aborting due to 14 previous errors
148148

0 commit comments

Comments
 (0)