rust-clippy icon indicating copy to clipboard operation
rust-clippy copied to clipboard

Unnecessary parentheses in suggested fix for `needless_borrow`

Open ilyagr opened this issue 1 year ago • 1 comments

Summary

After updating to the latest nightly, cargo clippy --fix makes an annoying-looking fix with extraneous parentheses.

Reproducer

I ran cargo clippy --workspace in https://github.com/martinvonz/jj/tree/afebea6e730e853ecefb856d1c850612f146c93c. (Sorry, I know this is not minimal).

I got the following warnings. The warnings are correct, but the suggested fix isn't.

warning: this expression creates a reference which is immediately dereferenced by the compiler
   --> lib/src/repo_path.rs:138:33
    |
138 | ..._or_else(|| (&self.value, &self.value[self.v...
    |                 ^^^^^^^^^^^ help: change this to: `(self.value)`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow
    = note: `#[warn(clippy::needless_borrow)]` on by default

warning: this expression creates a reference which is immediately dereferenced by the compiler
   --> lib/src/repo_path.rs:152:51
    |
152 | ....value[..0], &self.value));
    |                 ^^^^^^^^^^^ help: change this to: `(self.value)`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow

warning: `jj-lib` (lib) generated 2 warnings (run `cargo clippy --fix --lib -p jj-lib` to apply 2 suggestions)

I expected to see this happen:

The fix in both cases should be self.value, not (self.value).

Instead, this happened:

See above. Running cargo clippy --fix also applies the weird suggestion, which is then annoying to clean up.

Version

rustc 1.78.0-nightly (d44e3b95c 2024-02-09)
binary: rustc
commit-hash: d44e3b95cb9d410d89cb8ab3233906a33f43756a
commit-date: 2024-02-09
host: aarch64-apple-darwin
release: 1.78.0-nightly
LLVM version: 17.0.6



clippy 0.1.78 (d44e3b95 2024-02-09)

Additional Labels

No response

ilyagr avatar Feb 10 '24 23:02 ilyagr

Example:

fn func(option: Option<(&i32, )>) {
    let foo = (&1, );
    option.unwrap_or((&foo.0, ));
}
warning: this expression creates a reference which is immediately dereferenced by the compiler
 --> src/lib.rs:6:23
  |
6 |     option.unwrap_or((&foo.0, ));
  |                       ^^^^^^ help: change this to: `(foo.0)`

dima74 avatar Feb 18 '24 18:02 dima74