Skip to content

needless_borrow bad suggestion - removing needed parens #9160

Closed
@ghost

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)()
    }
}

Playground

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions