rust-lang / rust-lang/rust-clippy

Suggest .as_ref() and .as_mut() instead of Some(ref ...) and Some(ref mut ...) patterns

Open
#8,900 0 comments 2 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

Suggests using .as_ref() and .as_mut() on Option to avoid using ref and ref mut in patterns.

Lint Name

useless_ref

Category

style, pedantic

Advantage

ref and ref mut patterns are rarely used comapred to as_ref(), so code using it is hard to read, especially for new contributors who never encountered these constructions.

Drawbacks

.as_ref() is slightly longer than ref

Example
let Some(ref x) = y;
let Some(ref mut x) = y;

Could be written as:

let Some(x) = y.as_ref();
let Some(x) = y.as_mut();

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 locating the Clippy implementation for the useless_ref lint and examining comparable pattern-rewriting lints. Use the issue’s Some(ref ...) and Some(ref mut ...) examples to define the cases that should receive suggestions, then add or update coverage so the lint recommends .as_ref() and .as_mut() without changing unrelated patterns.

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
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.