@@ -4,6 +4,7 @@ mod filter_map_identity;
4
4
mod implicit_clone;
5
5
mod inefficient_to_string;
6
6
mod inspect_for_each;
7
+ mod iter_count;
7
8
mod manual_saturating_arithmetic;
8
9
mod option_map_unwrap_or;
9
10
mod unnecessary_filter_map;
@@ -32,12 +33,12 @@ use crate::consts::{constant, Constant};
32
33
use crate :: utils:: eager_or_lazy:: is_lazyness_candidate;
33
34
use crate :: utils:: usage:: mutated_variables;
34
35
use crate :: utils:: {
35
- contains_return, contains_ty, get_parent_expr, get_trait_def_id, has_iter_method, higher, implements_trait ,
36
- in_macro, is_copy, is_expn_of, is_type_diagnostic_item, iter_input_pats, last_path_segment, match_def_path ,
37
- match_qpath, match_trait_method, match_type, meets_msrv, method_calls, method_chain_args, path_to_local_id , paths ,
38
- remove_blocks, return_ty, single_segment_path, snippet, snippet_with_applicability, snippet_with_macro_callsite ,
39
- span_lint, span_lint_and_help, span_lint_and_sugg, span_lint_and_then, strip_pat_refs, sugg , walk_ptrs_ty_depth ,
40
- SpanlessEq ,
36
+ contains_return, contains_ty, derefs_to_slice , get_parent_expr, get_trait_def_id, has_iter_method, higher,
37
+ implements_trait , in_macro, is_copy, is_expn_of, is_type_diagnostic_item, iter_input_pats, last_path_segment,
38
+ match_def_path , match_qpath, match_trait_method, match_type, meets_msrv, method_calls, method_chain_args,
39
+ path_to_local_id , paths , remove_blocks, return_ty, single_segment_path, snippet, snippet_with_applicability,
40
+ snippet_with_macro_callsite , span_lint, span_lint_and_help, span_lint_and_sugg, span_lint_and_then, strip_pat_refs,
41
+ sugg , walk_ptrs_ty_depth , SpanlessEq ,
41
42
} ;
42
43
43
44
declare_clippy_lint ! {
@@ -1691,8 +1692,8 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
1691
1692
lint_search_is_some ( cx, expr, "rposition" , arg_lists[ 1 ] , arg_lists[ 0 ] , method_spans[ 1 ] )
1692
1693
} ,
1693
1694
[ "extend" , ..] => lint_extend ( cx, expr, arg_lists[ 0 ] ) ,
1694
- [ "count" , "into_iter" | "iter" ] => lint_iter_count ( cx, expr, & arg_lists[ 1 ] , false ) ,
1695
- [ "count" , "iter_mut" ] => lint_iter_count ( cx, expr, & arg_lists[ 1 ] , true ) ,
1695
+ [ "count" , "into_iter" | "iter" ] => iter_count :: lints ( cx, expr, & arg_lists[ 1 ] , false ) ,
1696
+ [ "count" , "iter_mut" ] => iter_count :: lints ( cx, expr, & arg_lists[ 1 ] , true ) ,
1696
1697
[ "nth" , "iter" ] => lint_iter_nth ( cx, expr, & arg_lists, false ) ,
1697
1698
[ "nth" , "iter_mut" ] => lint_iter_nth ( cx, expr, & arg_lists, true ) ,
1698
1699
[ "nth" , "bytes" ] => bytes_nth:: lints ( cx, expr, & arg_lists[ 1 ] ) ,
@@ -2661,46 +2662,6 @@ fn lint_iter_next<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>, iter_
2661
2662
}
2662
2663
}
2663
2664
2664
- fn lint_iter_count < ' tcx > ( cx : & LateContext < ' tcx > , expr : & Expr < ' _ > , iter_args : & ' tcx [ Expr < ' tcx > ] , is_mut : bool ) {
2665
- let mut_str = if is_mut { "_mut" } else { "" } ;
2666
- let iter_method = if method_chain_args ( expr, & [ format ! ( "iter{}" , mut_str) . as_str ( ) , "count" ] ) . is_some ( ) {
2667
- "iter"
2668
- } else if method_chain_args ( expr, & [ "into_iter" , "count" ] ) . is_some ( ) {
2669
- "into_iter"
2670
- } else {
2671
- return ;
2672
- } ;
2673
- if_chain ! {
2674
- let caller_type = if derefs_to_slice( cx, & iter_args[ 0 ] , cx. typeck_results( ) . expr_ty( & iter_args[ 0 ] ) ) . is_some( ) {
2675
- Some ( "slice" )
2676
- } else if is_type_diagnostic_item( cx, cx. typeck_results( ) . expr_ty( & iter_args[ 0 ] ) , sym:: vec_type) {
2677
- Some ( "Vec" )
2678
- } else if is_type_diagnostic_item( cx, cx. typeck_results( ) . expr_ty( & iter_args[ 0 ] ) , sym!( vecdeque_type) ) {
2679
- Some ( "VecDeque" )
2680
- } else if match_trait_method( cx, expr, & paths:: ITERATOR ) {
2681
- Some ( "std::iter::Iterator" )
2682
- } else {
2683
- None
2684
- } ;
2685
- if let Some ( caller_type) = caller_type;
2686
- then {
2687
- let mut applicability = Applicability :: MachineApplicable ;
2688
- span_lint_and_sugg(
2689
- cx,
2690
- ITER_COUNT ,
2691
- expr. span,
2692
- & format!( "called `.{}{}().count()` on a `{}`" , iter_method, mut_str, caller_type) ,
2693
- "try" ,
2694
- format!(
2695
- "{}.len()" ,
2696
- snippet_with_applicability( cx, iter_args[ 0 ] . span, ".." , & mut applicability) ,
2697
- ) ,
2698
- applicability,
2699
- ) ;
2700
- }
2701
- }
2702
- }
2703
-
2704
2665
fn lint_iter_nth < ' tcx > (
2705
2666
cx : & LateContext < ' tcx > ,
2706
2667
expr : & hir:: Expr < ' _ > ,
@@ -2841,46 +2802,6 @@ fn lint_iter_skip_next(cx: &LateContext<'_>, expr: &hir::Expr<'_>, skip_args: &[
2841
2802
}
2842
2803
}
2843
2804
2844
- fn derefs_to_slice < ' tcx > (
2845
- cx : & LateContext < ' tcx > ,
2846
- expr : & ' tcx hir:: Expr < ' tcx > ,
2847
- ty : Ty < ' tcx > ,
2848
- ) -> Option < & ' tcx hir:: Expr < ' tcx > > {
2849
- fn may_slice < ' a > ( cx : & LateContext < ' a > , ty : Ty < ' a > ) -> bool {
2850
- match ty. kind ( ) {
2851
- ty:: Slice ( _) => true ,
2852
- ty:: Adt ( def, _) if def. is_box ( ) => may_slice ( cx, ty. boxed_ty ( ) ) ,
2853
- ty:: Adt ( ..) => is_type_diagnostic_item ( cx, ty, sym:: vec_type) ,
2854
- ty:: Array ( _, size) => size
2855
- . try_eval_usize ( cx. tcx , cx. param_env )
2856
- . map_or ( false , |size| size < 32 ) ,
2857
- ty:: Ref ( _, inner, _) => may_slice ( cx, inner) ,
2858
- _ => false ,
2859
- }
2860
- }
2861
-
2862
- if let hir:: ExprKind :: MethodCall ( ref path, _, ref args, _) = expr. kind {
2863
- if path. ident . name == sym:: iter && may_slice ( cx, cx. typeck_results ( ) . expr_ty ( & args[ 0 ] ) ) {
2864
- Some ( & args[ 0 ] )
2865
- } else {
2866
- None
2867
- }
2868
- } else {
2869
- match ty. kind ( ) {
2870
- ty:: Slice ( _) => Some ( expr) ,
2871
- ty:: Adt ( def, _) if def. is_box ( ) && may_slice ( cx, ty. boxed_ty ( ) ) => Some ( expr) ,
2872
- ty:: Ref ( _, inner, _) => {
2873
- if may_slice ( cx, inner) {
2874
- Some ( expr)
2875
- } else {
2876
- None
2877
- }
2878
- } ,
2879
- _ => None ,
2880
- }
2881
- }
2882
- }
2883
-
2884
2805
/// lint use of `unwrap()` for `Option`s and `Result`s
2885
2806
fn lint_unwrap ( cx : & LateContext < ' _ > , expr : & hir:: Expr < ' _ > , unwrap_args : & [ hir:: Expr < ' _ > ] ) {
2886
2807
let obj_ty = cx. typeck_results ( ) . expr_ty ( & unwrap_args[ 0 ] ) . peel_refs ( ) ;
0 commit comments