Skip to content

Commit 0ff5a6e

Browse files
authored
Rollup merge of #112316 - clubby789:unused-parens-space, r=compiler-errors
Ensure space is inserted after keyword in `unused_delims` Fixes #112276
2 parents ff43249 + 1fa7692 commit 0ff5a6e

File tree

4 files changed

+131
-30
lines changed

4 files changed

+131
-30
lines changed

Diff for: compiler/rustc_lint/src/unused.rs

+42-17
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,7 @@ trait UnusedDelimLint {
556556
followed_by_block: bool,
557557
left_pos: Option<BytePos>,
558558
right_pos: Option<BytePos>,
559+
is_kw: bool,
559560
);
560561

561562
fn is_expr_delims_necessary(
@@ -624,6 +625,7 @@ trait UnusedDelimLint {
624625
ctx: UnusedDelimsCtx,
625626
left_pos: Option<BytePos>,
626627
right_pos: Option<BytePos>,
628+
is_kw: bool,
627629
) {
628630
// If `value` has `ExprKind::Err`, unused delim lint can be broken.
629631
// For example, the following code caused ICE.
@@ -667,7 +669,7 @@ trait UnusedDelimLint {
667669
left_pos.is_some_and(|s| s >= value.span.lo()),
668670
right_pos.is_some_and(|s| s <= value.span.hi()),
669671
);
670-
self.emit_unused_delims(cx, value.span, spans, ctx.into(), keep_space);
672+
self.emit_unused_delims(cx, value.span, spans, ctx.into(), keep_space, is_kw);
671673
}
672674

673675
fn emit_unused_delims(
@@ -677,6 +679,7 @@ trait UnusedDelimLint {
677679
spans: Option<(Span, Span)>,
678680
msg: &str,
679681
keep_space: (bool, bool),
682+
is_kw: bool,
680683
) {
681684
let primary_span = if let Some((lo, hi)) = spans {
682685
if hi.is_empty() {
@@ -690,7 +693,7 @@ trait UnusedDelimLint {
690693
let suggestion = spans.map(|(lo, hi)| {
691694
let sm = cx.sess().source_map();
692695
let lo_replace =
693-
if keep_space.0 &&
696+
if (keep_space.0 || is_kw) &&
694697
let Ok(snip) = sm.span_to_prev_source(lo) && !snip.ends_with(' ') {
695698
" "
696699
} else {
@@ -720,15 +723,15 @@ trait UnusedDelimLint {
720723

721724
fn check_expr(&mut self, cx: &EarlyContext<'_>, e: &ast::Expr) {
722725
use rustc_ast::ExprKind::*;
723-
let (value, ctx, followed_by_block, left_pos, right_pos) = match e.kind {
726+
let (value, ctx, followed_by_block, left_pos, right_pos, is_kw) = match e.kind {
724727
// Do not lint `unused_braces` in `if let` expressions.
725728
If(ref cond, ref block, _)
726729
if !matches!(cond.kind, Let(_, _, _))
727730
|| Self::LINT_EXPR_IN_PATTERN_MATCHING_CTX =>
728731
{
729732
let left = e.span.lo() + rustc_span::BytePos(2);
730733
let right = block.span.lo();
731-
(cond, UnusedDelimsCtx::IfCond, true, Some(left), Some(right))
734+
(cond, UnusedDelimsCtx::IfCond, true, Some(left), Some(right), true)
732735
}
733736

734737
// Do not lint `unused_braces` in `while let` expressions.
@@ -738,27 +741,27 @@ trait UnusedDelimLint {
738741
{
739742
let left = e.span.lo() + rustc_span::BytePos(5);
740743
let right = block.span.lo();
741-
(cond, UnusedDelimsCtx::WhileCond, true, Some(left), Some(right))
744+
(cond, UnusedDelimsCtx::WhileCond, true, Some(left), Some(right), true)
742745
}
743746

744747
ForLoop(_, ref cond, ref block, ..) => {
745-
(cond, UnusedDelimsCtx::ForIterExpr, true, None, Some(block.span.lo()))
748+
(cond, UnusedDelimsCtx::ForIterExpr, true, None, Some(block.span.lo()), true)
746749
}
747750

748751
Match(ref head, _) if Self::LINT_EXPR_IN_PATTERN_MATCHING_CTX => {
749752
let left = e.span.lo() + rustc_span::BytePos(5);
750-
(head, UnusedDelimsCtx::MatchScrutineeExpr, true, Some(left), None)
753+
(head, UnusedDelimsCtx::MatchScrutineeExpr, true, Some(left), None, true)
751754
}
752755

753756
Ret(Some(ref value)) => {
754757
let left = e.span.lo() + rustc_span::BytePos(3);
755-
(value, UnusedDelimsCtx::ReturnValue, false, Some(left), None)
758+
(value, UnusedDelimsCtx::ReturnValue, false, Some(left), None, true)
756759
}
757760

758-
Index(_, ref value) => (value, UnusedDelimsCtx::IndexExpr, false, None, None),
761+
Index(_, ref value) => (value, UnusedDelimsCtx::IndexExpr, false, None, None, false),
759762

760763
Assign(_, ref value, _) | AssignOp(.., ref value) => {
761-
(value, UnusedDelimsCtx::AssignedValue, false, None, None)
764+
(value, UnusedDelimsCtx::AssignedValue, false, None, None, false)
762765
}
763766
// either function/method call, or something this lint doesn't care about
764767
ref call_or_other => {
@@ -778,12 +781,20 @@ trait UnusedDelimLint {
778781
return;
779782
}
780783
for arg in args_to_check {
781-
self.check_unused_delims_expr(cx, arg, ctx, false, None, None);
784+
self.check_unused_delims_expr(cx, arg, ctx, false, None, None, false);
782785
}
783786
return;
784787
}
785788
};
786-
self.check_unused_delims_expr(cx, &value, ctx, followed_by_block, left_pos, right_pos);
789+
self.check_unused_delims_expr(
790+
cx,
791+
&value,
792+
ctx,
793+
followed_by_block,
794+
left_pos,
795+
right_pos,
796+
is_kw,
797+
);
787798
}
788799

789800
fn check_stmt(&mut self, cx: &EarlyContext<'_>, s: &ast::Stmt) {
@@ -794,7 +805,7 @@ trait UnusedDelimLint {
794805
None => UnusedDelimsCtx::AssignedValue,
795806
Some(_) => UnusedDelimsCtx::AssignedValueLetElse,
796807
};
797-
self.check_unused_delims_expr(cx, init, ctx, false, None, None);
808+
self.check_unused_delims_expr(cx, init, ctx, false, None, None, false);
798809
}
799810
}
800811
StmtKind::Expr(ref expr) => {
@@ -805,6 +816,7 @@ trait UnusedDelimLint {
805816
false,
806817
None,
807818
None,
819+
false,
808820
);
809821
}
810822
_ => {}
@@ -824,6 +836,7 @@ trait UnusedDelimLint {
824836
false,
825837
None,
826838
None,
839+
false,
827840
);
828841
}
829842
}
@@ -879,6 +892,7 @@ impl UnusedDelimLint for UnusedParens {
879892
followed_by_block: bool,
880893
left_pos: Option<BytePos>,
881894
right_pos: Option<BytePos>,
895+
is_kw: bool,
882896
) {
883897
match value.kind {
884898
ast::ExprKind::Paren(ref inner) => {
@@ -893,7 +907,7 @@ impl UnusedDelimLint for UnusedParens {
893907
_,
894908
) if node.lazy()))
895909
{
896-
self.emit_unused_delims_expr(cx, value, ctx, left_pos, right_pos)
910+
self.emit_unused_delims_expr(cx, value, ctx, left_pos, right_pos, is_kw)
897911
}
898912
}
899913
ast::ExprKind::Let(_, ref expr, _) => {
@@ -904,6 +918,7 @@ impl UnusedDelimLint for UnusedParens {
904918
followed_by_block,
905919
None,
906920
None,
921+
false,
907922
);
908923
}
909924
_ => {}
@@ -942,7 +957,7 @@ impl UnusedParens {
942957
.span
943958
.find_ancestor_inside(value.span)
944959
.map(|inner| (value.span.with_hi(inner.lo()), value.span.with_lo(inner.hi())));
945-
self.emit_unused_delims(cx, value.span, spans, "pattern", keep_space);
960+
self.emit_unused_delims(cx, value.span, spans, "pattern", keep_space, false);
946961
}
947962
}
948963
}
@@ -967,6 +982,7 @@ impl EarlyLintPass for UnusedParens {
967982
true,
968983
None,
969984
None,
985+
true,
970986
);
971987
for stmt in &block.stmts {
972988
<Self as UnusedDelimLint>::check_stmt(self, cx, stmt);
@@ -985,6 +1001,7 @@ impl EarlyLintPass for UnusedParens {
9851001
false,
9861002
None,
9871003
None,
1004+
true,
9881005
);
9891006
}
9901007
}
@@ -1043,6 +1060,7 @@ impl EarlyLintPass for UnusedParens {
10431060
false,
10441061
None,
10451062
None,
1063+
false,
10461064
);
10471065
}
10481066
ast::TyKind::Paren(r) => {
@@ -1057,7 +1075,7 @@ impl EarlyLintPass for UnusedParens {
10571075
.find_ancestor_inside(ty.span)
10581076
.map(|r| (ty.span.with_hi(r.lo()), ty.span.with_lo(r.hi())));
10591077

1060-
self.emit_unused_delims(cx, ty.span, spans, "type", (false, false));
1078+
self.emit_unused_delims(cx, ty.span, spans, "type", (false, false), false);
10611079
}
10621080
}
10631081
self.with_self_ty_parens = false;
@@ -1130,6 +1148,7 @@ impl UnusedDelimLint for UnusedBraces {
11301148
followed_by_block: bool,
11311149
left_pos: Option<BytePos>,
11321150
right_pos: Option<BytePos>,
1151+
is_kw: bool,
11331152
) {
11341153
match value.kind {
11351154
ast::ExprKind::Block(ref inner, None)
@@ -1170,7 +1189,7 @@ impl UnusedDelimLint for UnusedBraces {
11701189
&& !value.span.from_expansion()
11711190
&& !inner.span.from_expansion()
11721191
{
1173-
self.emit_unused_delims_expr(cx, value, ctx, left_pos, right_pos)
1192+
self.emit_unused_delims_expr(cx, value, ctx, left_pos, right_pos, is_kw)
11741193
}
11751194
}
11761195
}
@@ -1183,6 +1202,7 @@ impl UnusedDelimLint for UnusedBraces {
11831202
followed_by_block,
11841203
None,
11851204
None,
1205+
false,
11861206
);
11871207
}
11881208
_ => {}
@@ -1207,6 +1227,7 @@ impl EarlyLintPass for UnusedBraces {
12071227
false,
12081228
None,
12091229
None,
1230+
false,
12101231
);
12111232
}
12121233
}
@@ -1220,6 +1241,7 @@ impl EarlyLintPass for UnusedBraces {
12201241
false,
12211242
None,
12221243
None,
1244+
false,
12231245
);
12241246
}
12251247
}
@@ -1233,6 +1255,7 @@ impl EarlyLintPass for UnusedBraces {
12331255
false,
12341256
None,
12351257
None,
1258+
false,
12361259
);
12371260
}
12381261
}
@@ -1247,6 +1270,7 @@ impl EarlyLintPass for UnusedBraces {
12471270
false,
12481271
None,
12491272
None,
1273+
false,
12501274
);
12511275
}
12521276

