Skip to content

clone_on_copy gives wrong suggestion, causes error #15610

@teofr

Description

@teofr

Summary

clone_on_copy checks if a type implements copy disregarding the lifetimes, causing it to fail in certain cases


From what I could see this problem is not only related to clone_on_copy, but to every lint that uses the is_copy function, since it doesn't take into account regions. It also seems to be related to #15577

I'm not sure if there's any reason to not consider lifetimes when checking trait implementations, but if it's a decision across clippy it may affect more lints.

Lint Name

clone_on_copy

Reproducer

I tried this code:

#![no_main]

struct Weird<'a>(&'a i32);

impl Clone for Weird<'_> {
    fn clone(&self) -> Self {
        Weird(self.0)
    }
}

impl Copy for Weird<'static> {}

impl Weird<'_> {
    fn foo(&self) -> Self {
        self.clone()
    }
}

I saw this happen:

warning: using `clone` on type `Weird<'_>` which implements the `Copy` trait
  --> test3.rs:15:9
   |
15 |         self.clone()
   |         ^^^^^^^^^^^^ help: try dereferencing it: `*self`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#clone_on_copy
   = note: `#[warn(clippy::clone_on_copy)]` on by default

After applying the fixes compilation fails:

error: lifetime may not live long enough
  --> test3.rs:15:9
   |
14 |     fn foo(&self) -> Self {
   |            ----- has type `&Weird<'1>`
15 |         *self
   |         ^^^^^ copying this value requires that `'1` must outlive `'static`

error: aborting due to 1 previous error

I expected to see this happen:

No lint

Version

rustc 1.91.0-nightly (6ba0ce409 2025-08-21)
binary: rustc
commit-hash: 6ba0ce40941eee1ca02e9ba49c791ada5158747a
commit-date: 2025-08-21
host: aarch64-apple-darwin
release: 1.91.0-nightly
LLVM version: 21.1.0

Additional Labels

@rustbot label +I-suggestion-causes-error

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingE-hardCall for participation: This a hard problem and requires more experience or effort to work onI-false-positiveIssue: The lint was triggered on code it shouldn't haveI-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