Skip to content

Commit 3fe2478

Browse files
committed
don't unnecessarily walk more in visitor and add more tests
1 parent e305b07 commit 3fe2478

File tree

4 files changed

+30
-4
lines changed

4 files changed

+30
-4
lines changed

clippy_lints/src/redundant_closure_call.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ impl<'tcx> Visitor<'tcx> for ReturnVisitor {
5555
fn visit_expr(&mut self, ex: &'tcx hir::Expr<'tcx>) {
5656
if let hir::ExprKind::Ret(_) | hir::ExprKind::Match(.., hir::MatchSource::TryDesugar) = ex.kind {
5757
self.found_return = true;
58+
} else {
59+
hir_visit::walk_expr(self, ex);
5860
}
59-
60-
hir_visit::walk_expr(self, ex);
6161
}
6262
}
6363

@@ -122,7 +122,7 @@ fn get_parent_call_exprs<'tcx>(
122122
let mut depth = 1;
123123
while let Some(parent) = get_parent_expr(cx, expr)
124124
&& let hir::ExprKind::Call(recv, _) = parent.kind
125-
&& let hir::ExprKind::Call(..) = recv.kind
125+
&& expr.span == recv.span
126126
{
127127
expr = parent;
128128
depth += 1;

tests/ui/redundant_closure_call_fixable.fixed

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,11 @@ fn issue9956() {
7878
|| || || 42
7979
}
8080
let _ = x()()()();
81+
82+
fn bar() -> fn(i32, i32) {
83+
foo
84+
}
85+
fn foo(_: i32, _: i32) {}
86+
bar()(42, 5);
87+
foo(42, 5);
8188
}

tests/ui/redundant_closure_call_fixable.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,11 @@ fn issue9956() {
7878
|| || || 42
7979
}
8080
let _ = x()()()();
81+
82+
fn bar() -> fn(i32, i32) {
83+
foo
84+
}
85+
fn foo(_: i32, _: i32) {}
86+
bar()((|| || 42)()(), 5);
87+
foo((|| || 42)()(), 5);
8188
}

tests/ui/redundant_closure_call_fixable.stderr

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,5 +110,17 @@ error: try not to call a closure in the expression where it is declared
110110
LL | let a = (|| echo!((|| 123)))()();
111111
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try doing something like: `123`
112112

113-
error: aborting due to 12 previous errors
113+
error: try not to call a closure in the expression where it is declared
114+
--> $DIR/redundant_closure_call_fixable.rs:86:11
115+
|
116+
LL | bar()((|| || 42)()(), 5);
117+
| ^^^^^^^^^^^^^^ help: try doing something like: `42`
118+
119+
error: try not to call a closure in the expression where it is declared
120+
--> $DIR/redundant_closure_call_fixable.rs:87:9
121+
|
122+
LL | foo((|| || 42)()(), 5);
123+
| ^^^^^^^^^^^^^^ help: try doing something like: `42`
124+
125+
error: aborting due to 14 previous errors
114126

0 commit comments

Comments
 (0)