rust-lang / rust-lang/rust-clippy
clone-on-ref-ptr suggested path does not work
Open
@profetia is already working on this.
Since Nov 2, 2025.
C-bug
I-suggestion-causes-error
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
Using the following flags
--force-warn clippy::clone-on-ref-ptr
this code:
fn forget<T>(val: T) {
use std::cell::RefCell;
use std::rc::Rc;
struct Foo<T>(T, RefCell<Option<Rc<Foo<T>>>>);
let x = Rc::new(Foo(val, RefCell::new(None)));
*x.1.borrow_mut() = Some(x.clone());
}
struct DontDropMe;
impl Drop for DontDropMe {
fn drop(&mut self) { unreachable!() }
}
fn main() {
forget(DontDropMe)
}
caused the following diagnostics:
Checking _d08ddf1ce5e901e82ebe070352639d815c40fdfe v0.1.0 (/tmp/icemaker_global_tempdir.tMrpGeJ6WWUI/icemaker_clippyfix_tempdir.KFb9lwGSgQS7/_d08ddf1ce5e901e82ebe070352639d815c40fdfe)
warning: using `.clone()` on a ref-counted pointer
--> src/main.rs:6:30
|
6 | *x.1.borrow_mut() = Some(x.clone());
| ^^^^^^^^^ help: try: `std::rc::Rc::<forget::Foo<T>>::clone(&x)`
|
= 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: `_d08ddf1ce5e901e82ebe070352639d815c40fdfe` (bin "_d08ddf1ce5e901e82ebe070352639d815c40fdfe") generated 1 warning
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.25s
However after applying these diagnostics, the resulting code:
fn forget<T>(val: T) {
use std::cell::RefCell;
use std::rc::Rc;
struct Foo<T>(T, RefCell<Option<Rc<Foo<T>>>>);
let x = Rc::new(Foo(val, RefCell::new(None)));
*x.1.borrow_mut() = Some(std::rc::Rc::<forget::Foo<T>>::clone(&x));
}
struct DontDropMe;
impl Drop for DontDropMe {
fn drop(&mut self) { unreachable!() }
}
fn main() {
forget(DontDropMe)
}
no longer compiled:
Checking _d08ddf1ce5e901e82ebe070352639d815c40fdfe v0.1.0 (/tmp/icemaker_global_tempdir.tMrpGeJ6WWUI/icemaker_clippyfix_tempdir.KFb9lwGSgQS7/_d08ddf1ce5e901e82ebe070352639d815c40fdfe)
error[E0433]: failed to resolve: function `forget` is not a crate or module
--> src/main.rs:6:44
|
6 | *x.1.borrow_mut() = Some(std::rc::Rc::<forget::Foo<T>>::clone(&x));
| ^^^^^^ function `forget` is not a crate or module
For more information about this error, try `rustc --explain E0433`.
error: could not compile `_d08ddf1ce5e901e82ebe070352639d815c40fdfe` (bin "_d08ddf1ce5e901e82ebe070352639d815c40fdfe") due to 1 previous error
warning: build failed, waiting for other jobs to finish...
error: could not compile `_d08ddf1ce5e901e82ebe070352639d815c40fdfe` (bin "_d08ddf1ce5e901e82ebe070352639d815c40fdfe" test) due to 1 previous error
Version:
rustc 1.93.0-nightly (6a884ad1b 2025-11-02)
binary: rustc
commit-hash: 6a884ad1b502fe48307d363858510702429fc735
commit-date: 2025-11-02
host: x86_64-unknown-linux-gnu
release: 1.93.0-nightly
LLVM version: 21.1.3
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.