Skip to content

clone-on-ref-ptr suggested path does not work #16016

@matthiaskrgr

Description

@matthiaskrgr

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

Metadata

Metadata

Assignees

Labels

C-bugCategory: Clippy is not doing the correct thingI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions