Closed
Description
Summary
With a very recent nightly build, the needless_borrow lint suggests removing a necessary borrow when the borrow occurs in a loop (though I haven't spent any time trying to figure out exactly what the condition is).
Lint Name
needless_borrow
Reproducer
I tried this code:
fn main() {
let x = std::path::PathBuf::from("foo");
for _ in 0..3 {
std::fs::read(&x).ok();
}
}
I saw this happen:
Checking borrow v0.0.1 (/tmp/tmp.GV7LLZAxFz)
warning: failed to automatically apply fixes suggested by rustc to crate `borrow`
after fixes were automatically applied the compiler reported errors within these files:
* src/main.rs
This likely indicates a bug in either rustc or cargo itself,
and we would appreciate a bug report! You're likely to see
a number of compiler warnings after this message which cargo
attempted to fix but failed. If you could open an issue at
https://github.com/rust-lang/rust/issues
quoting the full output of this command we'd be very appreciative!
Note that you may be able to make some more progress in the near-term
fixing code with the `--broken-code` flag
The following errors were reported:
error[E0382]: use of moved value: `x`
--> src/main.rs:4:23
|
2 | let x = std::path::PathBuf::from("foo");
| - move occurs because `x` has type `std::path::PathBuf`, which does not implement the `Copy` trait
3 | for _ in 0..3 {
4 | std::fs::read(x).ok();
| ^ value moved here, in previous iteration of loop
error: aborting due to previous error
For more information about this error, try `rustc --explain E0382`.
Original diagnostics will follow.
warning: the borrowed expression implements the required traits
--> src/main.rs:4:23
|
4 | std::fs::read(&x).ok();
| ^^ help: change this to: `x`
|
= 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: `borrow` (bin "borrow") generated 1 warning
warning: `borrow` (bin "borrow" test) generated 1 warning (1 duplicate)
Finished dev [unoptimized + debuginfo] target(s) in 0.53s
I expected to see this happen:
No warning
Version
rustc 1.67.0-nightly (11ebe6512 2022-11-01)
binary: rustc
commit-hash: 11ebe6512b4c77633c59f8dcdd421df3b79d1a9f
commit-date: 2022-11-01
host: x86_64-unknown-linux-gnu
release: 1.67.0-nightly
LLVM version: 15.0.4
Additional Labels
No response