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: "use of moved value" #9710

Closed
dtolnay opened this issue Oct 25, 2022 · 1 comment · Fixed by #9711
Closed

needless_borrow false positive: "use of moved value" #9710

dtolnay opened this issue Oct 25, 2022 · 1 comment · Fixed by #9711
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

@dtolnay
Copy link
Member

dtolnay commented Oct 25, 2022

Summary

Beginning in nightly-2022-10-25, the needless_borrow lint makes suggestions that involve changing a borrow to a move, even when a move would not be legal.

Lint Name

needless_borrow

Reproducer

fn main() {
    let string = String::new();
    for _i in 0..10 {
        f(string);
    }
}

fn f<T: AsRef<str>>(_: T) {}
$ cargo +nightly-2022-10-24 clippy
    Checking testing v0.0.0 (/git/testing)
    Finished dev [unoptimized + debuginfo] target(s) in 0.03s

$ cargo +nightly-2022-10-25 clippy
    Checking testing v0.0.0 (/git/testing)
warning: the borrowed expression implements the required traits
 --> src/main.rs:4:11
  |
4 |         f(&string);
  |           ^^^^^^^ help: change this to: `string`
  |
  = 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: `testing` (bin "testing") generated 1 warning
    Finished dev [unoptimized + debuginfo] target(s) in 0.20s

After making the suggested change:

$ cargo check
    Checking testing v0.0.0 (/git/testing)
error[E0382]: use of moved value: `string`
 --> src/main.rs:4:11
  |
2 |     let string = String::new();
  |         ------ move occurs because `string` has type `String`, which does not implement the `Copy` trait
3 |     for _i in 0..10 {
4 |         f(string);
  |           ^^^^^^ value moved here, in previous iteration of loop

For more information about this error, try `rustc --explain E0382`.
error: could not compile `testing` due to previous error

Version

rustc 1.66.0-nightly (758f19645 2022-10-24)
binary: rustc
commit-hash: 758f19645b8ebce61ea52d1f6672fd057bc8dbee
commit-date: 2022-10-24
host: x86_64-unknown-linux-gnu
release: 1.66.0-nightly
LLVM version: 15.0.2

Additional Labels

@rustbot label +I-suggestion-causes-error

@dtolnay dtolnay 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 25, 2022
@rustbot rustbot added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Oct 25, 2022
@dtolnay
Copy link
Member Author

dtolnay commented Oct 25, 2022

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

dtolnay added a commit to dtolnay/cargo-expand that referenced this issue Oct 25, 2022
rust-lang/rust-clippy#9710

    error[E0382]: use of moved value: `rustfmt`
       --> src/main.rs:244:47
        |
    238 |             if let Some(rustfmt) = rustfmt.or_else(which_rustfmt) {
        |                         ------- move occurs because `rustfmt` has type `PathBuf`, which does not implement the `Copy` trait
    ...
    244 |                     let output = Command::new(rustfmt)
        |                                               ^^^^^^^ value moved here, in previous iteration of loop
smoelius added a commit to smoelius/rust-clippy that referenced this issue Oct 25, 2022
bors added a commit that referenced this issue Oct 29, 2022
Fix `needless_borrow` false positive #9710

Fixes #9710

changelog: fix `needless_borrow` false positive #9710
@bors bors closed this as completed in c42626f Oct 29, 2022
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.

2 participants