Closed
Description
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 :)