@@ -1258,6 +1282,7 @@ impl EarlyLintPass for UnusedBraces {
12581282
false,
12591283
None,
12601284
None,
1285+
false,
12611286
);
12621287
}
12631288

Diff for: tests/ui/lint/lint-unnecessary-parens.fixed

+8
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ pub fn passes_unused_parens_lint() -> &'static (dyn Trait) {
3535
panic!()
3636
}
3737

38+
pub fn parens_with_keyword(e: &[()]) -> i32 {
39+
if true {} //~ ERROR unnecessary parentheses around `if`
40+
while true {} //~ ERROR unnecessary parentheses around `while`
41+
for _ in e {} //~ ERROR unnecessary parentheses around `for`
42+
match 1 { _ => ()} //~ ERROR unnecessary parentheses around `match`
43+
return 1; //~ ERROR unnecessary parentheses around `return` value
44+
}
45+
3846
macro_rules! baz {
3947
($($foo:expr),+) => {
4048
($($foo),*)

Diff for: tests/ui/lint/lint-unnecessary-parens.rs

+8
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ pub fn passes_unused_parens_lint() -> &'static (dyn Trait) {
3535
panic!()
3636
}
3737

38+
pub fn parens_with_keyword(e: &[()]) -> i32 {
39+
if(true) {} //~ ERROR unnecessary parentheses around `if`
40+
while(true) {} //~ ERROR unnecessary parentheses around `while`
41+
for _ in(e) {} //~ ERROR unnecessary parentheses around `for`
42+
match(1) { _ => ()} //~ ERROR unnecessary parentheses around `match`
43+
return(1); //~ ERROR unnecessary parentheses around `return` value
44+
}
45+
3846
macro_rules! baz {
3947
($($foo:expr),+) => {
4048
($($foo),*)

0 commit comments

Comments
 (0)