rust-lang / rust-lang/rust-clippy

`clone_on_copy` gives wrong suggestion, causes error

Open
#15,610 8 comments 0 reactions 1 assignee View on GitHub

@arunsingh is already working on this.

Since Jun 6, 2026.

C-bug E-hard I-false-positive I-suggestion-causes-error
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.