Skip to content

needless_borrow should trigger when taking a reference to Option<&_> #11326

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jplatte opened this issue Aug 11, 2023 · 2 comments
Closed

needless_borrow should trigger when taking a reference to Option<&_> #11326

jplatte opened this issue Aug 11, 2023 · 2 comments
Labels
C-bug Category: Clippy is not doing the correct thing I-false-negative Issue: The lint should have been triggered on code, but wasn't

Comments

@jplatte
Copy link
Contributor

jplatte commented Aug 11, 2023

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;
    if let Some(s) = &o.str_getter() {
        println!("{s}");
    }
}

I expected to see this happen: needless_borrow triggering for &o.str_getter()

Instead, this happened: nothing

Version

Tested on Rust playground, hence less info. Let me know if you need me to reproduce locally.

Nightly version: 1.73.0-nightly
(2023-08-10 439d066bcf9496b1b8c8)
@jplatte jplatte added C-bug Category: Clippy is not doing the correct thing I-false-negative Issue: The lint should have been triggered on code, but wasn't labels Aug 11, 2023
@samueltardieu
Copy link
Contributor

Just to make sure I understand the issue at hand, on which part of code would the needless_borrow lint apply?

If this is about the & in &o.str_getter(), removing it would change the type of s from &&str to &str due to match ergonomics. Even if it would work here as Display is implemented the same way on &&str and &str, it is not equivalent if s is later used in a larger expression.

Of course, in (&o).str_getter(), the lint would trigger, as this borrow would indeed be needless.

@jplatte
Copy link
Contributor Author

jplatte commented Jan 28, 2025

I didn't think of that. I guess involving extra analysis on whether the bound variable from the if let needs to be a double ref would be become pretty complex pretty fast. Feel free to close this.

@samueltardieu samueltardieu closed this as not planned Won't fix, can't repro, duplicate, stale Jan 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-negative Issue: The lint should have been triggered on code, but wasn't
Projects
None yet
Development

No branches or pull requests

2 participants