Skip to content

clone-on-ref-ptr could suggest std::sync::Arc when Arc is not in scope #15258

@matthiaskrgr

Description

@matthiaskrgr

Using the following flags

--force-warn clippy::clone-on-ref-ptr

this code:

//@ run-pass #![allow(unused_must_use)] #![allow(deprecated)] //@ needs-threads //@ needs-subprocess use std::{env, fmt, process, sync, thread}; struct SlowFmt(u32); impl fmt::Debug for SlowFmt { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { thread::sleep_ms(3); self.0.fmt(f) } } fn do_print(x: u32) { let x = SlowFmt(x); println!("{:?}{:?}{:?}{:?}{:?}", x, x, x, x, x); } fn main(){ if env::args().count() == 2 { let barrier = sync::Arc::new(sync::Barrier::new(2)); let tbarrier = barrier.clone(); let t = thread::spawn(move || { tbarrier.wait(); do_print(1); }); barrier.wait(); do_print(2); t.join(); } else { let this = env::args().next().unwrap(); let output = process::Command::new(this).arg("-").output().unwrap(); for line in String::from_utf8(output.stdout).unwrap().lines() { match line.chars().next().unwrap() { '1' => assert_eq!(line, "11111"), '2' => assert_eq!(line, "22222"), chr => panic!("unexpected character {:?}", chr) } } } }

caused the following diagnostics:

 Checking _atomic-print v0.1.0 (/tmp/icemaker_global_tempdir.ROAms2Xd0g8l/icemaker_clippyfix_tempdir.UiIt7ujO1kHB/_atomic-print) warning: using `.clone()` on a ref-counted pointer --> src/main.rs:26:24 | 26 | let tbarrier = barrier.clone(); | ^^^^^^^^^^^^^^^ help: try: `Arc::<std::sync::Barrier>::clone(&barrier)` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#clone_on_ref_ptr = note: requested on the command line with `--force-warn clippy::clone-on-ref-ptr` warning: `_atomic-print` (bin "_atomic-print") generated 1 warning Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.49s 

However after applying these diagnostics, the resulting code:

//@ run-pass #![allow(unused_must_use)] #![allow(deprecated)] //@ needs-threads //@ needs-subprocess use std::{env, fmt, process, sync, thread}; struct SlowFmt(u32); impl fmt::Debug for SlowFmt { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { thread::sleep_ms(3); self.0.fmt(f) } } fn do_print(x: u32) { let x = SlowFmt(x); println!("{:?}{:?}{:?}{:?}{:?}", x, x, x, x, x); } fn main(){ if env::args().count() == 2 { let barrier = sync::Arc::new(sync::Barrier::new(2)); let tbarrier = Arc::<std::sync::Barrier>::clone(&barrier); let t = thread::spawn(move || { tbarrier.wait(); do_print(1); }); barrier.wait(); do_print(2); t.join(); } else { let this = env::args().next().unwrap(); let output = process::Command::new(this).arg("-").output().unwrap(); for line in String::from_utf8(output.stdout).unwrap().lines() { match line.chars().next().unwrap() { '1' => assert_eq!(line, "11111"), '2' => assert_eq!(line, "22222"), chr => panic!("unexpected character {:?}", chr) } } } }

no longer compiled:

 Checking _atomic-print v0.1.0 (/tmp/icemaker_global_tempdir.ROAms2Xd0g8l/icemaker_clippyfix_tempdir.UiIt7ujO1kHB/_atomic-print) warning: error applying suggestions to `src/main.rs` The full error message was: > cannot replace slice of data that was already replaced 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-clippy/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 warning: failed to automatically apply fixes suggested by rustc to crate `_atomic_print` 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-clippy/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[E0433]: failed to resolve: use of undeclared type `Arc` --> src/main.rs:26:24 | 26 | let tbarrier = Arc::<std::sync::Barrier>::clone(&barrier); | ^^^ use of undeclared type `Arc` | help: consider importing one of these structs | 8 + use crate::sync::Arc; | 8 + use std::sync::Arc; | error: aborting due to 1 previous error For more information about this error, try `rustc --explain E0433`. Original diagnostics will follow. error[E0433]: failed to resolve: use of undeclared type `Arc` --> src/main.rs:26:24 | 26 | let tbarrier = Arc::<std::sync::Barrier>::clone(&barrier); | ^^^ use of undeclared type `Arc` | help: consider importing one of these structs | 8 + use crate::sync::Arc; | 8 + use std::sync::Arc; | For more information about this error, try `rustc --explain E0433`. error: could not compile `_atomic-print` (bin "_atomic-print" test) due to 1 previous error warning: build failed, waiting for other jobs to finish... warning: error applying suggestions to `src/main.rs` The full error message was: > cannot replace slice of data that was already replaced 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-clippy/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 warning: failed to automatically apply fixes suggested by rustc to crate `_atomic_print` 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-clippy/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[E0433]: failed to resolve: use of undeclared type `Arc` --> src/main.rs:26:24 | 26 | let tbarrier = Arc::<std::sync::Barrier>::clone(&barrier); | ^^^ use of undeclared type `Arc` | help: consider importing one of these structs | 8 + use crate::sync::Arc; | 8 + use std::sync::Arc; | error: aborting due to 1 previous error For more information about this error, try `rustc --explain E0433`. Original diagnostics will follow. error: could not compile `_atomic-print` (bin "_atomic-print") due to 1 previous error 

Version:

rustc 1.90.0-nightly (855e0fe46 2025-07-11) binary: rustc commit-hash: 855e0fe46e68d94e9f6147531b75ac2d488c548e commit-date: 2025-07-11 host: x86_64-unknown-linux-gnu release: 1.90.0-nightly LLVM version: 20.1.7 

Metadata

Metadata

Assignees

Labels

L-suggestionLint: Improving, adding or fixing lint suggestions

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions