Skip to content

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

Closed
@dtolnay

Description

@dtolnay

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

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