Skip to content

Lint for using Option::as_deref #4918

Closed
@jan-auer

Description

@jan-auer

With Rust 1.40.0, Option::as_deref has landed.

It would be great to have a clippy lint that detects Option::as_ref chained by Option::map<F> where F: FnOnce(&T) -> &<T as Deref>::Target. This would then automatically account for cases where AsRef::as_ref or a custom method like String::as_str are used.

By far the most compelling case is to apply it to Option<String> or Option<PathBuf>, with probably lots of code bases now containing opt.as_ref().map(String::as_str) or similar constructs.

Example in:

let opt = Some(" hello ".to_owned());
let trimmed = opt.as_ref().map(String::as_str).map(str::trim);

Example out:

let opt = Some(" hello ".to_owned());
let trimmed = opt.as_deref().map(str::trim);

There are probably more cases that could be detected, like ? chaining or match statements, but I think those will get gradually less useful to be worth the effort.

Icing on the cake would be compatibility to rustfix :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintArea: New lintsE-mediumCall for participation: Medium difficulty level problem and requires some initial experience.L-complexityLint: Belongs in the complexity lint group

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions