Skip to content

Add result_as_ref_deref lint #13342

@aleksanderkrauze

Description

@aleksanderkrauze

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");

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintArea: New lints

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions