Skip to content
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

needless_borrow false positive: if and else have incompatible types #9739

Closed
est31 opened this issue Oct 28, 2022 · 3 comments · Fixed by #9791
Closed

needless_borrow false positive: if and else have incompatible types #9739

est31 opened this issue Oct 28, 2022 · 3 comments · Fixed by #9791
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@est31
Copy link
Member

est31 commented Oct 28, 2022

Summary

If you have a function accepting both a slice and an array, the needless_borrow lint suggests you to turn the slice into an array. Most times this works, but if you have an else/if with differently sized slices in the then/else bodies, applying the lint's suggestion causes a compilation error.

Lint Name

needless_borrow

Reproducer

I tried this code:

fn foo<D: std::fmt::Display>(_it: impl IntoIterator<Item = D>) {}

fn main() {
    foo(if std::env::var_os("HI").is_some()  {
        &[0]
    } else {
        &[] as &[u32]
    });
}

I saw this happen:

error: the borrowed expression implements the required traits
 --> a.rs:7:9
  |
7 |         &[0]
  |         ^^^^ help: change this to: `[0]`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow

Version

rustc 1.66.0-nightly (0da281b60 2022-10-27)

but also on

rustc 1.65.0-beta.4 (a31705549 2022-10-21)

(no idea about stable)

Additional Labels

No response

@est31 est31 added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Oct 28, 2022
@est31
Copy link
Member Author

est31 commented Oct 28, 2022

@rustbot label +I-suggestion-causes-error

@rustbot rustbot added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Oct 28, 2022
@est31
Copy link
Member Author

est31 commented Oct 28, 2022

Duplicating the comment from here (this bug doesn't seem like a dupe):

Mentioning @smoelius @Jarcho who recently worked on this lint in #9136 and #9386.

@smoelius
Copy link
Contributor

@Jarcho Do you have a preference for how this is fixed?

One option might be to require that walk_parents not actually walk any parents when offering warnings like these.

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-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants