@@ -581,10 +581,24 @@ trait UnusedDelimLint {
581581 lint. set_arg ( "delim" , Self :: DELIM_STR ) ;
582582 lint. set_arg ( "item" , msg) ;
583583 if let Some ( ( lo, hi) ) = spans {
584- let replacement = vec ! [
585- ( lo, if keep_space. 0 { " " . into( ) } else { "" . into( ) } ) ,
586- ( hi, if keep_space. 1 { " " . into( ) } else { "" . into( ) } ) ,
587- ] ;
584+ let sm = cx. sess ( ) . source_map ( ) ;
585+ let lo_replace =
586+ if keep_space. 0 &&
587+ let Ok ( snip) = sm. span_to_prev_source ( lo) && !snip. ends_with ( " " ) {
588+ " " . to_string ( )
589+ } else {
590+ "" . to_string ( )
591+ } ;
592+
593+ let hi_replace =
594+ if keep_space. 1 &&
595+ let Ok ( snip) = sm. span_to_next_source ( hi) && !snip. starts_with ( " " ) {
596+ " " . to_string ( )
597+ } else {
598+ "" . to_string ( )
599+ } ;
600+
601+ let replacement = vec ! [ ( lo, lo_replace) , ( hi, hi_replace) ] ;
588602 lint. multipart_suggestion (
589603 fluent:: suggestion,
590604 replacement,
@@ -781,6 +795,7 @@ impl UnusedParens {
781795 value : & ast:: Pat ,
782796 avoid_or : bool ,
783797 avoid_mut : bool ,
798+ keep_space : ( bool , bool ) ,
784799 ) {
785800 use ast:: { BindingAnnotation , PatKind } ;
786801
@@ -805,7 +820,7 @@ impl UnusedParens {
805820 } else {
806821 None
807822 } ;
808- self . emit_unused_delims ( cx, value. span , spans, "pattern" , ( false , false ) ) ;
823+ self . emit_unused_delims ( cx, value. span , spans, "pattern" , keep_space ) ;
809824 }
810825 }
811826}
@@ -814,7 +829,7 @@ impl EarlyLintPass for UnusedParens {
814829 fn check_expr ( & mut self , cx : & EarlyContext < ' _ > , e : & ast:: Expr ) {
815830 match e. kind {
816831 ExprKind :: Let ( ref pat, _, _) | ExprKind :: ForLoop ( ref pat, ..) => {
817- self . check_unused_parens_pat ( cx, pat, false , false ) ;
832+ self . check_unused_parens_pat ( cx, pat, false , false , ( true , true ) ) ;
818833 }
819834 // We ignore parens in cases like `if (((let Some(0) = Some(1))))` because we already
820835 // handle a hard error for them during AST lowering in `lower_expr_mut`, but we still
@@ -858,40 +873,41 @@ impl EarlyLintPass for UnusedParens {
858873
859874 fn check_pat ( & mut self , cx : & EarlyContext < ' _ > , p : & ast:: Pat ) {
860875 use ast:: { Mutability , PatKind :: * } ;
876+ let keep_space = ( false , false ) ;
861877 match & p. kind {
862878 // Do not lint on `(..)` as that will result in the other arms being useless.
863879 Paren ( _)
864880 // The other cases do not contain sub-patterns.
865881 | Wild | Rest | Lit ( ..) | MacCall ( ..) | Range ( ..) | Ident ( .., None ) | Path ( ..) => { } ,
866882 // These are list-like patterns; parens can always be removed.
867883 TupleStruct ( _, _, ps) | Tuple ( ps) | Slice ( ps) | Or ( ps) => for p in ps {
868- self . check_unused_parens_pat ( cx, p, false , false ) ;
884+ self . check_unused_parens_pat ( cx, p, false , false , keep_space ) ;
869885 } ,
870886 Struct ( _, _, fps, _) => for f in fps {
871- self . check_unused_parens_pat ( cx, & f. pat , false , false ) ;
887+ self . check_unused_parens_pat ( cx, & f. pat , false , false , keep_space ) ;
872888 } ,
873889 // Avoid linting on `i @ (p0 | .. | pn)` and `box (p0 | .. | pn)`, #64106.
874- Ident ( .., Some ( p) ) | Box ( p) => self . check_unused_parens_pat ( cx, p, true , false ) ,
890+ Ident ( .., Some ( p) ) | Box ( p) => self . check_unused_parens_pat ( cx, p, true , false , keep_space ) ,
875891 // Avoid linting on `&(mut x)` as `&mut x` has a different meaning, #55342.
876892 // Also avoid linting on `& mut? (p0 | .. | pn)`, #64106.
877- Ref ( p, m) => self . check_unused_parens_pat ( cx, p, true , * m == Mutability :: Not ) ,
893+ Ref ( p, m) => self . check_unused_parens_pat ( cx, p, true , * m == Mutability :: Not , keep_space ) ,
878894 }
879895 }
880896
881897 fn check_stmt ( & mut self , cx : & EarlyContext < ' _ > , s : & ast:: Stmt ) {
882898 if let StmtKind :: Local ( ref local) = s. kind {
883- self . check_unused_parens_pat ( cx, & local. pat , true , false ) ;
899+ self . check_unused_parens_pat ( cx, & local. pat , true , false , ( false , false ) ) ;
884900 }
885901
886902 <Self as UnusedDelimLint >:: check_stmt ( self , cx, s)
887903 }
888904
889905 fn check_param ( & mut self , cx : & EarlyContext < ' _ > , param : & ast:: Param ) {
890- self . check_unused_parens_pat ( cx, & param. pat , true , false ) ;
906+ self . check_unused_parens_pat ( cx, & param. pat , true , false , ( false , false ) ) ;
891907 }
892908
893909 fn check_arm ( & mut self , cx : & EarlyContext < ' _ > , arm : & ast:: Arm ) {
894- self . check_unused_parens_pat ( cx, & arm. pat , false , false ) ;
910+ self . check_unused_parens_pat ( cx, & arm. pat , false , false , ( false , false ) ) ;
895911 }
896912
897913 fn check_ty ( & mut self , cx : & EarlyContext < ' _ > , ty : & ast:: Ty ) {
0 commit comments