Skip to content

Implicit Copy Lint #9061

Open
Open
@Kile-Asmussen

Description

@Kile-Asmussen

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>);

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintArea: New lints

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions