rust-lang / rust-lang/rust-clippy

Use `.share()` instead of `.clone()`

Open
#17,466 4 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

This lint should suggest using std::clone::Share::share over std::clone::Clone::clone where applicable.

Advantage

The purpose of the Share trait is to make the behavior of Clone more explicit. share is the exact same as clone, but expresses intent and semantics more clearly, so it would be useful to have a lint to suggest turning the former into the latter.

Drawbacks

The obvious drawback is that Share is currently experimental, so implementing a lint for it at this stage might be wasted effort, although from what I can tell it does seem rather finalized. This suggestion is mostly intended for when/if Share does stabilize.

Another drawback is that calling share requires importing std::clone::Share, which might be seen as unnecessary import bloat.

Example
let a = Rc::new(621);
let b = a.clone();

Could be written as:

let a = Rc::new(621);
let b = a.share();
Comparison with existing lints

As far as I know, there is currently no lint or suggestion(s) for one which cover this.

Additional Context

No response

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

No files, tests, or entry points are named. Start by locating Clippy's existing clone-related lints and their tests, then determine the stabilization and applicability requirements for std::clone::Share. Done means a lint can reliably suggest .share() instead of .clone() where applicable, with coverage for the example and relevant edge cases.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.