Skip to content

needless_borrow stopped triggering on references to Option<&_> if they're returned as part of a tuple #11786

Closed
@liketechnik

Description

@liketechnik

Summary

I found out about this, because a project's CI pipeline failed on code for which a local invocation of cargo clippy reported no errors/warnings. I was able to pin this down to different versions of clippy being installed: clippy 0.1.72 reports the warning, while newer versions (see below) do not report it anymore.

The original code is from a private project, so I created the shown more minimal reproducer.

This issue might have something in common with #11326 which was reported against 1.73.0; which is the first version this issue started showing up, afaict.

Lint Name

needless_borrow

Reproducer

I tried this code:

struct Object;

impl Object {
    fn str_getter(&self) -> Option<&str> {
        None
    }
}

fn main() {
    let o = Object;
    broken(&o);
}

fn broken(o: &Object) -> (&str, usize) {
    (&o.str_getter().unwrap(), 1)
}

I expected to see this happen (as it happens with clippy 0.1.72 (b2b34bd 2023-06-06)):

user$ cargo clippy
    Checking repro-needless-borrow v0.1.0 (/tmp/bla)
warning: this expression creates a reference which is immediately dereferenced by the compiler
  --> src/main.rs:15:6
   |
15 |     (&o.str_getter().unwrap(), 1)
   |      ^^^^^^^^^^^^^^^^^^^^^^^^ help: change this to: `(o.str_getter().unwrap())`
   |
   = 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: `repro-needless-borrow` (bin "repro-needless-borrow") generated 1 warning (run `cargo clippy --fix --bin "repro-needless-borrow"` to apply 1 suggestion)
    Finished dev [unoptimized + debuginfo] target(s) in 0.09s

Instead, this happened (with later clippy versions, starting at clippy 0.1.73 (cc66ad46 2023-10-03), see below):

    Checking repro-needless-borrow v0.1.0 (/tmp/bla)
    Finished dev [unoptimized + debuginfo] target(s) in 0.10s

Version

clippy version reporting the warning: 

- clippy 0.1.72 (b2b34bd 2023-06-06) (1.72 nightly) (with the project in ci and the minimal reproducer)
- clippy 0.1.72 (d5c2e9c 2023-09-13) (1.72.1 stable-2023-09-19) (reproduced locally)
- clippy 0.1.72 (5680fa1 2023-08-23) (1.72.0 stable-2023-08-24) (reproduced locally)


clippy version(s) not reporting the warning: 

- clippy 0.1.75 (fdaaaf9f 2023-11-08) (1.75 nigthly-2023-11-09) (with the local project and the minimal reproducer)
- clippy 0.1.74 (efc300e 2023-11-07) (1.74 beta-2023-11-08) (reproduced locally)
- clippy 0.1.73 (cc66ad46 2023-10-03) (1.73.0 stable-2023-10-05) (with the local project and the minimal reproducer),

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-negativeIssue: The lint should have been triggered on code, but wasn't

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions