@@ -565,10 +565,26 @@ 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_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) ] ;
572
588
lint. multipart_suggestion (
573
589
fluent:: suggestion,
574
590
replacement,
@@ -765,6 +781,7 @@ impl UnusedParens {
765
781
value : & ast:: Pat ,
766
782
avoid_or : bool ,
767
783
avoid_mut : bool ,
784
+ keep_space : ( bool , bool ) ,
768
785
) {
769
786
use ast:: { BindingAnnotation , PatKind } ;
770
787
@@ -789,7 +806,7 @@ impl UnusedParens {
789
806
} else {
790
807
None
791
808
} ;
792
- self . emit_unused_delims ( cx, value. span , spans, "pattern" , ( false , false ) ) ;
809
+ self . emit_unused_delims ( cx, value. span , spans, "pattern" , keep_space ) ;
793
810
}
794
811
}
795
812
}
@@ -798,7 +815,7 @@ impl EarlyLintPass for UnusedParens {
798
815
fn check_expr ( & mut self , cx : & EarlyContext < ' _ > , e : & ast:: Expr ) {
799
816
match e. kind {
800
817
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 ) ) ;
802
819
}
803
820
// We ignore parens in cases like `if (((let Some(0) = Some(1))))` because we already
804
821
// handle a hard error for them during AST lowering in `lower_expr_mut`, but we still
@@ -842,40 +859,41 @@ impl EarlyLintPass for UnusedParens {
842
859
843
860
fn check_pat ( & mut self , cx : & EarlyContext < ' _ > , p : & ast:: Pat ) {
844
861
use ast:: { Mutability , PatKind :: * } ;
862
+ let keep_space = ( false , false ) ;
845
863
match & p. kind {
846
864
// Do not lint on `(..)` as that will result in the other arms being useless.
847
865
Paren ( _)
848
866
// The other cases do not contain sub-patterns.
849
867
| Wild | Rest | Lit ( ..) | MacCall ( ..) | Range ( ..) | Ident ( .., None ) | Path ( ..) => { } ,
850
868
// These are list-like patterns; parens can always be removed.
851
869
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 ) ;
853
871
} ,
854
872
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 ) ;
856
874
} ,
857
875
// 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 ) ,
859
877
// Avoid linting on `&(mut x)` as `&mut x` has a different meaning, #55342.
860
878
// 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 ) ,
862
880
}
863
881
}
864
882
865
883
fn check_stmt ( & mut self , cx : & EarlyContext < ' _ > , s : & ast:: Stmt ) {
866
884
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 ) ) ;
868
886
}
869
887
870
888
<Self as UnusedDelimLint >:: check_stmt ( self , cx, s)
871
889
}
872
890
873
891
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 ) ) ;
875
893
}
876
894
877
895
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 ) ) ;
879
897
}
880
898
881
899
fn check_ty ( & mut self , cx : & EarlyContext < ' _ > , ty : & ast:: Ty ) {
0 commit comments