Closed
Description
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