rust-lang / rust-lang/rust-clippy

Lint against Arc<impl Copy>

Open
#13,237 3 comments 1 reaction 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

There is very little reason to wrap a Copy type in Rc/Arc: just copying the value directly is usually faster and certainly more ergonomic. Additionally, it's a decently common beginner mistake to try to do Arc::new(&value), which of course doesn't work as wanted. This lint would catch that as well as a side effect.

I don't know much about designing lints, but I think what should be linted against is calls of Arc::new with a Copy parameter, and also explicit mentions of Arc<T> inside structs and variables and the like. The latter ought to catch most instances where Arcs are created via for example Into or Default.

It might also be good to warn against Arc<&mut T> even though it is not Copy, because it's useless.

Of course, everything said here also applies to Rc.

Advantage
  • Slight performance and/or ergonomics improvement
  • Avoids a common beginner mistake
Drawbacks

There may be rare false positives where an Arc is actually desirable. One such case is large arrays: here we could either apply pass-by-value-size-limit or some similar parameter, or just leave people to #[allow] it in those rare cases. Another case is Arc<()> where it's used purely for the count, but this is vanishingly rare and unidiomatic.

Example
#[derive(Clone, Default)]
struct Wrapper {
    value: Arc<u64>
}

let thing = Arc::new(&thing);

Could be written as:

#[derive(Clone, Default)]
struct Wrapper {
    value: u64
}

let thing = Arc::new(thing);

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

The issue provides no source files, tests, or entry points; begin by using its Arc and Rc examples to identify the lint scope. Done means deciding and implementing diagnostics for Copy values and possibly Arc<&mut T>, including false-positive handling, with coverage for the proposed cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.