rust-lang / rust-lang/rust-clippy

Extend `clone_on_ref_ptr` to also work for `bytes::Bytes`

Open
#10,657 1 comment 0 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

The clone_on_ref_ptr lint checks for the usage of .clone() on a reference counted pointer, preferring a user call clone via unified function syntax, e.g. Rc::clone(...). The idea being it's easier to tell that you're incrementing a reference count, and not cloning data, by using a syntax like Rc::clone(...) instead of just .clone().

It would be nice if we could extend this lint, or define a new one, to also check the bytes::Bytes type, since Bytes is essentially a reference counted chunk of memory.

Lint Name

No response

Category

restriction

Advantage
  • Makes it easier to understand at a glance that you're not duplicating a chunk of bytes, instead just incrementing a reference count.
Drawbacks
  • Extending clippy to cover non-std types, could open a can of worms? I'm not sure what the prior art is here.
Example
use bytes::Bytes;

let b = Bytes::from(vec![1, 2, 3, 4]);
let b1 = b.clone();

Could be written as:

use bytes::Bytes;

let b = Bytes::from(vec![1, 2, 3, 4]);
let b1 = Bytes::clone(b);

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 reading the existing clone_on_ref_ptr lint and reviewing how bytes::Bytes implements cloning. Determine whether the existing lint should be extended or a new lint defined, then verify the chosen behavior with coverage for the example. Done means Bytes::clone(...) is recommended instead of .clone() for bytes::Bytes.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.