Skip to content

Commit e98b3ca

Browse files
committed
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
2 parents e57c6d6 + 4034074 commit e98b3ca

File tree

4 files changed

+65
-2
lines changed

4 files changed

+65
-2
lines changed

clippy_lints/src/dereference.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1028,9 +1028,10 @@ fn report<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, state: State, data
10281028
let mut app = Applicability::MachineApplicable;
10291029
let (snip, snip_is_macro) = snippet_with_context(cx, expr.span, data.span.ctxt(), "..", &mut app);
10301030
span_lint_hir_and_then(cx, NEEDLESS_BORROW, data.hir_id, data.span, state.msg, |diag| {
1031+
let calls_field = matches!(expr.kind, ExprKind::Field(..)) && matches!(data.position, Position::Callee);
10311032
let sugg = if !snip_is_macro
1032-
&& expr.precedence().order() < data.position.precedence()
10331033
&& !has_enclosing_paren(&snip)
1034+
&& (expr.precedence().order() < data.position.precedence() || calls_field)
10341035
{
10351036
format!("({})", snip)
10361037
} else {

tests/ui/needless_borrow.fixed

+25
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,28 @@ fn check_expect_suppression() {
158158
#[expect(clippy::needless_borrow)]
159159
let _ = x(&&a);
160160
}
161+
162+
#[allow(dead_code)]
163+
mod issue9160 {
164+
pub struct S<F> {
165+
f: F,
166+
}
167+
168+
impl<T, F> S<F>
169+
where
170+
F: Fn() -> T,
171+
{
172+
fn calls_field(&self) -> T {
173+
(self.f)()
174+
}
175+
}
176+
177+
impl<T, F> S<F>
178+
where
179+
F: FnMut() -> T,
180+
{
181+
fn calls_mut_field(&mut self) -> T {
182+
(self.f)()
183+
}
184+
}
185+
}

tests/ui/needless_borrow.rs

+25
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,28 @@ fn check_expect_suppression() {
158158
#[expect(clippy::needless_borrow)]
159159
let _ = x(&&a);
160160
}
161+
162+
#[allow(dead_code)]
163+
mod issue9160 {
164+
pub struct S<F> {
165+
f: F,
166+
}
167+
168+
impl<T, F> S<F>
169+
where
170+
F: Fn() -> T,
171+
{
172+
fn calls_field(&self) -> T {
173+
(&self.f)()
174+
}
175+
}
176+
177+
impl<T, F> S<F>
178+
where
179+
F: FnMut() -> T,
180+
{
181+
fn calls_mut_field(&mut self) -> T {
182+
(&mut self.f)()
183+
}
184+
}
185+
}

tests/ui/needless_borrow.stderr

+13-1
Original file line numberDiff line numberDiff line change
@@ -120,5 +120,17 @@ error: this expression creates a reference which is immediately dereferenced by
120120
LL | (&&5).foo();
121121
| ^^^^^ help: change this to: `(&5)`
122122

123-
error: aborting due to 20 previous errors
123+
error: this expression borrows a value the compiler would automatically borrow
124+
--> $DIR/needless_borrow.rs:173:13
125+
|
126+
LL | (&self.f)()
127+
| ^^^^^^^^^ help: change this to: `(self.f)`
128+
129+
error: this expression borrows a value the compiler would automatically borrow
130+
--> $DIR/needless_borrow.rs:182:13
131+
|
132+
LL | (&mut self.f)()
133+
| ^^^^^^^^^^^^^ help: change this to: `(self.f)`
134+
135+
error: aborting due to 22 previous errors
124136

0 commit comments

Comments
 (0)