@@ -312,16 +312,16 @@ fn suggestion_snippet_for_continue_inside_if<'a>(ctx: &EarlyContext<'_>, data: &
312312 let cond_code = snippet ( ctx, data. if_cond . span , ".." ) ;
313313
314314 let continue_code = snippet_block ( ctx, data. if_block . span , ".." , Some ( data. if_expr . span ) ) ;
315- // region B
315+
316316 let else_code = snippet_block ( ctx, data. else_expr . span , ".." , Some ( data. if_expr . span ) ) ;
317317
318318 let indent_if = indent_of ( ctx, data. if_expr . span ) . unwrap_or ( 0 ) ;
319319 format ! (
320- "{}if {} {} {}" ,
321- " " . repeat( indent_if) ,
320+ "{indent}if {} {}\n {indent}{}" ,
322321 cond_code,
323322 continue_code,
324323 else_code,
324+ indent = " " . repeat( indent_if) ,
325325 )
326326}
327327
@@ -389,9 +389,9 @@ fn check_and_warn<'a>(ctx: &EarlyContext<'_>, expr: &'a ast::Expr) {
389389 } ) ;
390390}
391391
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.
395395///
396396/// ```rust
397397/// {
@@ -405,20 +405,21 @@ fn check_and_warn<'a>(ctx: &EarlyContext<'_>, expr: &'a ast::Expr) {
405405/// {
406406/// let x = 5;
407407/// ```
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.
411408#[ must_use]
412409fn erode_from_back ( s : & str ) -> String {
413- let mut ret = String :: from ( s ) ;
410+ let mut ret = s . to_string ( ) ;
414411 while ret. pop ( ) . map_or ( false , |c| c != '}' ) { }
415412 while let Some ( c) = ret. pop ( ) {
416413 if !c. is_whitespace ( ) {
417414 ret. push ( c) ;
418415 break ;
419416 }
420417 }
421- ret
418+ if ret. is_empty ( ) {
419+ s. to_string ( )
420+ } else {
421+ ret
422+ }
422423}
423424
424425fn 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> {
428429#[ cfg( test) ]
429430mod test {
430431 use super :: erode_from_back;
432+
431433 #[ test]
432434 #[ rustfmt:: skip]
433435 fn test_erode_from_back ( ) {
@@ -453,7 +455,7 @@ mod test {
453455 let x = 5;
454456let y = something();
455457" ;
456- let expected = "" ;
458+ let expected = input ;
457459 let got = erode_from_back ( input) ;
458460 assert_eq ! ( expected, got) ;
459461 }
0 commit comments