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