Closed

Description
Summary
needless_borrow
suggests removing parenthesis when calling a function stored in a field with a needless borrow. The warning is valid but removing the parenthesis causes a compiler error. This is only really a problem when using cargo clippy --fix
.
Reproducer
I tried this code:
pub struct S<F> {
f: F,
}
impl<T,F> S<F> where F: FnMut() -> T,
{
fn g(&mut self) -> T {
(&mut self.f)()
}
}
I expected to see this happen:
A suggestion to remove &mut
from (&mut self.f)()
or replace (&mut self.f)
with (self.f)
.
Instead, this happened:
A suggestion to replace (&mut self.f)
with self.f
.
warning: this expression borrows a value the compiler would automatically borrow
--> src/lib.rs:8:9
|
8 | (&mut self.f)()
| ^^^^^^^^^^^^^ help: change this to: `self.f`
|
= note: `#[warn(clippy::needless_borrow)]` on by default
This leads to the error:
|
8 | self.f()
| ^ field, not a method
|
help: to call the function stored in `f`, surround the field access with parentheses
|
8 | (self.f)()
Version
0.1.64 (2022-07-11) 38b7215 on the Rust Playground
Additional Labels
@rustbot +label I-suggestion-causes-error