29
29
//! This lint is **warn** by default.
30
30
use rustc:: lint:: * ;
31
31
use syntax:: ast;
32
- use syntax:: codemap:: { original_sp, DUMMY_SP } ;
32
+ use syntax:: codemap:: { original_sp, DUMMY_SP } ;
33
33
use std:: borrow:: Cow ;
34
34
35
35
use utils:: { in_macro, span_help_and_lint, snippet_block, snippet, trim_multiline} ;
@@ -163,7 +163,7 @@ impl EarlyLintPass for NeedlessContinue {
163
163
* // region C
164
164
* }
165
165
* }
166
- */
166
+ * * /
167
167
168
168
/// Given an expression, returns true if either of the following is true
169
169
///
@@ -181,23 +181,27 @@ fn needless_continue_in_else(else_expr: &ast::Expr) -> bool {
181
181
fn is_first_block_stmt_continue ( block : & ast:: Block ) -> bool {
182
182
block. stmts . get ( 0 ) . map_or ( false , |stmt| match stmt. node {
183
183
ast:: StmtKind :: Semi ( ref e) |
184
- ast:: StmtKind :: Expr ( ref e) => if let ast:: ExprKind :: Continue ( _) = e. node {
185
- true
186
- } else {
187
- false
184
+ ast:: StmtKind :: Expr ( ref e) => {
185
+ if let ast:: ExprKind :: Continue ( _) = e. node {
186
+ true
187
+ } else {
188
+ false
189
+ }
188
190
} ,
189
191
_ => false ,
190
192
} )
191
193
}
192
194
193
195
/// If `expr` is a loop expression (while/while let/for/loop), calls `func` with
194
196
/// the AST object representing the loop block of `expr`.
195
- fn with_loop_block < F > ( expr : & ast:: Expr , mut func : F ) where F : FnMut ( & ast:: Block ) {
197
+ fn with_loop_block < F > ( expr : & ast:: Expr , mut func : F )
198
+ where F : FnMut ( & ast:: Block )
199
+ {
196
200
match expr. node {
197
- ast:: ExprKind :: While ( _, ref loop_block, _) |
201
+ ast:: ExprKind :: While ( _, ref loop_block, _) |
198
202
ast:: ExprKind :: WhileLet ( _, _, ref loop_block, _) |
199
- ast:: ExprKind :: ForLoop ( _, _, ref loop_block, _) |
200
- ast:: ExprKind :: Loop ( ref loop_block, _) => func ( loop_block) ,
203
+ ast:: ExprKind :: ForLoop ( _, _, ref loop_block, _) |
204
+ ast:: ExprKind :: Loop ( ref loop_block, _) => func ( loop_block) ,
201
205
_ => { } ,
202
206
}
203
207
}
@@ -211,15 +215,16 @@ fn with_loop_block<F>(expr: &ast::Expr, mut func: F) where F: FnMut(&ast::Block)
211
215
/// - The `else` expression.
212
216
///
213
217
fn with_if_expr < F > ( stmt : & ast:: Stmt , mut func : F )
214
- where F : FnMut ( & ast:: Expr , & ast:: Expr , & ast:: Block , & ast:: Expr ) {
218
+ where F : FnMut ( & ast:: Expr , & ast:: Expr , & ast:: Block , & ast:: Expr )
219
+ {
215
220
match stmt. node {
216
221
ast:: StmtKind :: Semi ( ref e) |
217
222
ast:: StmtKind :: Expr ( ref e) => {
218
223
if let ast:: ExprKind :: If ( ref cond, ref if_block, Some ( ref else_expr) ) = e. node {
219
224
func ( e, cond, if_block, else_expr) ;
220
225
}
221
226
} ,
222
- _ => { } ,
227
+ _ => { } ,
223
228
}
224
229
}
225
230
@@ -249,45 +254,37 @@ struct LintData<'a> {
249
254
250
255
const MSG_REDUNDANT_ELSE_BLOCK : & ' static str = "This else block is redundant.\n " ;
251
256
252
- const MSG_ELSE_BLOCK_NOT_NEEDED : & ' static str = "There is no need for an explicit `else` block for this `if` expression\n " ;
257
+ const MSG_ELSE_BLOCK_NOT_NEEDED : & ' static str = "There is no need for an explicit `else` block for this `if` \
258
+ expression\n ";
253
259
254
- const DROP_ELSE_BLOCK_AND_MERGE_MSG : & ' static str =
255
- "Consider dropping the else clause and merging the code that follows (in the loop) with the if block, like so:\n ";
260
+ const DROP_ELSE_BLOCK_AND_MERGE_MSG : & ' static str = "Consider dropping the else clause and merging the code that \
261
+ follows (in the loop) with the if block, like so:\n ";
256
262
257
- const DROP_ELSE_BLOCK_MSG : & ' static str =
258
- "Consider dropping the else clause, and moving out the code in the else block, like so:\n ";
263
+ const DROP_ELSE_BLOCK_MSG : & ' static str = "Consider dropping the else clause, and moving out the code in the else \
264
+ block, like so:\n ";
259
265
260
266
261
- fn emit_warning < ' a > ( ctx : & EarlyContext ,
262
- data : & ' a LintData ,
263
- header : & str ,
264
- typ : LintType ) {
267
+ fn emit_warning < ' a > ( ctx : & EarlyContext , data : & ' a LintData , header : & str , typ : LintType ) {
265
268
266
269
// snip is the whole *help* message that appears after the warning.
267
270
// message is the warning message.
268
271
// expr is the expression which the lint warning message refers to.
269
272
let ( snip, message, expr) = match typ {
270
273
LintType :: ContinueInsideElseBlock => {
271
- ( suggestion_snippet_for_continue_inside_else ( ctx, data, header) ,
272
- MSG_REDUNDANT_ELSE_BLOCK ,
273
- data. else_expr )
274
+ ( suggestion_snippet_for_continue_inside_else ( ctx, data, header) , MSG_REDUNDANT_ELSE_BLOCK , data. else_expr )
274
275
} ,
275
276
LintType :: ContinueInsideThenBlock => {
276
- ( suggestion_snippet_for_continue_inside_if ( ctx, data, header) ,
277
- MSG_ELSE_BLOCK_NOT_NEEDED ,
278
- data. if_expr )
279
- }
277
+ ( suggestion_snippet_for_continue_inside_if ( ctx, data, header) , MSG_ELSE_BLOCK_NOT_NEEDED , data. if_expr )
278
+ } ,
280
279
} ;
281
280
span_help_and_lint ( ctx, NEEDLESS_CONTINUE , expr. span , message, & snip) ;
282
281
}
283
282
284
- fn suggestion_snippet_for_continue_inside_if < ' a > ( ctx : & EarlyContext ,
285
- data : & ' a LintData ,
286
- header : & str ) -> String {
283
+ fn suggestion_snippet_for_continue_inside_if < ' a > ( ctx : & EarlyContext , data : & ' a LintData , header : & str ) -> String {
287
284
let cond_code = snippet ( ctx, data. if_cond . span , ".." ) ;
288
285
289
- let if_code = format ! ( "if {} {{\n continue;\n }}\n " , cond_code) ;
290
- /* ^^^^--- Four spaces of indentation. */
286
+ let if_code = format ! ( "if {} {{\n continue;\n }}\n " , cond_code) ;
287
+ /* ^^^^--- Four spaces of indentation. */
291
288
// region B
292
289
let else_code = snippet ( ctx, data. else_expr . span , ".." ) . into_owned ( ) ;
293
290
let else_code = erode_block ( & else_code) ;
@@ -300,12 +297,9 @@ fn suggestion_snippet_for_continue_inside_if<'a>(ctx: &EarlyContext,
300
297
ret
301
298
}
302
299
303
- fn suggestion_snippet_for_continue_inside_else < ' a > ( ctx : & EarlyContext ,
304
- data : & ' a LintData ,
305
- header : & str ) -> String
306
- {
300
+ fn suggestion_snippet_for_continue_inside_else < ' a > ( ctx : & EarlyContext , data : & ' a LintData , header : & str ) -> String {
307
301
let cond_code = snippet ( ctx, data. if_cond . span , ".." ) ;
308
- let mut if_code = format ! ( "if {} {{\n " , cond_code) ;
302
+ let mut if_code = format ! ( "if {} {{\n " , cond_code) ;
309
303
310
304
// Region B
311
305
let block_code = & snippet ( ctx, data. if_block . span , ".." ) . into_owned ( ) ;
@@ -318,13 +312,12 @@ fn suggestion_snippet_for_continue_inside_else<'a>(ctx: &EarlyContext,
318
312
// These is the code in the loop block that follows the if/else construction
319
313
// we are complaining about. We want to pull all of this code into the
320
314
// `then` block of the `if` statement.
321
- let to_annex = data. block_stmts [ data. stmt_idx +1 ..]
322
- . iter ( )
323
- . map ( |stmt| {
324
- original_sp ( stmt. span , DUMMY_SP )
325
- } )
326
- . map ( |span| snippet_block ( ctx, span, ".." ) . into_owned ( ) )
327
- . collect :: < Vec < _ > > ( ) . join ( "\n " ) ;
315
+ let to_annex = data. block_stmts [ data. stmt_idx + 1 ..]
316
+ . iter ( )
317
+ . map ( |stmt| original_sp ( stmt. span , DUMMY_SP ) )
318
+ . map ( |span| snippet_block ( ctx, span, ".." ) . into_owned ( ) )
319
+ . collect :: < Vec < _ > > ( )
320
+ . join ( "\n " ) ;
328
321
329
322
let mut ret = String :: from ( header) ;
330
323
@@ -336,24 +329,22 @@ fn suggestion_snippet_for_continue_inside_else<'a>(ctx: &EarlyContext,
336
329
}
337
330
338
331
fn check_and_warn < ' a > ( ctx : & EarlyContext , expr : & ' a ast:: Expr ) {
339
- with_loop_block ( expr, |loop_block| {
340
- for ( i, stmt) in loop_block. stmts . iter ( ) . enumerate ( ) {
341
- with_if_expr ( stmt, |if_expr, cond, then_block, else_expr| {
342
- let data = & LintData {
343
- stmt_idx : i,
344
- if_expr : if_expr,
345
- if_cond : cond,
346
- if_block : then_block,
347
- else_expr : else_expr,
348
- block_stmts : & loop_block. stmts ,
349
- } ;
350
- if needless_continue_in_else ( else_expr) {
351
- emit_warning ( ctx, data, DROP_ELSE_BLOCK_AND_MERGE_MSG , LintType :: ContinueInsideElseBlock ) ;
352
- } else if is_first_block_stmt_continue ( then_block) {
353
- emit_warning ( ctx, data, DROP_ELSE_BLOCK_MSG , LintType :: ContinueInsideThenBlock ) ;
354
- }
355
- } ) ;
356
- }
332
+ with_loop_block ( expr, |loop_block| for ( i, stmt) in loop_block. stmts . iter ( ) . enumerate ( ) {
333
+ with_if_expr ( stmt, |if_expr, cond, then_block, else_expr| {
334
+ let data = & LintData {
335
+ stmt_idx : i,
336
+ if_expr : if_expr,
337
+ if_cond : cond,
338
+ if_block : then_block,
339
+ else_expr : else_expr,
340
+ block_stmts : & loop_block. stmts ,
341
+ } ;
342
+ if needless_continue_in_else ( else_expr) {
343
+ emit_warning ( ctx, data, DROP_ELSE_BLOCK_AND_MERGE_MSG , LintType :: ContinueInsideElseBlock ) ;
344
+ } else if is_first_block_stmt_continue ( then_block) {
345
+ emit_warning ( ctx, data, DROP_ELSE_BLOCK_MSG , LintType :: ContinueInsideThenBlock ) ;
346
+ }
347
+ } ) ;
357
348
} ) ;
358
349
}
359
350
@@ -378,7 +369,7 @@ fn check_and_warn<'a>(ctx: &EarlyContext, expr: &'a ast::Expr) {
378
369
/// an empty string will be returned in that case.
379
370
pub fn erode_from_back ( s : & str ) -> String {
380
371
let mut ret = String :: from ( s) ;
381
- while ret. pop ( ) . map_or ( false , |c| c != '}' ) { }
372
+ while ret. pop ( ) . map_or ( false , |c| c != '}' ) { }
382
373
while let Some ( c) = ret. pop ( ) {
383
374
if !c. is_whitespace ( ) {
384
375
ret. push ( c) ;
@@ -409,10 +400,10 @@ pub fn erode_from_back(s: &str) -> String {
409
400
///
410
401
pub fn erode_from_front ( s : & str ) -> String {
411
402
s. chars ( )
412
- . skip_while ( |c| c. is_whitespace ( ) )
413
- . skip_while ( |c| * c == '{' )
414
- . skip_while ( |c| * c == '\n' )
415
- . collect :: < String > ( )
403
+ . skip_while ( |c| c. is_whitespace ( ) )
404
+ . skip_while ( |c| * c == '{' )
405
+ . skip_while ( |c| * c == '\n' )
406
+ . collect :: < String > ( )
416
407
}
417
408
418
409
/// If `s` contains the code for a block, delimited by braces, this function
0 commit comments