@@ -257,59 +257,40 @@ declare_clippy_lint! {
257257}
258258
259259declare_clippy_lint ! {
260- /// **What it does:** Checks for usage of `_.map(_).unwrap_or(_)`.
260+ /// **What it does:** Checks for usage of `option.map(_).unwrap_or(_)` or `option.map(_).unwrap_or_else(_)` or
261+ /// `result.map(_).unwrap_or_else(_)`.
261262 ///
262- /// **Why is this bad?** Readability, this can be written more concisely as
263- /// `_ .map_or(_, _)`.
263+ /// **Why is this bad?** Readability, these can be written more concisely (resp.) as
264+ /// `option .map_or(_, _)`, `option.map_or_else(_, _)` and `result.map_or_else (_, _)`.
264265 ///
265266 /// **Known problems:** The order of the arguments is not in execution order
266267 ///
267- /// **Example :**
268+ /// **Examples :**
268269 /// ```rust
269270 /// # let x = Some(1);
270- /// x.map(|a| a + 1).unwrap_or(0);
271- /// ```
272- pub OPTION_MAP_UNWRAP_OR ,
273- pedantic,
274- "using `Option.map(f).unwrap_or(a)`, which is more succinctly expressed as `map_or(a, f)`"
275- }
276-
277- declare_clippy_lint ! {
278- /// **What it does:** Checks for usage of `_.map(_).unwrap_or_else(_)`.
279- ///
280- /// **Why is this bad?** Readability, this can be written more concisely as
281- /// `_.map_or_else(_, _)`.
282271 ///
283- /// **Known problems:** The order of the arguments is not in execution order.
272+ /// // Bad
273+ /// x.map(|a| a + 1).unwrap_or(0);
284274 ///
285- /// **Example:**
286- /// ```rust
287- /// # let x = Some(1);
288- /// # fn some_function() -> usize { 1 }
289- /// x.map(|a| a + 1).unwrap_or_else(some_function);
275+ /// // Good
276+ /// x.map_or(0, |a| a + 1);
290277 /// ```
291- pub OPTION_MAP_UNWRAP_OR_ELSE ,
292- pedantic,
293- "using `Option.map(f).unwrap_or_else(g)`, which is more succinctly expressed as `map_or_else(g, f)`"
294- }
295-
296- declare_clippy_lint ! {
297- /// **What it does:** Checks for usage of `result.map(_).unwrap_or_else(_)`.
298278 ///
299- /// **Why is this bad?** Readability, this can be written more concisely as
300- /// `result.map_or_else(_, _)`.
301- ///
302- /// **Known problems:** None.
279+ /// // or
303280 ///
304- /// **Example:**
305281 /// ```rust
306282 /// # let x: Result<usize, ()> = Ok(1);
307283 /// # fn some_function(foo: ()) -> usize { 1 }
284+ ///
285+ /// // Bad
308286 /// x.map(|a| a + 1).unwrap_or_else(some_function);
287+ ///
288+ /// // Good
289+ /// x.map_or_else(some_function, |a| a + 1);
309290 /// ```
310- pub RESULT_MAP_UNWRAP_OR_ELSE ,
291+ pub MAP_UNWRAP ,
311292 pedantic,
312- "using `Result .map(f).unwrap_or_else(g )`, which is more succinctly expressed as `. map_or_else(g , f)`"
293+ "using `.map(f).unwrap_or(a)` or `.map(f). unwrap_or_else(func )`, which are more succinctly expressed as `map_or(a, f)` or ` map_or_else(a , f)`"
313294}
314295
315296declare_clippy_lint ! {
@@ -1294,9 +1275,7 @@ declare_lint_pass!(Methods => [
12941275 WRONG_SELF_CONVENTION ,
12951276 WRONG_PUB_SELF_CONVENTION ,
12961277 OK_EXPECT ,
1297- OPTION_MAP_UNWRAP_OR ,
1298- OPTION_MAP_UNWRAP_OR_ELSE ,
1299- RESULT_MAP_UNWRAP_OR_ELSE ,
1278+ MAP_UNWRAP ,
13001279 RESULT_MAP_OR_INTO_OPTION ,
13011280 OPTION_MAP_OR_NONE ,
13021281 OPTION_AND_THEN_SOME ,
@@ -1503,9 +1482,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Methods {
15031482 cx,
15041483 lint,
15051484 first_arg. pat. span,
1506- & format!(
1507- "methods called `{}` usually take {}; consider choosing a less \
1508- ambiguous name",
1485+ & format!( "methods called `{}` usually take {}; consider choosing a less ambiguous name" ,
15091486 conv,
15101487 & self_kinds
15111488 . iter( )
@@ -1678,7 +1655,7 @@ fn lint_or_fun_call<'a, 'tcx>(
16781655 let self_ty = cx. tables. expr_ty( self_expr) ;
16791656
16801657 if let Some ( & ( _, fn_has_arguments, poss, suffix) ) =
1681- know_types. iter( ) . find( |&&i| match_type( cx, self_ty, i. 0 ) ) ;
1658+ know_types. iter( ) . find( |&&i| match_type( cx, self_ty, i. 0 ) ) ;
16821659
16831660 if poss. contains( & name) ;
16841661
@@ -1931,7 +1908,7 @@ fn lint_clone_on_copy(cx: &LateContext<'_, '_>, expr: &hir::Expr<'_>, arg: &hir:
19311908 CLONE_DOUBLE_REF ,
19321909 expr. span ,
19331910 "using `clone` on a double-reference; \
1934- this will copy the reference instead of cloning the inner type",
1911+ this will copy the reference instead of cloning the inner type",
19351912 |diag| {
19361913 if let Some ( snip) = sugg:: Sugg :: hir_opt ( cx, arg) {
19371914 let mut ty = innermost;
@@ -2121,7 +2098,7 @@ fn lint_iter_cloned_collect<'a, 'tcx>(
21212098 ITER_CLONED_COLLECT ,
21222099 to_replace,
21232100 "called `iter().cloned().collect()` on a slice to create a `Vec`. Calling `to_vec()` is both faster and \
2124- more readable",
2101+ more readable",
21252102 "try" ,
21262103 ".to_vec()" . to_string( ) ,
21272104 Applicability :: MachineApplicable ,
@@ -2436,7 +2413,7 @@ fn lint_unwrap(cx: &LateContext<'_, '_>, expr: &hir::Expr<'_>, unwrap_args: &[hi
24362413 None ,
24372414 & format ! (
24382415 "if you don't want to handle the `{}` case gracefully, consider \
2439- using `expect()` to provide a better panic message",
2416+ using `expect()` to provide a better panic message",
24402417 none_value,
24412418 ) ,
24422419 ) ;
@@ -2494,7 +2471,7 @@ fn lint_map_flatten<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr<
24942471 // lint if caller of `.map().flatten()` is an Iterator
24952472 if match_trait_method ( cx, expr, & paths:: ITERATOR ) {
24962473 let msg = "called `map(..).flatten()` on an `Iterator`. \
2497- This is more succinctly expressed by calling `.flat_map(..)`";
2474+ This is more succinctly expressed by calling `.flat_map(..)`";
24982475 let self_snippet = snippet ( cx, map_args[ 0 ] . span , ".." ) ;
24992476 let func_snippet = snippet ( cx, map_args[ 1 ] . span , ".." ) ;
25002477 let hint = format ! ( "{0}.flat_map({1})" , self_snippet, func_snippet) ;
@@ -2555,10 +2532,10 @@ fn lint_map_unwrap_or_else<'a, 'tcx>(
25552532 // lint message
25562533 let msg = if is_option {
25572534 "called `map(f).unwrap_or_else(g)` on an `Option` value. This can be done more directly by calling \
2558- `map_or_else(g, f)` instead"
2535+ `map_or_else(g, f)` instead"
25592536 } else {
25602537 "called `map(f).unwrap_or_else(g)` on a `Result` value. This can be done more directly by calling \
2561- `.map_or_else(g, f)` instead"
2538+ `.map_or_else(g, f)` instead"
25622539 } ;
25632540 // get snippets for args to map() and unwrap_or_else()
25642541 let map_snippet = snippet ( cx, map_args[ 1 ] . span , ".." ) ;
@@ -2570,11 +2547,7 @@ fn lint_map_unwrap_or_else<'a, 'tcx>(
25702547 if same_span && !multiline {
25712548 span_lint_and_note (
25722549 cx,
2573- if is_option {
2574- OPTION_MAP_UNWRAP_OR_ELSE
2575- } else {
2576- RESULT_MAP_UNWRAP_OR_ELSE
2577- } ,
2550+ MAP_UNWRAP ,
25782551 expr. span ,
25792552 msg,
25802553 None ,
@@ -2584,16 +2557,7 @@ fn lint_map_unwrap_or_else<'a, 'tcx>(
25842557 ) ,
25852558 ) ;
25862559 } else if same_span && multiline {
2587- span_lint (
2588- cx,
2589- if is_option {
2590- OPTION_MAP_UNWRAP_OR_ELSE
2591- } else {
2592- RESULT_MAP_UNWRAP_OR_ELSE
2593- } ,
2594- expr. span ,
2595- msg,
2596- ) ;
2560+ span_lint ( cx, MAP_UNWRAP , expr. span , msg) ;
25972561 } ;
25982562 }
25992563}
0 commit comments