@@ -41,6 +41,7 @@ mod map_collect_result_unit;
41
41
mod map_flatten;
42
42
mod map_identity;
43
43
mod map_unwrap_or;
44
+ mod needless_option_as_deref;
44
45
mod ok_expect;
45
46
mod option_as_ref_deref;
46
47
mod option_map_or_none;
@@ -2106,6 +2107,30 @@ declare_clippy_lint! {
2106
2107
"using `.collect::<Vec<String>>().join(\" \" )` on an iterator"
2107
2108
}
2108
2109
2110
+ declare_clippy_lint ! {
2111
+ /// ### What it does
2112
+ /// Checks for no-op uses of `Option::{as_deref, as_deref_mut}`,
2113
+ /// for example, `Option<&T>::as_deref()` returns the same type.
2114
+ ///
2115
+ /// ### Why is this bad?
2116
+ /// Redundant code and improving readability.
2117
+ ///
2118
+ /// ### Example
2119
+ /// ```rust
2120
+ /// let a = Some(&1);
2121
+ /// let b = a.as_deref(); // goes from Option<&i32> to Option<&i32>
2122
+ /// ```
2123
+ /// Could be written as:
2124
+ /// ```rust
2125
+ /// let a = Some(&1);
2126
+ /// let b = a;
2127
+ /// ```
2128
+ #[ clippy:: version = "1.57.0" ]
2129
+ pub NEEDLESS_OPTION_AS_DEREF ,
2130
+ complexity,
2131
+ "no-op use of `deref` or `deref_mut` method to `Option`."
2132
+ }
2133
+
2109
2134
pub struct Methods {
2110
2135
avoid_breaking_exported_api : bool ,
2111
2136
msrv : Option < RustcVersion > ,
@@ -2193,6 +2218,7 @@ impl_lint_pass!(Methods => [
2193
2218
UNNECESSARY_TO_OWNED ,
2194
2219
UNNECESSARY_JOIN ,
2195
2220
ERR_EXPECT ,
2221
+ NEEDLESS_OPTION_AS_DEREF ,
2196
2222
] ) ;
2197
2223
2198
2224
/// Extracts a method call name, args, and `Span` of the method name.
@@ -2425,6 +2451,9 @@ fn check_methods<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, msrv: Optio
2425
2451
unnecessary_lazy_eval:: check ( cx, expr, recv, arg, "and" ) ;
2426
2452
}
2427
2453
} ,
2454
+ ( "as_deref" | "as_deref_mut" , [ ] ) => {
2455
+ needless_option_as_deref:: check ( cx, expr, recv, name) ;
2456
+ } ,
2428
2457
( "as_mut" , [ ] ) => useless_asref:: check ( cx, expr, "as_mut" , recv) ,
2429
2458
( "as_ref" , [ ] ) => useless_asref:: check ( cx, expr, "as_ref" , recv) ,
2430
2459
( "assume_init" , [ ] ) => uninit_assumed_init:: check ( cx, expr, recv) ,
0 commit comments