|
1 | 1 | mod bind_instead_of_map; |
| 2 | +mod filter_map_identity; |
2 | 3 | mod inefficient_to_string; |
3 | 4 | mod inspect_for_each; |
4 | 5 | mod manual_saturating_arithmetic; |
@@ -1467,6 +1468,29 @@ declare_clippy_lint! { |
1467 | 1468 | "using `.inspect().for_each()`, which can be replaced with `.for_each()`" |
1468 | 1469 | } |
1469 | 1470 |
|
| 1471 | +declare_clippy_lint! { |
| 1472 | + /// **What it does:** Checks for usage of `filter_map(|x| x)`. |
| 1473 | + /// |
| 1474 | + /// **Why is this bad?** Readability, this can be written more concisely by using `flatten`. |
| 1475 | + /// |
| 1476 | + /// **Known problems:** None. |
| 1477 | + /// |
| 1478 | + /// **Example:** |
| 1479 | + /// |
| 1480 | + /// ```rust |
| 1481 | + /// # let iter = vec![Some(1)].into_iter(); |
| 1482 | + /// iter.filter_map(|x| x); |
| 1483 | + /// ``` |
| 1484 | + /// Use instead: |
| 1485 | + /// ```rust |
| 1486 | + /// # let iter = vec![Some(1)].into_iter(); |
| 1487 | + /// iter.flatten(); |
| 1488 | + /// ``` |
| 1489 | + pub FILTER_MAP_IDENTITY, |
| 1490 | + complexity, |
| 1491 | + "call to `filter_map` where `flatten` is sufficient" |
| 1492 | +} |
| 1493 | + |
1470 | 1494 | pub struct Methods { |
1471 | 1495 | msrv: Option<RustcVersion>, |
1472 | 1496 | } |
@@ -1504,6 +1528,7 @@ impl_lint_pass!(Methods => [ |
1504 | 1528 | FILTER_NEXT, |
1505 | 1529 | SKIP_WHILE_NEXT, |
1506 | 1530 | FILTER_MAP, |
| 1531 | + FILTER_MAP_IDENTITY, |
1507 | 1532 | MANUAL_FILTER_MAP, |
1508 | 1533 | MANUAL_FIND_MAP, |
1509 | 1534 | FILTER_MAP_NEXT, |
@@ -1597,7 +1622,10 @@ impl<'tcx> LateLintPass<'tcx> for Methods { |
1597 | 1622 | ["as_ref"] => lint_asref(cx, expr, "as_ref", arg_lists[0]), |
1598 | 1623 | ["as_mut"] => lint_asref(cx, expr, "as_mut", arg_lists[0]), |
1599 | 1624 | ["fold", ..] => lint_unnecessary_fold(cx, expr, arg_lists[0], method_spans[0]), |
1600 | | - ["filter_map", ..] => unnecessary_filter_map::lint(cx, expr, arg_lists[0]), |
| 1625 | + ["filter_map", ..] => { |
| 1626 | + unnecessary_filter_map::lint(cx, expr, arg_lists[0]); |
| 1627 | + filter_map_identity::check(cx, expr, arg_lists[0], method_spans[0]); |
| 1628 | + }, |
1601 | 1629 | ["count", "map"] => lint_suspicious_map(cx, expr), |
1602 | 1630 | ["assume_init"] => lint_maybe_uninit(cx, &arg_lists[0][0], expr), |
1603 | 1631 | ["unwrap_or", arith @ ("checked_add" | "checked_sub" | "checked_mul")] => { |
|
0 commit comments