@@ -565,10 +565,26 @@ trait UnusedDelimLint {
565565 lint. set_arg ( "delim" , Self :: DELIM_STR ) ;
566566 lint. set_arg ( "item" , msg) ;
567567 if let Some ( ( lo, hi) ) = spans {
568- let replacement = vec ! [
569- ( lo, if keep_space. 0 { " " . into( ) } else { "" . into( ) } ) ,
570- ( hi, if keep_space. 1 { " " . into( ) } else { "" . into( ) } ) ,
571- ] ;
568+ let sm = cx. sess ( ) . source_map ( ) ;
569+ let lo_replace =
570+ if keep_space. 0 &&
571+ let Ok ( snip) = sm. span_to_snippet ( lo. with_lo ( lo. lo ( ) - BytePos ( 1 ) ) ) &&
572+ !snip. starts_with ( " " ) {
573+ " " . to_string ( )
574+ } else {
575+ "" . to_string ( )
576+ } ;
577+
578+ let hi_replace =
579+ if keep_space. 1 &&
580+ let Ok ( snip) = sm. span_to_snippet ( sm. next_point ( hi) ) &&
581+ !snip. starts_with ( " " ) {
582+ " " . to_string ( )
583+ } else {
584+ "" . to_string ( )
585+ } ;
586+
587+ let replacement = vec ! [ ( lo, lo_replace) , ( hi, hi_replace) ] ;
572588 lint. multipart_suggestion (
573589 fluent:: suggestion,
574590 replacement,
@@ -765,6 +781,7 @@ impl UnusedParens {
765781 value : & ast:: Pat ,
766782 avoid_or : bool ,
767783 avoid_mut : bool ,
784+ keep_space : ( bool , bool ) ,
768785 ) {
769786 use ast:: { BindingAnnotation , PatKind } ;
770787
@@ -789,7 +806,7 @@ impl UnusedParens {
789806 } else {
790807 None
791808 } ;
792- self . emit_unused_delims ( cx, value. span , spans, "pattern" , ( false , false ) ) ;
809+ self . emit_unused_delims ( cx, value. span , spans, "pattern" , keep_space ) ;
793810 }
794811 }
795812}
@@ -798,7 +815,7 @@ impl EarlyLintPass for UnusedParens {
798815 fn check_expr ( & mut self , cx : & EarlyContext < ' _ > , e : & ast:: Expr ) {
799816 match e. kind {
800817 ExprKind :: Let ( ref pat, _, _) | ExprKind :: ForLoop ( ref pat, ..) => {
801- self . check_unused_parens_pat ( cx, pat, false , false ) ;
818+ self . check_unused_parens_pat ( cx, pat, false , false , ( true , true ) ) ;
802819 }
803820 // We ignore parens in cases like `if (((let Some(0) = Some(1))))` because we already
804821 // handle a hard error for them during AST lowering in `lower_expr_mut`, but we still
@@ -842,40 +859,41 @@ impl EarlyLintPass for UnusedParens {
842859
843860 fn check_pat ( & mut self , cx : & EarlyContext < ' _ > , p : & ast:: Pat ) {
844861 use ast:: { Mutability , PatKind :: * } ;
862+ let keep_space = ( false , false ) ;
845863 match & p. kind {
846864 // Do not lint on `(..)` as that will result in the other arms being useless.
847865 Paren ( _)
848866 // The other cases do not contain sub-patterns.
849867 | Wild | Rest | Lit ( ..) | MacCall ( ..) | Range ( ..) | Ident ( .., None ) | Path ( ..) => { } ,
850868 // These are list-like patterns; parens can always be removed.
851869 TupleStruct ( _, _, ps) | Tuple ( ps) | Slice ( ps) | Or ( ps) => for p in ps {
852- self . check_unused_parens_pat ( cx, p, false , false ) ;
870+ self . check_unused_parens_pat ( cx, p, false , false , keep_space ) ;
853871 } ,
854872 Struct ( _, _, fps, _) => for f in fps {
855- self . check_unused_parens_pat ( cx, & f. pat , false , false ) ;
873+ self . check_unused_parens_pat ( cx, & f. pat , false , false , keep_space ) ;
856874 } ,
857875 // Avoid linting on `i @ (p0 | .. | pn)` and `box (p0 | .. | pn)`, #64106.
858- Ident ( .., Some ( p) ) | Box ( p) => self . check_unused_parens_pat ( cx, p, true , false ) ,
876+ Ident ( .., Some ( p) ) | Box ( p) => self . check_unused_parens_pat ( cx, p, true , false , keep_space ) ,
859877 // Avoid linting on `&(mut x)` as `&mut x` has a different meaning, #55342.
860878 // Also avoid linting on `& mut? (p0 | .. | pn)`, #64106.
861- Ref ( p, m) => self . check_unused_parens_pat ( cx, p, true , * m == Mutability :: Not ) ,
879+ Ref ( p, m) => self . check_unused_parens_pat ( cx, p, true , * m == Mutability :: Not , keep_space ) ,
862880 }
863881 }
864882
865883 fn check_stmt ( & mut self , cx : & EarlyContext < ' _ > , s : & ast:: Stmt ) {
866884 if let StmtKind :: Local ( ref local) = s. kind {
867- self . check_unused_parens_pat ( cx, & local. pat , true , false ) ;
885+ self . check_unused_parens_pat ( cx, & local. pat , true , false , ( false , false ) ) ;
868886 }
869887
870888 <Self as UnusedDelimLint >:: check_stmt ( self , cx, s)
871889 }
872890
873891 fn check_param ( & mut self , cx : & EarlyContext < ' _ > , param : & ast:: Param ) {
874- self . check_unused_parens_pat ( cx, & param. pat , true , false ) ;
892+ self . check_unused_parens_pat ( cx, & param. pat , true , false , ( false , false ) ) ;
875893 }
876894
877895 fn check_arm ( & mut self , cx : & EarlyContext < ' _ > , arm : & ast:: Arm ) {
878- self . check_unused_parens_pat ( cx, & arm. pat , false , false ) ;
896+ self . check_unused_parens_pat ( cx, & arm. pat , false , false , ( false , false ) ) ;
879897 }
880898
881899 fn check_ty ( & mut self , cx : & EarlyContext < ' _ > , ty : & ast:: Ty ) {
0 commit comments