@@ -565,10 +565,24 @@ trait UnusedDelimLint {
565
565
lint. set_arg ( "delim" , Self :: DELIM_STR ) ;
566
566
lint. set_arg ( "item" , msg) ;
567
567
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_prev_source ( lo) && !snip. ends_with ( " " ) {
572
+ " " . to_string ( )
573
+ } else {
574
+ "" . to_string ( )
575
+ } ;
576
+
577
+ let hi_replace =
578
+ if keep_space. 1 &&
579
+ let Ok ( snip) = sm. span_to_next_source ( hi) && !snip. starts_with ( " " ) {
580
+ " " . to_string ( )
581
+ } else {
582
+ "" . to_string ( )
583
+ } ;
584
+
585
+ let replacement = vec ! [ ( lo, lo_replace) , ( hi, hi_replace) ] ;
572
586
lint. multipart_suggestion (
573
587
fluent:: suggestion,
574
588
replacement,
@@ -765,6 +779,7 @@ impl UnusedParens {
765
779
value : & ast:: Pat ,
766
780
avoid_or : bool ,
767
781
avoid_mut : bool ,
782
+ keep_space : ( bool , bool ) ,
768
783
) {
769
784
use ast:: { BindingAnnotation , PatKind } ;
770
785
@@ -789,7 +804,7 @@ impl UnusedParens {
789
804
} else {
790
805
None
791
806
} ;
792
- self . emit_unused_delims ( cx, value. span , spans, "pattern" , ( false , false ) ) ;
807
+ self . emit_unused_delims ( cx, value. span , spans, "pattern" , keep_space ) ;
793
808
}
794
809
}
795
810
}
@@ -798,7 +813,7 @@ impl EarlyLintPass for UnusedParens {
798
813
fn check_expr ( & mut self , cx : & EarlyContext < ' _ > , e : & ast:: Expr ) {
799
814
match e. kind {
800
815
ExprKind :: Let ( ref pat, _, _) | ExprKind :: ForLoop ( ref pat, ..) => {
801
- self . check_unused_parens_pat ( cx, pat, false , false ) ;
816
+ self . check_unused_parens_pat ( cx, pat, false , false , ( true , true ) ) ;
802
817
}
803
818
// We ignore parens in cases like `if (((let Some(0) = Some(1))))` because we already
804
819
// handle a hard error for them during AST lowering in `lower_expr_mut`, but we still
@@ -842,40 +857,41 @@ impl EarlyLintPass for UnusedParens {
842
857
843
858
fn check_pat ( & mut self , cx : & EarlyContext < ' _ > , p : & ast:: Pat ) {
844
859
use ast:: { Mutability , PatKind :: * } ;
860
+ let keep_space = ( false , false ) ;
845
861
match & p. kind {
846
862
// Do not lint on `(..)` as that will result in the other arms being useless.
847
863
Paren ( _)
848
864
// The other cases do not contain sub-patterns.
849
865
| Wild | Rest | Lit ( ..) | MacCall ( ..) | Range ( ..) | Ident ( .., None ) | Path ( ..) => { } ,
850
866
// These are list-like patterns; parens can always be removed.
851
867
TupleStruct ( _, _, ps) | Tuple ( ps) | Slice ( ps) | Or ( ps) => for p in ps {
852
- self . check_unused_parens_pat ( cx, p, false , false ) ;
868
+ self . check_unused_parens_pat ( cx, p, false , false , keep_space ) ;
853
869
} ,
854
870
Struct ( _, _, fps, _) => for f in fps {
855
- self . check_unused_parens_pat ( cx, & f. pat , false , false ) ;
871
+ self . check_unused_parens_pat ( cx, & f. pat , false , false , keep_space ) ;
856
872
} ,
857
873
// 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 ) ,
874
+ Ident ( .., Some ( p) ) | Box ( p) => self . check_unused_parens_pat ( cx, p, true , false , keep_space ) ,
859
875
// Avoid linting on `&(mut x)` as `&mut x` has a different meaning, #55342.
860
876
// Also avoid linting on `& mut? (p0 | .. | pn)`, #64106.
861
- Ref ( p, m) => self . check_unused_parens_pat ( cx, p, true , * m == Mutability :: Not ) ,
877
+ Ref ( p, m) => self . check_unused_parens_pat ( cx, p, true , * m == Mutability :: Not , keep_space ) ,
862
878
}
863
879
}
864
880
865
881
fn check_stmt ( & mut self , cx : & EarlyContext < ' _ > , s : & ast:: Stmt ) {
866
882
if let StmtKind :: Local ( ref local) = s. kind {
867
- self . check_unused_parens_pat ( cx, & local. pat , true , false ) ;
883
+ self . check_unused_parens_pat ( cx, & local. pat , true , false , ( false , false ) ) ;
868
884
}
869
885
870
886
<Self as UnusedDelimLint >:: check_stmt ( self , cx, s)
871
887
}
872
888
873
889
fn check_param ( & mut self , cx : & EarlyContext < ' _ > , param : & ast:: Param ) {
874
- self . check_unused_parens_pat ( cx, & param. pat , true , false ) ;
890
+ self . check_unused_parens_pat ( cx, & param. pat , true , false , ( false , false ) ) ;
875
891
}
876
892
877
893
fn check_arm ( & mut self , cx : & EarlyContext < ' _ > , arm : & ast:: Arm ) {
878
- self . check_unused_parens_pat ( cx, & arm. pat , false , false ) ;
894
+ self . check_unused_parens_pat ( cx, & arm. pat , false , false , ( false , false ) ) ;
879
895
}
880
896
881
897
fn check_ty ( & mut self , cx : & EarlyContext < ' _ > , ty : & ast:: Ty ) {
0 commit comments