@@ -41,6 +41,7 @@ mod map_collect_result_unit;
4141mod map_flatten;
4242mod map_identity;
4343mod map_unwrap_or;
44+ mod needless_option_as_deref;
4445mod ok_expect;
4546mod option_as_ref_deref;
4647mod option_map_or_none;
@@ -2106,6 +2107,30 @@ declare_clippy_lint! {
21062107 "using `.collect::<Vec<String>>().join(\" \" )` on an iterator"
21072108}
21082109
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+
21092134pub struct Methods {
21102135 avoid_breaking_exported_api : bool ,
21112136 msrv : Option < RustcVersion > ,
@@ -2193,6 +2218,7 @@ impl_lint_pass!(Methods => [
21932218 UNNECESSARY_TO_OWNED ,
21942219 UNNECESSARY_JOIN ,
21952220 ERR_EXPECT ,
2221+ NEEDLESS_OPTION_AS_DEREF ,
21962222] ) ;
21972223
21982224/// 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
24252451 unnecessary_lazy_eval:: check ( cx, expr, recv, arg, "and" ) ;
24262452 }
24272453 } ,
2454+ ( "as_deref" | "as_deref_mut" , [ ] ) => {
2455+ needless_option_as_deref:: check ( cx, expr, recv, name) ;
2456+ } ,
24282457 ( "as_mut" , [ ] ) => useless_asref:: check ( cx, expr, "as_mut" , recv) ,
24292458 ( "as_ref" , [ ] ) => useless_asref:: check ( cx, expr, "as_ref" , recv) ,
24302459 ( "assume_init" , [ ] ) => uninit_assumed_init:: check ( cx, expr, recv) ,
0 commit comments