1
1
mod for_loop_arg;
2
+ mod for_loop_explicit_counter;
2
3
mod for_loop_over_map_kv;
3
4
mod for_loop_range;
4
5
mod for_mut_range_bound;
@@ -32,7 +33,7 @@ use rustc_session::{declare_lint_pass, declare_tool_lint};
32
33
use rustc_span:: source_map:: Span ;
33
34
use rustc_span:: symbol:: { sym, Ident , Symbol } ;
34
35
use std:: iter:: { once, Iterator } ;
35
- use utils:: make_iterator_snippet;
36
+ use utils:: { get_span_of_entire_for_loop , make_iterator_snippet} ;
36
37
37
38
declare_clippy_lint ! {
38
39
/// **What it does:** Checks for for-loops that manually copy items between
@@ -857,7 +858,7 @@ fn check_for_loop<'tcx>(
857
858
let is_manual_memcpy_triggered = detect_manual_memcpy ( cx, pat, arg, body, expr) ;
858
859
if !is_manual_memcpy_triggered {
859
860
for_loop_range:: check_for_loop_range ( cx, pat, arg, body, expr) ;
860
- check_for_loop_explicit_counter ( cx, pat, arg, body, expr) ;
861
+ for_loop_explicit_counter :: check_for_loop_explicit_counter ( cx, pat, arg, body, expr) ;
861
862
}
862
863
for_loop_arg:: check_for_loop_arg ( cx, pat, arg, expr) ;
863
864
for_loop_over_map_kv:: check_for_loop_over_map_kv ( cx, pat, arg, body, expr) ;
@@ -867,17 +868,6 @@ fn check_for_loop<'tcx>(
867
868
manual_flatten:: check_manual_flatten ( cx, pat, arg, body, span) ;
868
869
}
869
870
870
- // this function assumes the given expression is a `for` loop.
871
- fn get_span_of_entire_for_loop ( expr : & Expr < ' _ > ) -> Span {
872
- // for some reason this is the only way to get the `Span`
873
- // of the entire `for` loop
874
- if let ExprKind :: Match ( _, arms, _) = & expr. kind {
875
- arms[ 0 ] . body . span
876
- } else {
877
- unreachable ! ( )
878
- }
879
- }
880
-
881
871
/// a wrapper of `Sugg`. Besides what `Sugg` do, this removes unnecessary `0`;
882
872
/// and also, it avoids subtracting a variable from the same one by replacing it with `0`.
883
873
/// it exists for the convenience of the overloaded operators while normal functions can do the
@@ -1474,55 +1464,6 @@ fn detect_same_item_push<'tcx>(
1474
1464
}
1475
1465
}
1476
1466
1477
- // To trigger the EXPLICIT_COUNTER_LOOP lint, a variable must be
1478
- // incremented exactly once in the loop body, and initialized to zero
1479
- // at the start of the loop.
1480
- fn check_for_loop_explicit_counter < ' tcx > (
1481
- cx : & LateContext < ' tcx > ,
1482
- pat : & ' tcx Pat < ' _ > ,
1483
- arg : & ' tcx Expr < ' _ > ,
1484
- body : & ' tcx Expr < ' _ > ,
1485
- expr : & ' tcx Expr < ' _ > ,
1486
- ) {
1487
- // Look for variables that are incremented once per loop iteration.
1488
- let mut increment_visitor = IncrementVisitor :: new ( cx) ;
1489
- walk_expr ( & mut increment_visitor, body) ;
1490
-
1491
- // For each candidate, check the parent block to see if
1492
- // it's initialized to zero at the start of the loop.
1493
- if let Some ( block) = get_enclosing_block ( & cx, expr. hir_id ) {
1494
- for id in increment_visitor. into_results ( ) {
1495
- let mut initialize_visitor = InitializeVisitor :: new ( cx, expr, id) ;
1496
- walk_block ( & mut initialize_visitor, block) ;
1497
-
1498
- if_chain ! {
1499
- if let Some ( ( name, initializer) ) = initialize_visitor. get_result( ) ;
1500
- if is_integer_const( cx, initializer, 0 ) ;
1501
- then {
1502
- let mut applicability = Applicability :: MachineApplicable ;
1503
-
1504
- let for_span = get_span_of_entire_for_loop( expr) ;
1505
-
1506
- span_lint_and_sugg(
1507
- cx,
1508
- EXPLICIT_COUNTER_LOOP ,
1509
- for_span. with_hi( arg. span. hi( ) ) ,
1510
- & format!( "the variable `{}` is used as a loop counter" , name) ,
1511
- "consider using" ,
1512
- format!(
1513
- "for ({}, {}) in {}.enumerate()" ,
1514
- name,
1515
- snippet_with_applicability( cx, pat. span, "item" , & mut applicability) ,
1516
- make_iterator_snippet( cx, arg, & mut applicability) ,
1517
- ) ,
1518
- applicability,
1519
- ) ;
1520
- }
1521
- }
1522
- }
1523
- }
1524
- }
1525
-
1526
1467
fn check_for_single_element_loop < ' tcx > (
1527
1468
cx : & LateContext < ' tcx > ,
1528
1469
pat : & ' tcx Pat < ' _ > ,
0 commit comments