Open glittershark opened 1 year ago
Narrowing down the range, I don't think this triggered as recently as rustc 1.69.0-nightly (13471d3b2 2023-03-02)
. At least, that seems to be the case for my similar issue at https://github.com/martinvonz/jj/pull/1465.
To help narrow down the range — the issue started sometime between 2023-03-16 and 2023-03-18. The last successful build for me was on 16 March 2023, and the first failed one was on 18 March.
FWIW I bumped into this too with this code in zbus.
error: redundant clone
--> zbus_macros/src/error.rs:239:26
|
239 | in_fields.clone()
| ^^^^^^^^ help: remove this
|
note: cloned value is neither consumed nor mutated
--> zbus_macros/src/error.rs:239:17
|
239 | in_fields.clone()
| ^^^^^^^^^^^^^^^^^
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_clone
= note: `-D clippy::redundant-clone` implied by `-D warnings`
We are hitting this as well on the https://github.com/solana-labs/solana repo when trying to upgrade our nightly version to 2023-04-15. (Our nightly is currently on 2023-03-04.)
hey, i created minimal test case:
use std::sync::Arc;
#[derive(Clone)]
struct Account {
data: Arc<usize>,
}
fn take<T>(_a: &T) {
}
fn main() {
let account = Account {data: Arc::new(30)};
let data = account.data.clone();
take(&account);
take(&data);
}
$ cargo +nightly-2023-03-16-x86_64-unknown-linux-gnu clippy --fix --allow-dirty -- --deny clippy::redundant_clone
Checking clippy-redundant-clone v0.1.0 (/home/sol/work/clippy-redundant-clone)
Finished dev [unoptimized + debuginfo] target(s) in 0.26s
$ cargo +nightly-2023-03-17-x86_64-unknown-linux-gnu clippy --fix --allow-dirty -- --deny clippy::redundant_clone
Checking clippy-redundant-clone v0.1.0 (/home/sol/work/clippy-redundant-clone)
warning: failed to automatically apply fixes suggested by rustc to crate `clippy_redundant_clone`
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]: borrow of partially moved value: `account`
--> src/main.rs:14:10
|
13 | let data = account.data;
| ------------ value partially moved here
14 | take(&account);
| ^^^^^^^^ value borrowed here after partial move
|
= note: partial move occurs because `account.data` has type `std::sync::Arc<usize>`, which does not implement the `Copy` trait
error: aborting due to previous error
For more information about this error, try `rustc --explain E0382`.
Original diagnostics will follow.
warning: redundant clone
--> src/main.rs:13:28
|
13 | let data = account.data.clone();
| ^^^^^^^^ help: remove this
|
note: cloned value is neither consumed nor mutated
--> src/main.rs:13:16
|
13 | let data = account.data.clone();
| ^^^^^^^^^^^^^^^^^^^^
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_clone
= note: requested on the command line with `-D clippy::redundant-clone`
warning: `clippy-redundant-clone` (bin "clippy-redundant-clone") generated 1 warning (run `cargo clippy --fix --bin "clippy-redundant-clone"` to apply 1 suggestion)
warning: `clippy-redundant-clone` (bin "clippy-redundant-clone" test) generated 1 warning (1 duplicate)
Finished dev [unoptimized + debuginfo] target(s) in 0.25s
also, i further narrowed down the regression window (thank to the prior work: https://github.com/rust-lang/rust-clippy/issues/10577#issuecomment-1494685235):
so, it looks like the regression window is quite narrow: https://github.com/rust-lang/rust/compare/ab65486...511364e
this is a hunch but i suspect https://github.com/rust-lang/rust/pull/108944 is the culprit, flipping this line: https://github.com/rust-lang/rust-clippy/blob/c4909746add9b34e3f10508ca37b87f5aa96c0b8/clippy_lints/src/redundant_clone.rs#L381
It looks like this bug is now in stable 1.70, I'm getting errors in https://github.com/printfn/fend.
I'm also seeing this error, with an even simpler minimal test case than was produced above (no need for Arc
s): playground
yeah, fyi, I confirmed that the root cause is rustc's internal changes (ref: https://github.com/rust-lang/rust/pull/108944#issuecomment-1564451009). hehe, as i guessed in the post, this is getting more attention because now 1.70 is shipped. ;)
according to https://github.com/rust-lang/rust/pull/108944#issuecomment-1564493704, it seems clippy needs to be fixed for its misuse somehow...
fyi, i only bisected as a normal rust community member, currently not actively trying fix the clippy.
Another instance in indicatif: https://github.com/rust-lang/rust/issues/112227
Excerpt of original output of cargo clippy --fix
in https://github.com/console-rs/indicatif/pull/547, which suggested to create an issue here:
Checking indicatif v0.17.4 (C:\foresterre\indicatif)
warning: failed to automatically apply fixes suggested by rustc to crate `multi_autodrop`
after fixes were automatically applied the compiler reported errors within these files:
* tests\multi-autodrop.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
I tried this code (can probably be made more minimal):
use std::sync::{atomic::AtomicU32, atomic::Ordering, Arc, Mutex, RwLock};
// Reproduction derived from indicatif::MultiProgress and ProgressBar where this Clippy suggestion was found.
// With Clippy 0.1.71 (2023-06-01 d59363a)
// With Rust 1.70.0
#[derive(Clone)]
struct MultiProgress {
state: Arc<RwLock<State>>,
}
impl Default for MultiProgress {
fn default() -> Self {
Self {
state: Arc::new(RwLock::new(State)),
}
}
}
impl MultiProgress {
fn add(&self, pb: ProgressBar) -> ProgressBar {
let state = self.state.write().unwrap();
drop(state);
pb.set_remote(self.state.clone());
pb
}
}
#[derive(Clone)]
struct ProgressBar {
value: Arc<AtomicU32>,
remote: Arc<Mutex<Arc<RwLock<State>>>>,
}
impl Default for ProgressBar {
fn default() -> Self {
Self {
value: Arc::new(AtomicU32::new(0)),
remote: Arc::new(Mutex::new(Arc::new(RwLock::new(State)))),
}
}
}
impl ProgressBar {
fn set_remote(&self, state: Arc<RwLock<State>>) {
*self.remote.lock().unwrap() = state;
}
fn inc(&self, delta: u32) {
self.value.fetch_add(delta, Ordering::SeqCst);
}
fn reset(&self) {
self.value.fetch_add(0, Ordering::SeqCst);
}
}
struct State;
fn main() {
let pb = {
let m = MultiProgress::default();
m.add(ProgressBar::default())
// m is dropped here
};
{
// Clippy faults here: it suggests to remove the clone.
let pb2 = pb.clone();
for _ in 0..10 {
pb2.inc(1);
}
}
pb.reset();
}
Running cargo clippy
:
hecking playground v0.0.1 (/playground)
warning: redundant clone
--> src/main.rs:70:21
|
70 | let pb2 = pb.clone();
| ^^^^^^^^ help: remove this
|
note: cloned value is neither consumed nor mutated
--> src/main.rs:70:19
|
70 | let pb2 = pb.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: `playground` (bin "playground") generated 1 warning (run `cargo clippy --fix --bin "playground"` to apply 1 suggestion)
Finished dev [unoptimized + debuginfo] target(s) in 0.42s
When applying the suggestion:
Checking playground v0.0.1 (/playground)
error[E0382]: borrow of moved value: `pb`
--> src/main.rs:76:5
|
62 | let pb = {
| -- move occurs because `pb` has type `ProgressBar`, which does not implement the `Copy` trait
...
70 | let pb2 = pb;
| -- value moved here
...
76 | pb.reset();
| ^^^^^^^^^^ value borrowed here after move
|
help: consider cloning the value if the performance cost is acceptable
|
70 | let pb2 = pb.clone();
| ++++++++
For more information about this error, try `rustc --explain E0382`.
error: could not compile `playground` (bin "playground") due to previous error
rustc --version --verbose
:
rustc 1.70.0 (90c541806 2023-05-31)
binary: rustc
commit-hash: 90c541806f23a127002de5b4038be731ba1458ca
commit-date: 2023-05-31
host: x86_64-pc-windows-msvc
release: 1.70.0
LLVM version: 16.0.2
cargo clippy --version --verbose
:
clippy 0.1.70 (90c54180 2023-05-31)
Another set of instances is in my crate awint
as of commit https://github.com/AaronKutch/awint/commit/71b35e8f034126d416d435fd718a2a6b95e94c4a (run cargo clippy --no-default-features
), the clone isn't redundant because the original var needs to be used for a special trait method call
Summary
I looked and this didn't look like any of the existing
redundant_clone
false positives. Sometime betweennightly-2022-10-01
andnightly-2023-03-18
(sorry for the wide range)clippy::redundant_clone
started false-positive firing here. If thatclone()
call is removed, compilation fails due toindex_on
being used hereLint Name
clippy::redundant_clone
Reproducer
See links
Version
Additional Labels
No response