rust-lang / rust-lang/rust-clippy

Implicit Copy Lint

Open
#9,061 3 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-lint
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

What it does

An allow-by-default lint to highlight implicit uses of the Copy trait, which has relevance in some kinds of unsafe code. Using it in deny mode would force the programmer to write .clone() everywhere they want to copy a value.

Lint Name

implicit_copy

Category

correctness

Advantage

Among other things, I suggested it in UnsafeCell should implement the Copy trait #25053 , as a potential solution. There are other cases in unsafe code.

Drawbacks

It will almost always be on allow; the use cases are very rare.

Example

Given a hypothetical Copy impl for UnsafeCell:

unsafe fn footgun(cell: UnsafeCell<u32>) {
  *cell.get() += 2
}

fn ouch_i_shot_my_foot() {
  let x = UnsafeCell::new(2);
  unsafe { footgun(x); }
  assert_eq(unsafe { *x.get() }, 4); // kaboom 
}

An example of how this would help:

// user code
unsafe fn footgun(cell: UnsafeCell<u32>) {
  *cell.get() += 2
}

#[deny(implicit_copy)]
fn ouch_i_shot_my_foot() {
  let x = UnsafeCell::new(2);
  unsafe { footgun(x); } // lint complains here!
  // programmer now has a strong hint that footgun should take an &UnsafeCell instead
  assert_eq(unsafe { *x.get() }, 4); // no kaboom 
}

// meanwhile this is possible
#[derive(Copy, Clone)]
struct MyCleverThing(UnsafeCell<u64>);

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.

Research direction

Start by reviewing existing Rust Clippy lint implementations and how correctness lints are registered and tested. Define the behavior from the issue's examples: detect implicit Copy uses, support the implicit_copy name and correctness category, and verify the deny-mode diagnostics without changing explicit clone behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools, tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.