@@ -312,16 +312,16 @@ fn suggestion_snippet_for_continue_inside_if<'a>(ctx: &EarlyContext<'_>, data: &
312
312
let cond_code = snippet ( ctx, data. if_cond . span , ".." ) ;
313
313
314
314
let continue_code = snippet_block ( ctx, data. if_block . span , ".." , Some ( data. if_expr . span ) ) ;
315
- // region B
315
+
316
316
let else_code = snippet_block ( ctx, data. else_expr . span , ".." , Some ( data. if_expr . span ) ) ;
317
317
318
318
let indent_if = indent_of ( ctx, data. if_expr . span ) . unwrap_or ( 0 ) ;
319
319
format ! (
320
- "{}if {} {} {}" ,
321
- " " . repeat( indent_if) ,
320
+ "{indent}if {} {}\n {indent}{}" ,
322
321
cond_code,
323
322
continue_code,
324
323
else_code,
324
+ indent = " " . repeat( indent_if) ,
325
325
)
326
326
}
327
327
@@ -389,9 +389,9 @@ fn check_and_warn<'a>(ctx: &EarlyContext<'_>, expr: &'a ast::Expr) {
389
389
} ) ;
390
390
}
391
391
392
- /// Eats at `s` from the end till a closing brace `}` is encountered, and then
393
- /// continues eating till a non-whitespace character is found.
394
- /// e.g., the string
392
+ /// Eats at `s` from the end till a closing brace `}` is encountered, and then continues eating
393
+ /// till a non-whitespace character is found. e.g., the string. If no closing `}` is present, the
394
+ /// string will be preserved.
395
395
///
396
396
/// ```rust
397
397
/// {
@@ -405,20 +405,21 @@ fn check_and_warn<'a>(ctx: &EarlyContext<'_>, expr: &'a ast::Expr) {
405
405
/// {
406
406
/// let x = 5;
407
407
/// ```
408
- ///
409
- /// NOTE: when there is no closing brace in `s`, `s` is _not_ preserved, i.e.,
410
- /// an empty string will be returned in that case.
411
408
#[ must_use]
412
409
fn erode_from_back ( s : & str ) -> String {
413
- let mut ret = String :: from ( s ) ;
410
+ let mut ret = s . to_string ( ) ;
414
411
while ret. pop ( ) . map_or ( false , |c| c != '}' ) { }
415
412
while let Some ( c) = ret. pop ( ) {
416
413
if !c. is_whitespace ( ) {
417
414
ret. push ( c) ;
418
415
break ;
419
416
}
420
417
}
421
- ret
418
+ if ret. is_empty ( ) {
419
+ s. to_string ( )
420
+ } else {
421
+ ret
422
+ }
422
423
}
423
424
424
425
fn span_of_first_expr_in_block ( block : & ast:: Block ) -> Option < Span > {
@@ -428,6 +429,7 @@ fn span_of_first_expr_in_block(block: &ast::Block) -> Option<Span> {
428
429
#[ cfg( test) ]
429
430
mod test {
430
431
use super :: erode_from_back;
432
+
431
433
#[ test]
432
434
#[ rustfmt:: skip]
433
435
fn test_erode_from_back ( ) {
@@ -453,7 +455,7 @@ mod test {
453
455
let x = 5;
454
456
let y = something();
455
457
" ;
456
- let expected = "" ;
458
+ let expected = input ;
457
459
let got = erode_from_back ( input) ;
458
460
assert_eq ! ( expected, got) ;
459
461
}
0 commit comments