rust-lang / rust-lang/rust-clippy

Clippy should warn against redundant calls to `Option::as_ref` on `Option<&_>`

Open
#11,618 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

Option::as_ref is used to convert &Option<T> to Option<&T>. Clippy should warn against using this function when T is already a reference type, unless the destination type requires Option<&&T> for some reason.

Advantage

Code cleanliness and brevity. Sometimes redundant calls to as_ref can arise if you refactor a variable from being owned to being passed by reference.

Drawbacks

No response

Example
fn my_fn(value: Option<&T>) {
    value.as_ref().map(...)
}

Could be written as:

fn my_fn(value: Option<&T>) {
    value.map(...)
}

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 Clippy's existing lint entry points and tests for Option method patterns; the issue's example provides the initial case to inspect. Done means redundant as_ref calls on Option<&T> are warned about, while cases requiring Option<&&T> remain allowed.

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.