|
1 | 1 | mod bind_instead_of_map; |
2 | 2 | mod inefficient_to_string; |
| 3 | +mod inspect_for_each; |
3 | 4 | mod manual_saturating_arithmetic; |
4 | 5 | mod option_map_unwrap_or; |
5 | 6 | mod unnecessary_filter_map; |
@@ -1405,6 +1406,36 @@ declare_clippy_lint! { |
1405 | 1406 | "use `.collect()` instead of `::from_iter()`" |
1406 | 1407 | } |
1407 | 1408 |
|
| 1409 | +declare_clippy_lint! { |
| 1410 | + /// **What it does:** Checks for usage of `inspect().for_each()`. |
| 1411 | + /// |
| 1412 | + /// **Why is this bad?** It is the same as performing the computation |
| 1413 | + /// inside `inspect` at the beginning of the closure in `for_each`. |
| 1414 | + /// |
| 1415 | + /// **Known problems:** None. |
| 1416 | + /// |
| 1417 | + /// **Example:** |
| 1418 | + /// |
| 1419 | + /// ```rust |
| 1420 | + /// [1,2,3,4,5].iter() |
| 1421 | + /// .inspect(|&x| println!("inspect the number: {}", x)) |
| 1422 | + /// .for_each(|&x| { |
| 1423 | + /// assert!(x >= 0); |
| 1424 | + /// }); |
| 1425 | + /// ``` |
| 1426 | + /// Can be written as |
| 1427 | + /// ```rust |
| 1428 | + /// [1,2,3,4,5].iter() |
| 1429 | + /// .for_each(|&x| { |
| 1430 | + /// println!("inspect the number: {}", x); |
| 1431 | + /// assert!(x >= 0); |
| 1432 | + /// }); |
| 1433 | + /// ``` |
| 1434 | + pub INSPECT_FOR_EACH, |
| 1435 | + complexity, |
| 1436 | + "using `.inspect().for_each()`, which can be replaced with `.for_each()`" |
| 1437 | +} |
| 1438 | + |
1408 | 1439 | pub struct Methods { |
1409 | 1440 | msrv: Option<RustcVersion>, |
1410 | 1441 | } |
@@ -1467,6 +1498,7 @@ impl_lint_pass!(Methods => [ |
1467 | 1498 | UNNECESSARY_LAZY_EVALUATIONS, |
1468 | 1499 | MAP_COLLECT_RESULT_UNIT, |
1469 | 1500 | FROM_ITER_INSTEAD_OF_COLLECT, |
| 1501 | + INSPECT_FOR_EACH, |
1470 | 1502 | ]); |
1471 | 1503 |
|
1472 | 1504 | impl<'tcx> LateLintPass<'tcx> for Methods { |
@@ -1553,6 +1585,7 @@ impl<'tcx> LateLintPass<'tcx> for Methods { |
1553 | 1585 | ["get_or_insert_with", ..] => unnecessary_lazy_eval::lint(cx, expr, arg_lists[0], "get_or_insert"), |
1554 | 1586 | ["ok_or_else", ..] => unnecessary_lazy_eval::lint(cx, expr, arg_lists[0], "ok_or"), |
1555 | 1587 | ["collect", "map"] => lint_map_collect(cx, expr, arg_lists[1], arg_lists[0]), |
| 1588 | + ["for_each", "inspect"] => inspect_for_each::lint(cx, expr, method_spans[1]), |
1556 | 1589 | _ => {}, |
1557 | 1590 | } |
1558 | 1591 |
|
|
0 commit comments