@@ -10,7 +10,6 @@ use rustc_hir::intravisit::{walk_block, walk_expr, NestedVisitorMap, Visitor};
10
10
use rustc_hir:: { Block , Expr , ExprKind , GenericArg , GenericArgs , HirId , Local , Pat , PatKind , QPath , StmtKind , Ty } ;
11
11
use rustc_lint:: LateContext ;
12
12
use rustc_middle:: hir:: map:: Map ;
13
-
14
13
use rustc_span:: symbol:: { sym, Ident } ;
15
14
use rustc_span:: { MultiSpan , Span } ;
16
15
@@ -28,32 +27,45 @@ fn check_needless_collect_direct_usage<'tcx>(expr: &'tcx Expr<'_>, cx: &LateCont
28
27
if let Some ( generic_args) = chain_method. args;
29
28
if let Some ( GenericArg :: Type ( ref ty) ) = generic_args. args. get( 0 ) ;
30
29
if let Some ( ty) = cx. typeck_results( ) . node_type_opt( ty. hir_id) ;
31
- if is_type_diagnostic_item( cx, ty, sym:: vec_type)
32
- || is_type_diagnostic_item( cx, ty, sym:: vecdeque_type)
33
- || match_type( cx, ty, & paths:: BTREEMAP )
34
- || is_type_diagnostic_item( cx, ty, sym:: hashmap_type) ;
35
- if let Some ( sugg) = match & * method. ident. name. as_str( ) {
36
- "len" => Some ( "count()" . to_string( ) ) ,
37
- "is_empty" => Some ( "next().is_none()" . to_string( ) ) ,
38
- "contains" => {
39
- let contains_arg = snippet( cx, args[ 1 ] . span, "??" ) ;
40
- let ( arg, pred) = contains_arg
41
- . strip_prefix( '&' )
42
- . map_or( ( "&x" , & * contains_arg) , |s| ( "x" , s) ) ;
43
- Some ( format!( "any(|{}| x == {})" , arg, pred) )
44
- }
45
- _ => None ,
46
- } ;
47
30
then {
48
- span_lint_and_sugg(
49
- cx,
50
- NEEDLESS_COLLECT ,
51
- method0_span. with_hi( expr. span. hi( ) ) ,
52
- NEEDLESS_COLLECT_MSG ,
53
- "replace with" ,
54
- sugg,
55
- Applicability :: MachineApplicable ,
56
- ) ;
31
+ let is_empty_sugg = Some ( "next().is_none()" . to_string( ) ) ;
32
+ let method_name = & * method. ident. name. as_str( ) ;
33
+ let sugg = if is_type_diagnostic_item( cx, ty, sym:: vec_type) ||
34
+ is_type_diagnostic_item( cx, ty, sym:: vecdeque_type) {
35
+ match method_name {
36
+ "len" => Some ( "count()" . to_string( ) ) ,
37
+ "is_empty" => is_empty_sugg,
38
+ "contains" => {
39
+ let contains_arg = snippet( cx, args[ 1 ] . span, "??" ) ;
40
+ let ( arg, pred) = contains_arg
41
+ . strip_prefix( '&' )
42
+ . map_or( ( "&x" , & * contains_arg) , |s| ( "x" , s) ) ;
43
+ Some ( format!( "any(|{}| x == {})" , arg, pred) )
44
+ }
45
+ _ => None ,
46
+ }
47
+ }
48
+ else if match_type( cx, ty, & paths:: BTREEMAP ) ||
49
+ is_type_diagnostic_item( cx, ty, sym:: hashmap_type) {
50
+ match method_name {
51
+ "is_empty" => is_empty_sugg,
52
+ _ => None ,
53
+ }
54
+ }
55
+ else {
56
+ None
57
+ } ;
58
+ if let Some ( sugg) = sugg {
59
+ span_lint_and_sugg(
60
+ cx,
61
+ NEEDLESS_COLLECT ,
62
+ method0_span. with_hi( expr. span. hi( ) ) ,
63
+ NEEDLESS_COLLECT_MSG ,
64
+ "replace with" ,
65
+ sugg,
66
+ Applicability :: MachineApplicable ,
67
+ ) ;
68
+ }
57
69
}
58
70
}
59
71
}
0 commit comments