about summary refs log tree commit diff
diff options
context:
space:
mode:
authory21 <30553356+y21@users.noreply.github.com>2023-06-15 22:04:25 +0200
committery21 <30553356+y21@users.noreply.github.com>2023-06-15 22:04:25 +0200
commit3fe2478ecf19d4d62cbdfcf50d57bc00fe5bb242 (patch)
treed956daf8edfbb828d6836be58c20509918b3daf0
parente305b0730fedfe3fa8b54dbb285f498a3277baa9 (diff)
downloadrust-3fe2478ecf19d4d62cbdfcf50d57bc00fe5bb242.tar.gz
rust-3fe2478ecf19d4d62cbdfcf50d57bc00fe5bb242.zip
don't unnecessarily walk more in visitor and add more tests
-rw-r--r--clippy_lints/src/redundant_closure_call.rs6
-rw-r--r--tests/ui/redundant_closure_call_fixable.fixed7
-rw-r--r--tests/ui/redundant_closure_call_fixable.rs7
-rw-r--r--tests/ui/redundant_closure_call_fixable.stderr14
4 files changed, 30 insertions, 4 deletions
diff --git a/clippy_lints/src/redundant_closure_call.rs b/clippy_lints/src/redundant_closure_call.rs
index d979087a6d7..b6ce4ebc28f 100644
--- a/clippy_lints/src/redundant_closure_call.rs
+++ b/clippy_lints/src/redundant_closure_call.rs
@@ -55,9 +55,9 @@ impl<'tcx> Visitor<'tcx> for ReturnVisitor {
     fn visit_expr(&mut self, ex: &'tcx hir::Expr<'tcx>) {
         if let hir::ExprKind::Ret(_) | hir::ExprKind::Match(.., hir::MatchSource::TryDesugar) = ex.kind {
             self.found_return = true;
+        } else {
+            hir_visit::walk_expr(self, ex);
         }
-
-        hir_visit::walk_expr(self, ex);
     }
 }
 
@@ -122,7 +122,7 @@ fn get_parent_call_exprs<'tcx>(
     let mut depth = 1;
     while let Some(parent) = get_parent_expr(cx, expr)
         && let hir::ExprKind::Call(recv, _) = parent.kind
-        && let hir::ExprKind::Call(..) = recv.kind
+        && expr.span == recv.span
     {
         expr = parent;
         depth += 1;
diff --git a/tests/ui/redundant_closure_call_fixable.fixed b/tests/ui/redundant_closure_call_fixable.fixed
index 1a2c1e59261..f3669a669ea 100644
--- a/tests/ui/redundant_closure_call_fixable.fixed
+++ b/tests/ui/redundant_closure_call_fixable.fixed
@@ -78,4 +78,11 @@ fn issue9956() {
         || || || 42
     }
     let _ = x()()()();
+
+    fn bar() -> fn(i32, i32) {
+        foo
+    }
+    fn foo(_: i32, _: i32) {}
+    bar()(42, 5);
+    foo(42, 5);
 }
diff --git a/tests/ui/redundant_closure_call_fixable.rs b/tests/ui/redundant_closure_call_fixable.rs
index fec46652189..db8c7f80df4 100644
--- a/tests/ui/redundant_closure_call_fixable.rs
+++ b/tests/ui/redundant_closure_call_fixable.rs
@@ -78,4 +78,11 @@ fn issue9956() {
         || || || 42
     }
     let _ = x()()()();
+
+    fn bar() -> fn(i32, i32) {
+        foo
+    }
+    fn foo(_: i32, _: i32) {}
+    bar()((|| || 42)()(), 5);
+    foo((|| || 42)()(), 5);
 }
diff --git a/tests/ui/redundant_closure_call_fixable.stderr b/tests/ui/redundant_closure_call_fixable.stderr
index 351da54ef31..618f5e071d6 100644
--- a/tests/ui/redundant_closure_call_fixable.stderr
+++ b/tests/ui/redundant_closure_call_fixable.stderr
@@ -110,5 +110,17 @@ error: try not to call a closure in the expression where it is declared
 LL |     let a = (|| echo!((|| 123)))()();
    |             ^^^^^^^^^^^^^^^^^^^^^^^^ help: try doing something like: `123`
 
-error: aborting due to 12 previous errors
+error: try not to call a closure in the expression where it is declared
+  --> $DIR/redundant_closure_call_fixable.rs:86:11
+   |
+LL |     bar()((|| || 42)()(), 5);
+   |           ^^^^^^^^^^^^^^ help: try doing something like: `42`
+
+error: try not to call a closure in the expression where it is declared
+  --> $DIR/redundant_closure_call_fixable.rs:87:9
+   |
+LL |     foo((|| || 42)()(), 5);
+   |         ^^^^^^^^^^^^^^ help: try doing something like: `42`
+
+error: aborting due to 14 previous errors