rust-lang / rust-lang/rust-clippy
Clippy should warn against redundant calls to `Option::as_ref` on `Option<&_>`
Nobody has claimed this yet.
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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