1
1
use itertools:: { repeat_n, Itertools } ;
2
2
use rustc:: hir:: * ;
3
- use rustc:: lint:: * ;
4
- use rustc:: ty:: { AssociatedKind , TypeVariants } ;
3
+ use rustc:: ty:: { AssociatedKind , TyKind } ;
5
4
use syntax:: ast:: NodeId ;
6
5
7
6
use std:: collections:: HashSet ;
8
7
8
+ use crate :: rustc_errors:: Applicability ;
9
+ use crate :: rustc:: lint:: {
10
+ LateContext , LateLintPass , LintArray , LintPass ,
11
+ } ;
12
+ use crate :: rustc:: { declare_tool_lint, lint_array} ;
9
13
use crate :: utils:: { match_trait_method, match_type, span_lint_and_sugg} ;
10
14
use crate :: utils:: paths;
11
15
16
+ use if_chain:: if_chain;
17
+
12
18
/// **What it does:** Detects collect calls on iterators to collections
13
19
/// of either `Result<_, E>` or `Option<_>` inside functions that also
14
20
/// have such a return type.
@@ -69,11 +75,11 @@ struct Suggestion {
69
75
70
76
fn format_suggestion_pattern < ' a , ' tcx > (
71
77
cx : & LateContext < ' a , ' tcx > ,
72
- collection_ty : & TypeVariants ,
78
+ collection_ty : & TyKind < ' _ > ,
73
79
is_option : bool ,
74
80
) -> String {
75
81
let collection_pat = match collection_ty {
76
- TypeVariants :: TyAdt ( def, subs) => {
82
+ TyKind :: Adt ( def, subs) => {
77
83
let mut buf = cx. tcx . item_path_str ( def. did ) ;
78
84
79
85
if !subs. is_empty ( ) {
@@ -84,7 +90,7 @@ fn format_suggestion_pattern<'a, 'tcx>(
84
90
85
91
buf
86
92
} ,
87
- TypeVariants :: TyParam ( p) => p. to_string ( ) ,
93
+ TyKind :: Param ( p) => p. to_string ( ) ,
88
94
_ => "_" . into ( ) ,
89
95
} ;
90
96
@@ -96,8 +102,8 @@ fn format_suggestion_pattern<'a, 'tcx>(
96
102
}
97
103
98
104
fn check_expr_for_collect < ' a , ' tcx > ( cx : & LateContext < ' a , ' tcx > , expr : & ' tcx Expr ) -> Option < Suggestion > {
99
- if let ExprMethodCall ( ref method, _, ref args) = expr. node {
100
- if args. len ( ) == 1 && method. name == "collect" && match_trait_method ( cx, expr, & paths:: ITERATOR ) {
105
+ if let ExprKind :: MethodCall ( ref method, _, ref args) = expr. node {
106
+ if args. len ( ) == 1 && method. ident . name == "collect" && match_trait_method ( cx, expr, & paths:: ITERATOR ) {
101
107
let collect_ty = cx. tables . expr_ty ( expr) ;
102
108
103
109
if match_type ( cx, collect_ty, & paths:: OPTION ) || match_type ( cx, collect_ty, & paths:: RESULT ) {
@@ -153,7 +159,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
153
159
}
154
160
155
161
if let Some ( suggestion) = check_expr_for_collect ( cx, expr) {
156
- let sugg_span = if let ExprMethodCall ( _, call_span, _) = expr. node {
162
+ let sugg_span = if let ExprKind :: MethodCall ( _, call_span, _) = expr. node {
157
163
expr. span . between ( call_span)
158
164
} else {
159
165
unreachable ! ( )
@@ -169,14 +175,15 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
169
175
suggestion. success_variant
170
176
) ,
171
177
format ! ( "collect::<{}>()" , suggestion. pattern) ,
178
+ Applicability :: MaybeIncorrect
172
179
) ;
173
180
}
174
181
}
175
182
176
183
fn check_stmt ( & mut self , cx : & LateContext < ' a , ' tcx > , stmt : & ' tcx Stmt ) {
177
184
if_chain ! {
178
- if let StmtDecl ( ref decl, _) = stmt. node;
179
- if let DeclLocal ( ref local) = decl. node;
185
+ if let StmtKind :: Decl ( ref decl, _) = stmt. node;
186
+ if let DeclKind :: Local ( ref local) = decl. node;
180
187
if let Some ( ref ty) = local. ty;
181
188
if let Some ( ref expr) = local. init;
182
189
then {
@@ -192,7 +199,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
192
199
"if you are only interested in the case where all values are `{}`, try" ,
193
200
suggestion. success_variant
194
201
) ,
195
- suggestion. pattern
202
+ suggestion. pattern,
203
+ Applicability :: MaybeIncorrect
196
204
) ;
197
205
}
198
206
}
0 commit comments