rust-lang / rust-lang/rust-clippy
Add `result_as_ref_deref` lint
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
This is analogous to already stable lint option_as_ref_deref. It should suggest using Result::as_deref[_mut] instead of Result::as_ref followed by Result::map(fn preforming deref).
I was quite surprised that this wasn't already implemented. I thought that option_as_ref_deref covered generic pattern of as_ref().map(Deref::deref) and was not limited only to Option type. Adding this lint would not really be adding a new "feature", and rather it would close a gap in current "implementation".
Advantage
Advantages are the same as with option_as_ref_deref lint.
Drawbacks
Other than adding a new lint, I think than naming is quite unfortunate. I personally would prefer one lint (for example called manual_as_deref) which would check both Option and Result, rather than two separate ones. However it is probably to late for that, since extending current lint to Result would be really misleading as lint's name refers to Option. And changing lint name would be to breaking.
Example
let x: Result<String, !> = todo!();
let y: &str = x.as_ref().map(String::as_str).unwrap_or("foo");
Could be written as:
let x: Result<String, !> = todo!();
let y: &str = x.as_deref().unwrap_or("foo");
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 the stable option_as_ref_deref lint and its existing tests, then compare the Result::as_ref().map(deref) pattern with the requested Result::as_deref[_mut] suggestion. Done means the analogous Result lint is implemented and tests cover the example and mutable variant.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- devtools
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100