rust-lang / rust-lang/rust-clippy

Lint againt the use of `ref`

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

Nobody has claimed this yet.

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

Description

The ref keyword seems to be causing some confusion
https://users.rust-lang.org/t/ref-keyword-versus/18818/26
https://github.com/rust-lang/rust-by-example/issues/390
https://www.reddit.com/r/rust/comments/2tq33x/ref_keyword/

There was also some effort in removing it from compiler suggestions (rust-lang/rust#52423).

As far as I know, you can replace any use of it, by borrowing the variable, that it is matched against.

For example

if let Some(ref x) = Some("Hello") {
    println!("{}", x);
}
if let Some(x) = &Some("Hello") {
    println!("{}", x);
}

or

match Some("Hello") {
    Some(ref x) =>  {
        println!("{}", x);
   }
   None => ()
}
match &Some("Hello") {
    Some(x) =>  {
        println!("{}", x);
   }
   None => ()
}
Some real world uses

https://github.com/TedDriggs/darling/blob/3c3d9a8224a164a5b8604a784381849caa32f034/core/src/error/kind.rs#L63
https://github.com/TedDriggs/darling/blob/b16d131a011f8b1f6cf99113d78edd4b119b5d97/core/src/codegen/variant_data.rs#L72
https://github.com/rust-random/rand/blob/249ebfc4352d8f3e1ebcb2a884f047b31ba32981/rand_distr/src/gamma.rs#L156


Edit(2020-04-11):

Relevant discussion on the rust internals forum:

https://internals.rust-lang.org/t/is-ref-more-powerful-than-match-ergonomics/12111

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 reviewing the linked Rust Book page, forum discussions, and compiler-suggestions issue to understand the proposed lint and its edge cases. Inspect the three real-world Rust examples in darling and rand, then define the supported scope and verify that the lint gives useful results without rejecting necessary uses. Done means the behavior and policy are agreed upon and covered by appropriate lint tests.

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
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.