rust-lang / rust-lang/rust-clippy

`clone_double_ref` doesn't help if the result leads to a compiler error

Open
#7,686 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

See for example this snippet:

struct NonCopy;

fn main() {
    let copy: NonCopy = (&NonCopy).clone();
}

It doesn't compile because of the type mismatch (expected struct NonCopy, found &NonCopy) and the lint isn't shown even though the clone of the double ref is what causes the issue.

In this small example, it's trivial to spot the error, however, in real code, the error may be caused by something less obvious and a lot harder to debug without knowing that the reference was cloned. For example, I've found a similar problem in generic async code where lifetime mismatch error was very confusing.

Is it possible to run this lint even if the compilation failed?

@rustbot label +C-enhancement

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 reproducing the provided clone_double_ref example and inspect how this lint is triggered when type checking fails. Trace the lint's compiler integration to determine whether diagnostics can still be emitted after the compilation error, then add coverage for the example and verify that the lint appears alongside the type error.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.