@@ -257,59 +257,40 @@ declare_clippy_lint! {
257
257
}
258
258
259
259
declare_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(_)`.
261
262
///
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 (_, _)`.
264
265
///
265
266
/// **Known problems:** The order of the arguments is not in execution order
266
267
///
267
- /// **Example :**
268
+ /// **Examples :**
268
269
/// ```rust
269
270
/// # 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(_, _)`.
282
271
///
283
- /// **Known problems:** The order of the arguments is not in execution order.
272
+ /// // Bad
273
+ /// x.map(|a| a + 1).unwrap_or(0);
284
274
///
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);
290
277
/// ```
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(_)`.
298
278
///
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
303
280
///
304
- /// **Example:**
305
281
/// ```rust
306
282
/// # let x: Result<usize, ()> = Ok(1);
307
283
/// # fn some_function(foo: ()) -> usize { 1 }
284
+ ///
285
+ /// // Bad
308
286
/// x.map(|a| a + 1).unwrap_or_else(some_function);
287
+ ///
288
+ /// // Good
289
+ /// x.map_or_else(some_function, |a| a + 1);
309
290
/// ```
310
- pub RESULT_MAP_UNWRAP_OR_ELSE ,
291
+ pub MAP_UNWRAP ,
311
292
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)`"
313
294
}
314
295
315
296
declare_clippy_lint ! {
@@ -1294,9 +1275,7 @@ declare_lint_pass!(Methods => [
1294
1275
WRONG_SELF_CONVENTION ,
1295
1276
WRONG_PUB_SELF_CONVENTION ,
1296
1277
OK_EXPECT ,
1297
- OPTION_MAP_UNWRAP_OR ,
1298
- OPTION_MAP_UNWRAP_OR_ELSE ,
1299
- RESULT_MAP_UNWRAP_OR_ELSE ,
1278
+ MAP_UNWRAP ,
1300
1279
RESULT_MAP_OR_INTO_OPTION ,
1301
1280
OPTION_MAP_OR_NONE ,
1302
1281
OPTION_AND_THEN_SOME ,
@@ -1503,9 +1482,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Methods {
1503
1482
cx,
1504
1483
lint,
1505
1484
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" ,
1509
1486
conv,
1510
1487
& self_kinds
1511
1488
. iter( )
@@ -1678,7 +1655,7 @@ fn lint_or_fun_call<'a, 'tcx>(
1678
1655
let self_ty = cx. tables. expr_ty( self_expr) ;
1679
1656
1680
1657
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 ) ) ;
1682
1659
1683
1660
if poss. contains( & name) ;
1684
1661
@@ -1931,7 +1908,7 @@ fn lint_clone_on_copy(cx: &LateContext<'_, '_>, expr: &hir::Expr<'_>, arg: &hir:
1931
1908
CLONE_DOUBLE_REF ,
1932
1909
expr. span ,
1933
1910
"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",
1935
1912
|diag| {
1936
1913
if let Some ( snip) = sugg:: Sugg :: hir_opt ( cx, arg) {
1937
1914
let mut ty = innermost;
@@ -2121,7 +2098,7 @@ fn lint_iter_cloned_collect<'a, 'tcx>(
2121
2098
ITER_CLONED_COLLECT ,
2122
2099
to_replace,
2123
2100
"called `iter().cloned().collect()` on a slice to create a `Vec`. Calling `to_vec()` is both faster and \
2124
- more readable",
2101
+ more readable",
2125
2102
"try" ,
2126
2103
".to_vec()" . to_string( ) ,
2127
2104
Applicability :: MachineApplicable ,
@@ -2436,7 +2413,7 @@ fn lint_unwrap(cx: &LateContext<'_, '_>, expr: &hir::Expr<'_>, unwrap_args: &[hi
2436
2413
None ,
2437
2414
& format ! (
2438
2415
"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",
2440
2417
none_value,
2441
2418
) ,
2442
2419
) ;
@@ -2494,7 +2471,7 @@ fn lint_map_flatten<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr<
2494
2471
// lint if caller of `.map().flatten()` is an Iterator
2495
2472
if match_trait_method ( cx, expr, & paths:: ITERATOR ) {
2496
2473
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(..)`";
2498
2475
let self_snippet = snippet ( cx, map_args[ 0 ] . span , ".." ) ;
2499
2476
let func_snippet = snippet ( cx, map_args[ 1 ] . span , ".." ) ;
2500
2477
let hint = format ! ( "{0}.flat_map({1})" , self_snippet, func_snippet) ;
@@ -2555,10 +2532,10 @@ fn lint_map_unwrap_or_else<'a, 'tcx>(
2555
2532
// lint message
2556
2533
let msg = if is_option {
2557
2534
"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"
2559
2536
} else {
2560
2537
"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"
2562
2539
} ;
2563
2540
// get snippets for args to map() and unwrap_or_else()
2564
2541
let map_snippet = snippet ( cx, map_args[ 1 ] . span , ".." ) ;
@@ -2570,11 +2547,7 @@ fn lint_map_unwrap_or_else<'a, 'tcx>(
2570
2547
if same_span && !multiline {
2571
2548
span_lint_and_note (
2572
2549
cx,
2573
- if is_option {
2574
- OPTION_MAP_UNWRAP_OR_ELSE
2575
- } else {
2576
- RESULT_MAP_UNWRAP_OR_ELSE
2577
- } ,
2550
+ MAP_UNWRAP ,
2578
2551
expr. span ,
2579
2552
msg,
2580
2553
None ,
@@ -2584,16 +2557,7 @@ fn lint_map_unwrap_or_else<'a, 'tcx>(
2584
2557
) ,
2585
2558
) ;
2586
2559
} 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) ;
2597
2561
} ;
2598
2562
}
2599
2563
}
0 commit comments