about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2022-07-19 21:16:34 +0000
committerbors <bors@rust-lang.org>2022-07-19 21:16:34 +0000
commite98b3ca87b4098804886c11bad130c4d0439b74b (patch)
tree107df8c0c7e11fad6816f248c6a3e714a7b6ae44
parente57c6d6f21fed530b250f54a47a6de8dd841a4f1 (diff)
parent40340745a80904722323845b7f93d43af1c1f83a (diff)
downloadrust-e98b3ca87b4098804886c11bad130c4d0439b74b.tar.gz
rust-e98b3ca87b4098804886c11bad130c4d0439b74b.zip
Auto merge of #9210 - evantypanski:issue9160, r=Manishearth
Fix suggestion causing error for [`needless_borrow`] function in field

Fixes #9160

changelog: [`needless_borrow`]: Fix suggestion removing parens from calling a field
-rw-r--r--clippy_lints/src/dereference.rs3
-rw-r--r--tests/ui/needless_borrow.fixed25
-rw-r--r--tests/ui/needless_borrow.rs25
-rw-r--r--tests/ui/needless_borrow.stderr14
4 files changed, 65 insertions, 2 deletions
diff --git a/clippy_lints/src/dereference.rs b/clippy_lints/src/dereference.rs
index 8c7cf7748be..a90f894a7b1 100644
--- a/clippy_lints/src/dereference.rs
+++ b/clippy_lints/src/dereference.rs
@@ -1028,9 +1028,10 @@ fn report<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, state: State, data
             let mut app = Applicability::MachineApplicable;
             let (snip, snip_is_macro) = snippet_with_context(cx, expr.span, data.span.ctxt(), "..", &mut app);
             span_lint_hir_and_then(cx, NEEDLESS_BORROW, data.hir_id, data.span, state.msg, |diag| {
+                let calls_field = matches!(expr.kind, ExprKind::Field(..)) && matches!(data.position, Position::Callee);
                 let sugg = if !snip_is_macro
-                    && expr.precedence().order() < data.position.precedence()
                     && !has_enclosing_paren(&snip)
+                    && (expr.precedence().order() < data.position.precedence() || calls_field)
                 {
                     format!("({})", snip)
                 } else {
diff --git a/tests/ui/needless_borrow.fixed b/tests/ui/needless_borrow.fixed
index 09afe2ddbbf..bfd2725ecaa 100644
--- a/tests/ui/needless_borrow.fixed
+++ b/tests/ui/needless_borrow.fixed
@@ -158,3 +158,28 @@ fn check_expect_suppression() {
     #[expect(clippy::needless_borrow)]
     let _ = x(&&a);
 }
+
+#[allow(dead_code)]
+mod issue9160 {
+    pub struct S<F> {
+        f: F,
+    }
+
+    impl<T, F> S<F>
+    where
+        F: Fn() -> T,
+    {
+        fn calls_field(&self) -> T {
+            (self.f)()
+        }
+    }
+
+    impl<T, F> S<F>
+    where
+        F: FnMut() -> T,
+    {
+        fn calls_mut_field(&mut self) -> T {
+            (self.f)()
+        }
+    }
+}
diff --git a/tests/ui/needless_borrow.rs b/tests/ui/needless_borrow.rs
index 3ae4722a1f8..c457d8c5471 100644
--- a/tests/ui/needless_borrow.rs
+++ b/tests/ui/needless_borrow.rs
@@ -158,3 +158,28 @@ fn check_expect_suppression() {
     #[expect(clippy::needless_borrow)]
     let _ = x(&&a);
 }
+
+#[allow(dead_code)]
+mod issue9160 {
+    pub struct S<F> {
+        f: F,
+    }
+
+    impl<T, F> S<F>
+    where
+        F: Fn() -> T,
+    {
+        fn calls_field(&self) -> T {
+            (&self.f)()
+        }
+    }
+
+    impl<T, F> S<F>
+    where
+        F: FnMut() -> T,
+    {
+        fn calls_mut_field(&mut self) -> T {
+            (&mut self.f)()
+        }
+    }
+}
diff --git a/tests/ui/needless_borrow.stderr b/tests/ui/needless_borrow.stderr
index 8a2e2b98959..66588689d81 100644
--- a/tests/ui/needless_borrow.stderr
+++ b/tests/ui/needless_borrow.stderr
@@ -120,5 +120,17 @@ error: this expression creates a reference which is immediately dereferenced by
 LL |     (&&5).foo();
    |     ^^^^^ help: change this to: `(&5)`
 
-error: aborting due to 20 previous errors
+error: this expression borrows a value the compiler would automatically borrow
+  --> $DIR/needless_borrow.rs:173:13
+   |
+LL |             (&self.f)()
+   |             ^^^^^^^^^ help: change this to: `(self.f)`
+
+error: this expression borrows a value the compiler would automatically borrow
+  --> $DIR/needless_borrow.rs:182:13
+   |
+LL |             (&mut self.f)()
+   |             ^^^^^^^^^^^^^ help: change this to: `(self.f)`
+
+error: aborting due to 22 previous errors