@@ -581,10 +581,24 @@ trait UnusedDelimLint {
581
581
lint. set_arg ( "delim" , Self :: DELIM_STR ) ;
582
582
lint. set_arg ( "item" , msg) ;
583
583
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) ] ;
588
602
lint. multipart_suggestion (
589
603
fluent:: suggestion,
590
604
replacement,
@@ -781,6 +795,7 @@ impl UnusedParens {
781
795
value : & ast:: Pat ,
782
796
avoid_or : bool ,
783
797
avoid_mut : bool ,
798
+ keep_space : ( bool , bool ) ,
784
799
) {
785
800
use ast:: { BindingAnnotation , PatKind } ;
786
801
@@ -805,7 +820,7 @@ impl UnusedParens {
805
820
} else {
806
821
None
807
822
} ;
808
- self . emit_unused_delims ( cx, value. span , spans, "pattern" , ( false , false ) ) ;
823
+ self . emit_unused_delims ( cx, value. span , spans, "pattern" , keep_space ) ;
809
824
}
810
825
}
811
826
}
@@ -814,7 +829,7 @@ impl EarlyLintPass for UnusedParens {
814
829
fn check_expr ( & mut self , cx : & EarlyContext < ' _ > , e : & ast:: Expr ) {
815
830
match e. kind {
816
831
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 ) ) ;
818
833
}
819
834
// We ignore parens in cases like `if (((let Some(0) = Some(1))))` because we already
820
835
// handle a hard error for them during AST lowering in `lower_expr_mut`, but we still
@@ -858,40 +873,41 @@ impl EarlyLintPass for UnusedParens {
858
873
859
874
fn check_pat ( & mut self , cx : & EarlyContext < ' _ > , p : & ast:: Pat ) {
860
875
use ast:: { Mutability , PatKind :: * } ;
876
+ let keep_space = ( false , false ) ;
861
877
match & p. kind {
862
878
// Do not lint on `(..)` as that will result in the other arms being useless.
863
879
Paren ( _)
864
880
// The other cases do not contain sub-patterns.
865
881
| Wild | Rest | Lit ( ..) | MacCall ( ..) | Range ( ..) | Ident ( .., None ) | Path ( ..) => { } ,
866
882
// These are list-like patterns; parens can always be removed.
867
883
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 ) ;
869
885
} ,
870
886
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 ) ;
872
888
} ,
873
889
// 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 ) ,
875
891
// Avoid linting on `&(mut x)` as `&mut x` has a different meaning, #55342.
876
892
// 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 ) ,
878
894
}
879
895
}
880
896
881
897
fn check_stmt ( & mut self , cx : & EarlyContext < ' _ > , s : & ast:: Stmt ) {
882
898
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 ) ) ;
884
900
}
885
901
886
902
<Self as UnusedDelimLint >:: check_stmt ( self , cx, s)
887
903
}
888
904
889
905
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 ) ) ;
891
907
}
892
908
893
909
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 ) ) ;
895
911
}
896
912
897
913
fn check_ty ( & mut self , cx : & EarlyContext < ' _ > , ty : & ast:: Ty ) {
0 commit comments