@@ -56,6 +56,7 @@ mod uninit_assumed_init;
5656mod unnecessary_filter_map;
5757mod unnecessary_fold;
5858mod unnecessary_lazy_eval;
59+ mod unwrap_or_else_default;
5960mod unwrap_used;
6061mod useless_asref;
6162mod utils;
@@ -310,6 +311,31 @@ declare_clippy_lint! {
310311 "using `ok().expect()`, which gives worse error messages than calling `expect` directly on the Result"
311312}
312313
314+ declare_clippy_lint ! {
315+ /// ### What it does
316+ /// Checks for usages of `_.unwrap_or_else(Default::default)` on Option and
317+ /// Result values.
318+ ///
319+ /// ### Why is this bad?
320+ /// Readability, these can be written as `option.unwrap_or_default` or
321+ /// `result.unwrap_or_default`.
322+ ///
323+ /// ### Examples
324+ /// ```rust
325+ /// # let x = Some(1);
326+ ///
327+ /// // Bad
328+ /// x.unwrap_or_else(Default::default);
329+ /// x.unwrap_or_else(u32::default);
330+ ///
331+ /// // Good
332+ /// x.unwrap_or_default();
333+ /// ```
334+ pub UNWRAP_OR_ELSE_DEFAULT ,
335+ style,
336+ "using `.unwrap_or_else(Default::default)`, which is more succinctly expressed as `.unwrap_or_default()`"
337+ }
338+
313339declare_clippy_lint ! {
314340 /// ### What it does
315341 /// Checks for usage of `option.map(_).unwrap_or(_)` or `option.map(_).unwrap_or_else(_)` or
@@ -1766,6 +1792,7 @@ impl_lint_pass!(Methods => [
17661792 SHOULD_IMPLEMENT_TRAIT ,
17671793 WRONG_SELF_CONVENTION ,
17681794 OK_EXPECT ,
1795+ UNWRAP_OR_ELSE_DEFAULT ,
17691796 MAP_UNWRAP_OR ,
17701797 RESULT_MAP_OR_INTO_OPTION ,
17711798 OPTION_MAP_OR_NONE ,
@@ -2172,7 +2199,10 @@ fn check_methods<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, msrv: Optio
21722199 } ,
21732200 ( "unwrap_or_else" , [ u_arg] ) => match method_call ! ( recv) {
21742201 Some ( ( "map" , [ recv, map_arg] , _) ) if map_unwrap_or:: check ( cx, expr, recv, map_arg, u_arg, msrv) => { } ,
2175- _ => unnecessary_lazy_eval:: check ( cx, expr, recv, u_arg, "unwrap_or" ) ,
2202+ _ => {
2203+ unwrap_or_else_default:: check ( cx, expr, recv, u_arg) ;
2204+ unnecessary_lazy_eval:: check ( cx, expr, recv, u_arg, "unwrap_or" ) ;
2205+ } ,
21762206 } ,
21772207 _ => { } ,
21782208 }
0 commit comments