@@ -231,17 +231,26 @@ impl LateLintPass for LoopsPass {
231
231
}
232
232
}
233
233
}
234
- if let ExprMatch ( ref expr , ref arms, MatchSource :: WhileLetDesugar ) = expr. node {
234
+ if let ExprMatch ( ref match_expr , ref arms, MatchSource :: WhileLetDesugar ) = expr. node {
235
235
let body = & arms[ 0 ] . body ;
236
236
let pat = & arms[ 0 ] . pats [ 0 ] . node ;
237
- if let ( & PatEnum ( ref path, _) , & ExprMethodCall ( method_name, _, ref args) ) = ( pat, & expr. node ) {
238
- let iterator_def_id = var_def_id ( cx, & args[ 0 ] ) ;
239
- if method_name. node . as_str ( ) == "next" &&
240
- match_trait_method ( cx, expr, & [ "core" , "iter" , "Iterator" ] ) &&
241
- path. segments . last ( ) . unwrap ( ) . identifier . name . as_str ( ) == "Some" &&
242
- !var_used ( body, iterator_def_id, cx) {
243
- span_lint ( cx, WHILE_LET_ON_ITERATOR , expr. span ,
244
- "this loop could be written as a `for` loop" ) ;
237
+ if let ( & PatEnum ( ref path, Some ( ref pat_args) ) ,
238
+ & ExprMethodCall ( method_name, _, ref method_args) ) =
239
+ ( pat, & match_expr. node ) {
240
+ let iterator_def_id = var_def_id ( cx, & method_args[ 0 ] ) ;
241
+ if let Some ( lhs_constructor) = path. segments . last ( ) {
242
+ if method_name. node . as_str ( ) == "next" &&
243
+ match_trait_method ( cx, match_expr, & [ "core" , "iter" , "Iterator" ] ) &&
244
+ lhs_constructor. identifier . name . as_str ( ) == "Some" &&
245
+ !var_used ( body, iterator_def_id, cx) {
246
+ let iterator = snippet ( cx, method_args[ 0 ] . span , "_" ) ;
247
+ let loop_var = snippet ( cx, pat_args[ 0 ] . span , "_" ) ;
248
+ span_help_and_lint ( cx, WHILE_LET_ON_ITERATOR , expr. span ,
249
+ "this loop could be written as a `for` loop" ,
250
+ & format ! ( "try\n for {} in {} {{...}}" ,
251
+ loop_var,
252
+ iterator) ) ;
253
+ }
245
254
}
246
255
}
247
256
}
0 commit comments