@@ -3,16 +3,17 @@ mod for_loop_explicit_counter;
3
3
mod for_loop_over_map_kv;
4
4
mod for_loop_range;
5
5
mod for_mut_range_bound;
6
+ mod for_single_element_loop;
6
7
mod manual_flatten;
7
8
mod utils;
8
9
9
10
use crate :: consts:: constant;
10
11
use crate :: utils:: sugg:: Sugg ;
11
12
use crate :: utils:: usage:: mutated_variables;
12
13
use crate :: utils:: {
13
- get_enclosing_block, get_parent_expr, get_trait_def_id, higher, implements_trait, indent_of , is_in_panic_handler,
14
+ get_enclosing_block, get_parent_expr, get_trait_def_id, higher, implements_trait, is_in_panic_handler,
14
15
is_integer_const, is_no_std_crate, is_refutable, is_type_diagnostic_item, last_path_segment, match_trait_method,
15
- match_type, path_to_local, path_to_local_id, paths, single_segment_path , snippet, snippet_with_applicability,
16
+ match_type, path_to_local, path_to_local_id, paths, snippet, snippet_with_applicability,
16
17
snippet_with_macro_callsite, span_lint, span_lint_and_help, span_lint_and_sugg, span_lint_and_then, sugg,
17
18
} ;
18
19
use if_chain:: if_chain;
@@ -863,7 +864,7 @@ fn check_for_loop<'tcx>(
863
864
for_loop_arg:: check_for_loop_arg ( cx, pat, arg, expr) ;
864
865
for_loop_over_map_kv:: check_for_loop_over_map_kv ( cx, pat, arg, body, expr) ;
865
866
for_mut_range_bound:: check_for_mut_range_bound ( cx, arg, body) ;
866
- check_for_single_element_loop ( cx, pat, arg, body, expr) ;
867
+ for_single_element_loop :: check_for_single_element_loop ( cx, pat, arg, body, expr) ;
867
868
detect_same_item_push ( cx, pat, arg, body, expr) ;
868
869
manual_flatten:: check_manual_flatten ( cx, pat, arg, body, span) ;
869
870
}
@@ -1464,42 +1465,6 @@ fn detect_same_item_push<'tcx>(
1464
1465
}
1465
1466
}
1466
1467
1467
- fn check_for_single_element_loop < ' tcx > (
1468
- cx : & LateContext < ' tcx > ,
1469
- pat : & ' tcx Pat < ' _ > ,
1470
- arg : & ' tcx Expr < ' _ > ,
1471
- body : & ' tcx Expr < ' _ > ,
1472
- expr : & ' tcx Expr < ' _ > ,
1473
- ) {
1474
- if_chain ! {
1475
- if let ExprKind :: AddrOf ( BorrowKind :: Ref , _, ref arg_expr) = arg. kind;
1476
- if let PatKind :: Binding ( .., target, _) = pat. kind;
1477
- if let ExprKind :: Array ( [ arg_expression] ) = arg_expr. kind;
1478
- if let ExprKind :: Path ( ref list_item) = arg_expression. kind;
1479
- if let Some ( list_item_name) = single_segment_path( list_item) . map( |ps| ps. ident. name) ;
1480
- if let ExprKind :: Block ( ref block, _) = body. kind;
1481
- if !block. stmts. is_empty( ) ;
1482
-
1483
- then {
1484
- let for_span = get_span_of_entire_for_loop( expr) ;
1485
- let mut block_str = snippet( cx, block. span, ".." ) . into_owned( ) ;
1486
- block_str. remove( 0 ) ;
1487
- block_str. pop( ) ;
1488
-
1489
-
1490
- span_lint_and_sugg(
1491
- cx,
1492
- SINGLE_ELEMENT_LOOP ,
1493
- for_span,
1494
- "for loop over a single element" ,
1495
- "try" ,
1496
- format!( "{{\n {}let {} = &{};{}}}" , " " . repeat( indent_of( cx, block. stmts[ 0 ] . span) . unwrap_or( 0 ) ) , target. name, list_item_name, block_str) ,
1497
- Applicability :: MachineApplicable
1498
- )
1499
- }
1500
- }
1501
- }
1502
-
1503
1468
fn is_used_inside < ' tcx > ( cx : & LateContext < ' tcx > , expr : & ' tcx Expr < ' _ > , container : & ' tcx Expr < ' _ > ) -> bool {
1504
1469
let def_id = match path_to_local ( expr) {
1505
1470
Some ( id) => id,
0 commit comments