@@ -40,6 +40,7 @@ mod map_collect_result_unit;
40
40
mod map_flatten;
41
41
mod map_identity;
42
42
mod map_unwrap_or;
43
+ mod needless_option_as_deref;
43
44
mod ok_expect;
44
45
mod option_as_ref_deref;
45
46
mod option_map_or_none;
@@ -2079,6 +2080,30 @@ declare_clippy_lint! {
2079
2080
"using `.collect::<Vec<String>>().join(\" \" )` on an iterator"
2080
2081
}
2081
2082
2083
+ declare_clippy_lint ! {
2084
+ /// ### What it does
2085
+ /// Checks for no-op uses of Option::{as_deref,as_deref_mut},
2086
+ /// for example, `Option<&T>::as_deref()` returns the same type.
2087
+ ///
2088
+ /// ### Why is this bad?
2089
+ /// Redundant code and improving readability.
2090
+ ///
2091
+ /// ### Example
2092
+ /// ```rust
2093
+ /// let a = Some(&1);
2094
+ /// let b = a.as_deref(); // goes from Option<&i32> to Option<&i32>
2095
+ /// ```
2096
+ /// Could be written as:
2097
+ /// ```rust
2098
+ /// let a = Some(&1);
2099
+ /// let b = a;
2100
+ /// ```
2101
+ #[ clippy:: version = "1.57.0" ]
2102
+ pub NEEDLESS_OPTION_AS_DEREF ,
2103
+ complexity,
2104
+ "no-op use of `deref` or `deref_mut` method to `Option`."
2105
+ }
2106
+
2082
2107
pub struct Methods {
2083
2108
avoid_breaking_exported_api : bool ,
2084
2109
msrv : Option < RustcVersion > ,
@@ -2165,6 +2190,7 @@ impl_lint_pass!(Methods => [
2165
2190
NEEDLESS_SPLITN ,
2166
2191
UNNECESSARY_TO_OWNED ,
2167
2192
UNNECESSARY_JOIN ,
2193
+ NEEDLESS_OPTION_AS_DEREF ,
2168
2194
] ) ;
2169
2195
2170
2196
/// Extracts a method call name, args, and `Span` of the method name.
@@ -2397,6 +2423,9 @@ fn check_methods<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, msrv: Optio
2397
2423
unnecessary_lazy_eval:: check ( cx, expr, recv, arg, "and" ) ;
2398
2424
}
2399
2425
} ,
2426
+ ( "as_deref" | "as_deref_mut" , [ ] ) => {
2427
+ needless_option_as_deref:: check ( cx, expr, recv, name) ;
2428
+ } ,
2400
2429
( "as_mut" , [ ] ) => useless_asref:: check ( cx, expr, "as_mut" , recv) ,
2401
2430
( "as_ref" , [ ] ) => useless_asref:: check ( cx, expr, "as_ref" , recv) ,
2402
2431
( "assume_init" , [ ] ) => uninit_assumed_init:: check ( cx, expr, recv) ,
0 commit comments