Skip to content

needless_borrow false positive in while loop #10083

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
satyarohith opened this issue Dec 15, 2022 · 3 comments
Closed

needless_borrow false positive in while loop #10083

satyarohith opened this issue Dec 15, 2022 · 3 comments
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

Comments

@satyarohith
Copy link

Summary

needless_borrow lint error when we borrow a value in while loop. But following the suggestion will result in value moved error. I've provided a reproduction.

Lint Name

needless_borrow

Reproducer

use std::{path::PathBuf, process};

fn main() {
    let bat = PathBuf::from("/opt/homebrew/bin/bat");
    let mut files = vec!["a.txt", "b.txt", "c.txt"];
    while let Some(file) = files.pop() {
        let _spawn_result = process::Command::new(&bat)
            .arg(file)
            .current_dir("/")
            .spawn();
    }
}

If we run clippy on the above code, the following error is thrown:

warning: the borrowed expression implements the required traits
 --> src/main.rs:7:51
  |
7 |         let _spawn_result = process::Command::new(&bat)
  |                                                   ^^^^ help: change this to: `bat`
  |
  = 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: `bad_lint` (bin "bad_lint") generated 1 warning
    Finished dev [unoptimized + debuginfo] target(s) in 0.15s

And if we try to follow the suggestion, we get the below error:

error[E0382]: use of moved value: `bat`
 --> src/main.rs:7:51
  |
4 |     let bat = PathBuf::from("/opt/homebrew/bin/bat");
  |         --- move occurs because `bat` has type `std::path::PathBuf`, which does not implement the `Copy` trait
...
7 |         let _spawn_result = process::Command::new(bat)
  |                                                   ^^^ value moved here, in previous iteration of loop

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

Version

rustc 1.66.0 (69f9c33d7 2022-12-12)
binary: rustc
commit-hash: 69f9c33d71c871fc16ac445211281c6e7a340943
commit-date: 2022-12-12
host: aarch64-apple-darwin
release: 1.66.0
LLVM version: 15.0.2

Additional Labels

@rustbot label +l-suggestion-causes-error

@satyarohith satyarohith 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 Dec 15, 2022
@rustbot
Copy link
Collaborator

rustbot commented Dec 15, 2022

Error: Label l-suggestion-causes-error can only be set by Rust team members

Please file an issue on GitHub at triagebot if there's a problem with this bot, or reach out on #t-infra on Zulip.

@eric-seppanen
Copy link

Probably a duplicate of #9778.

@y21
Copy link
Member

y21 commented Jun 16, 2023

@Jarcho Jarcho closed this as completed Jun 16, 2023
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
Projects
None yet
Development

No branches or pull requests

5 participants