@@ -10,6 +10,7 @@ mod inefficient_to_string;
10
10
mod inspect_for_each;
11
11
mod iter_cloned_collect;
12
12
mod iter_count;
13
+ mod iter_next_slice;
13
14
mod manual_saturating_arithmetic;
14
15
mod map_collect_result_unit;
15
16
mod ok_expect;
@@ -46,9 +47,9 @@ use crate::consts::{constant, Constant};
46
47
use crate :: utils:: eager_or_lazy:: is_lazyness_candidate;
47
48
use crate :: utils:: usage:: mutated_variables;
48
49
use crate :: utils:: {
49
- contains_return, contains_ty, get_parent_expr, get_trait_def_id, has_iter_method, higher , implements_trait ,
50
- in_macro , is_copy, is_expn_of, is_type_diagnostic_item, iter_input_pats, last_path_segment, match_def_path,
51
- match_qpath , match_trait_method, match_type, meets_msrv, method_calls, method_chain_args, path_to_local_id, paths,
50
+ contains_return, contains_ty, get_parent_expr, get_trait_def_id, has_iter_method, implements_trait , in_macro ,
51
+ is_copy, is_expn_of, is_type_diagnostic_item, iter_input_pats, last_path_segment, match_def_path, match_qpath ,
52
+ match_trait_method, match_type, meets_msrv, method_calls, method_chain_args, path_to_local_id, paths,
52
53
remove_blocks, return_ty, single_segment_path, snippet, snippet_with_applicability, snippet_with_macro_callsite,
53
54
span_lint, span_lint_and_help, span_lint_and_sugg, span_lint_and_then, strip_pat_refs, sugg, walk_ptrs_ty_depth,
54
55
SpanlessEq ,
@@ -1688,7 +1689,7 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
1688
1689
} ,
1689
1690
[ "next" , "filter" ] => filter_next:: check ( cx, expr, arg_lists[ 1 ] ) ,
1690
1691
[ "next" , "skip_while" ] => skip_while_next:: check ( cx, expr, arg_lists[ 1 ] ) ,
1691
- [ "next" , "iter" ] => lint_iter_next ( cx, expr, arg_lists[ 1 ] ) ,
1692
+ [ "next" , "iter" ] => iter_next_slice :: check ( cx, expr, arg_lists[ 1 ] ) ,
1692
1693
[ "map" , "filter" ] => lint_filter_map ( cx, expr, false ) ,
1693
1694
[ "map" , "filter_map" ] => lint_filter_map_map ( cx, expr, arg_lists[ 1 ] , arg_lists[ 0 ] ) ,
1694
1695
[ "next" , "filter_map" ] => lint_filter_map_next ( cx, expr, arg_lists[ 1 ] , self . msrv . as_ref ( ) ) ,
@@ -2598,63 +2599,6 @@ fn lint_step_by<'tcx>(cx: &LateContext<'tcx>, expr: &hir::Expr<'_>, args: &'tcx
2598
2599
}
2599
2600
}
2600
2601
2601
- fn lint_iter_next < ' tcx > ( cx : & LateContext < ' tcx > , expr : & ' tcx hir:: Expr < ' _ > , iter_args : & ' tcx [ hir:: Expr < ' _ > ] ) {
2602
- let caller_expr = & iter_args[ 0 ] ;
2603
-
2604
- // Skip lint if the `iter().next()` expression is a for loop argument,
2605
- // since it is already covered by `&loops::ITER_NEXT_LOOP`
2606
- let mut parent_expr_opt = get_parent_expr ( cx, expr) ;
2607
- while let Some ( parent_expr) = parent_expr_opt {
2608
- if higher:: for_loop ( parent_expr) . is_some ( ) {
2609
- return ;
2610
- }
2611
- parent_expr_opt = get_parent_expr ( cx, parent_expr) ;
2612
- }
2613
-
2614
- if derefs_to_slice ( cx, caller_expr, cx. typeck_results ( ) . expr_ty ( caller_expr) ) . is_some ( ) {
2615
- // caller is a Slice
2616
- if_chain ! {
2617
- if let hir:: ExprKind :: Index ( ref caller_var, ref index_expr) = & caller_expr. kind;
2618
- if let Some ( higher:: Range { start: Some ( start_expr) , end: None , limits: ast:: RangeLimits :: HalfOpen } )
2619
- = higher:: range( index_expr) ;
2620
- if let hir:: ExprKind :: Lit ( ref start_lit) = & start_expr. kind;
2621
- if let ast:: LitKind :: Int ( start_idx, _) = start_lit. node;
2622
- then {
2623
- let mut applicability = Applicability :: MachineApplicable ;
2624
- span_lint_and_sugg(
2625
- cx,
2626
- ITER_NEXT_SLICE ,
2627
- expr. span,
2628
- "using `.iter().next()` on a Slice without end index" ,
2629
- "try calling" ,
2630
- format!( "{}.get({})" , snippet_with_applicability( cx, caller_var. span, ".." , & mut applicability) , start_idx) ,
2631
- applicability,
2632
- ) ;
2633
- }
2634
- }
2635
- } else if is_type_diagnostic_item ( cx, cx. typeck_results ( ) . expr_ty ( caller_expr) , sym:: vec_type)
2636
- || matches ! (
2637
- & cx. typeck_results( ) . expr_ty( caller_expr) . peel_refs( ) . kind( ) ,
2638
- ty:: Array ( _, _)
2639
- )
2640
- {
2641
- // caller is a Vec or an Array
2642
- let mut applicability = Applicability :: MachineApplicable ;
2643
- span_lint_and_sugg (
2644
- cx,
2645
- ITER_NEXT_SLICE ,
2646
- expr. span ,
2647
- "using `.iter().next()` on an array" ,
2648
- "try calling" ,
2649
- format ! (
2650
- "{}.get(0)" ,
2651
- snippet_with_applicability( cx, caller_expr. span, ".." , & mut applicability)
2652
- ) ,
2653
- applicability,
2654
- ) ;
2655
- }
2656
- }
2657
-
2658
2602
fn lint_iter_nth < ' tcx > (
2659
2603
cx : & LateContext < ' tcx > ,
2660
2604
expr : & hir:: Expr < ' _ > ,
0 commit comments