-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
False positive on redundant clone: borrow of moved value #10940
Comments
@rustbot label I-false-positive |
Hmm, appears to have something to do with the format! macro. The following snippet does NOT generate a redundant clone warning: #[derive(Clone)]
struct Variable {
name: String,
}
fn generate_for_loop(ident: Variable) -> String {
let mut expr_ident = ident.clone();
expr_ident.name = "testing".to_owned();
ident.name
}
fn main() {} However, changing the return My (admittedly not-very-well-informed) theory is that the use of format! prevents clippy from noticing that both expr_ident and ident are being used - perhaps because the I'd like to take a shot at this: @rustbot claim |
Thanks for the small snippet. I think this is likely more complicated though, some of the other reports contain no macro calls - #10873 |
Just clicked through the issue you linked in more depth, and it appears y'all have already found the root issue. If there's anything left to do that I can help with, I'd love to join in, otherwise I'll just leave this here and find something else to do :) |
This is hitting me particularly hard. I'm running
that I guess includes the recent move to nursery. Shouldn't that have removed the warning? I also tested with a new Cargo project, and the warning is still present with my simplified reproduction below. #[derive(Clone, Eq, PartialEq)]
struct T {}
impl T {
fn b(&self) -> bool {
false
}
fn f(&self) -> T {
T {}
}
}
fn main() {
let x = T {};
let mut y = x.clone();
while y.b() {
y = y.f();
}
println!("{}", y == x);
} Do I need to manually disable this warning? |
Nightly doesn't have that change yet, the In the meanwhile yeah you can manually disable it |
I've a similar error with this sample code on rust fn test() {
let foo1 = String::from("I'm a simple string");
let foo2 = foo1.clone();
assert_eq!(foo1, foo2);
} Here is the output of Checking redundant-clone v0.1.0 (/home/[REDACTED]/Documents/parsec-cloud/redundant-clone)
warning: failed to automatically apply fixes suggested by rustc to crate `redundant_clone`
after fixes were automatically applied the compiler reported errors within these files:
* /home/[REDACTED]/.rustup/toolchains/1.70.0-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/macros/mod.rs
* redundant-clone/src/lib.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]: borrow of moved value: `foo1`
--> redundant-clone/src/lib.rs:5:5
|
2 | let foo1 = String::from("I'm a simple string");
| ---- move occurs because `foo1` has type `std::string::String`, which does not implement the `Copy` trait
3 | let foo2 = foo1;
| ---- value moved here
4 |
5 | assert_eq!(foo1, foo2);
| ^^^^^^^^^^^^^^^^^^^^^^ value borrowed here after move
|
= note: this error originates in the macro `assert_eq` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider cloning the value if the performance cost is acceptable
|
3 | let foo2 = foo1.clone();
| ++++++++
error: aborting due to previous error
For more information about this error, try `rustc --explain E0382`.
Original diagnostics will follow.
warning: redundant clone
--> redundant-clone/src/lib.rs:3:20
|
3 | let foo2 = foo1.clone();
| ^^^^^^^^ help: remove this
|
note: cloned value is neither consumed nor mutated
--> redundant-clone/src/lib.rs:3:16
|
3 | let foo2 = foo1.clone();
| ^^^^^^^^^^^^
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_clone
= note: `#[warn(clippy::redundant_clone)]` on by default
warning: `redundant-clone` (lib test) generated 1 warning (run `cargo clippy --fix --lib -p redundant-clone --tests` to apply 1 suggestion)
warning: `redundant-clone` (lib) generated 1 warning (1 duplicate)
Finished dev [unoptimized + debuginfo] target(s) in 0.13s |
- Fix clippy `needless_lifetimes` - Fix `redundant_clone` false positive, see rust-lang/rust-clippy#10940
- Fix clippy `needless_lifetimes` - Fix `redundant_clone` false positive, see rust-lang/rust-clippy#10940
I'm no longer seeing this on 1.71 |
Can confirm. Fixed in 1.71 |
This is not fixed. |
Summary
Clippy falsely reports a "redundant clone" warning. Removing the clone leads to a
borrow of moved value
error.This bug occurs on 1.70.0 as well as on nightly.
Reproducer
I tried this code:
I expected to see this happen: The code to run successfully and apply fixes suggested by
cargo clippy --fix
.Instead, this happened: I encountered the following error:
Version
Additional Labels
@rustbot label +I-suggestion-causes-error
The text was updated successfully, but these errors were encountered: