Skip to content

needless_reborrow false positive with trait that consumes self #16200

@shadowcatzero

Description

@shadowcatzero

Summary

Just learned about how rust prioritizes T -> &T -> &mut T when searching for a trait method here, and it appears that clippy gives a needless_reborrow warning even though it's an error if you remove the reborrow in one of these cases.

Bug aside, that selection process is a little unintuitive to me in this case (I expected an ambiguity error originally), and I wonder if there should be a warning or hint of some kind (without the reborrow / when there's an error) that makes it clear that other implementations exist on later "levels" (T, &T, &mut T). I've only ran into this once though and immediately knew what was wrong, so not sure if that's worth the effort.

Lint Name

needless_borrow

Reproducer

I tried this code:

trait Trait {
    fn run(&self);
}

struct Test;
impl Test {
    fn run(self, x: f32) {}
}
impl Trait for Test {
    fn run(&self) {}
}

fn main() {
    (&Test).run();
}

I saw this happen:

    (&Test).run(3.0);
//  ^^^^^^^
// Diagnostics:
// 1. this expression borrows a value the compiler would automatically borrow
//    for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow
//    `#[warn(clippy::needless_borrow)]` on by default [needless_borrow]
// 2. change this to: `Test` [needless_borrow]

after applying suggestion:

    Test.run();
//       ^^^^^
// Diagnostics:
// 1. expected 1 argument, found 0 [E0107]
// 2. this method takes 1 argument but 0 arguments were supplied [E0061]
// 3. argument #1 of type `f32` is missing [E0061]
// 4. provide the argument: `(/* f32 */)` [E0061]

I expected to see this happen:

no warning or suggestion

Version

rustc 1.94.0-nightly (1aa9bab4e 2025-12-05)
binary: rustc
commit-hash: 1aa9bab4ecbce4859eaad53000f78158ebe2be2c
commit-date: 2025-12-05
host: x86_64-unknown-linux-gnu
release: 1.94.0-nightly
LLVM version: 21.1.5

Additional Labels

@rustbot label +I-suggestion-causes-error

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't haveI-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