Skip to content

Commit c315891

Browse files
committed
Auto merge of #157175 - Unique-Usman:ua/syntatic, r=JonathanBrouwer,Kivooeo
Don't suggest semicolon after if expression closing brace When a type mismatch occurs inside an if expression, the compiler incorrectly suggests adding a semicolon after the closing brace. This doesn't fix the error. The fix excludes all if expressions from the suggest_semicolon_at_end suggestion in coercion.rs. Fix: #156621
2 parents f375177 + fc7fa89 commit c315891

7 files changed

Lines changed: 11 additions & 56 deletions

compiler/rustc_hir_typeck/src/coercion.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2051,7 +2051,10 @@ impl<'tcx> CoerceMany<'tcx> {
20512051
err.span_label(cond_expr.span, "expected this to be `()`");
20522052
}
20532053
if expr.can_have_side_effects() {
2054-
fcx.suggest_semicolon_at_end(cond_expr.span, &mut err);
2054+
// Don't suggest semicolon after if expressions as it does not fix the issue
2055+
if !matches!(cond_expr.kind, hir::ExprKind::If(..)) {
2056+
fcx.suggest_semicolon_at_end(cond_expr.span, &mut err);
2057+
}
20552058
}
20562059
}
20572060
}

tests/ui/parser/expr-as-stmt-2.stderr

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,6 @@ help: parentheses are required to parse this as an expression
136136
|
137137
LL | (if true { bar() } else { bar() }) && if true { bar() } else { bar() };
138138
| + +
139-
help: consider using a semicolon here
140-
|
141-
LL | if true { bar() } else { bar() }; && if true { bar() } else { bar() };
142-
| +
143139

144140
error[E0308]: mismatched types
145141
--> $DIR/expr-as-stmt-2.rs:24:30
@@ -154,46 +150,26 @@ help: parentheses are required to parse this as an expression
154150
|
155151
LL | (if true { bar() } else { bar() }) && if true { bar() } else { bar() };
156152
| + +
157-
help: consider using a semicolon here
158-
|
159-
LL | if true { bar() } else { bar() }; && if true { bar() } else { bar() };
160-
| +
161153

162154
error[E0308]: mismatched types
163155
--> $DIR/expr-as-stmt-2.rs:27:15
164156
|
165157
LL | if true { bar() } else { bar() } if true { bar() } else { bar() };
166158
| ----------^^^^^-----------------
167-
| | |
159+
| | | |
160+
| | | help: consider using a semicolon here: `;`
168161
| | expected `()`, found `bool`
169162
| expected this to be `()`
170-
|
171-
help: consider using a semicolon here
172-
|
173-
LL | if true { bar(); } else { bar() } if true { bar() } else { bar() };
174-
| +
175-
help: consider using a semicolon here
176-
|
177-
LL | if true { bar() } else { bar() }; if true { bar() } else { bar() };
178-
| +
179163

180164
error[E0308]: mismatched types
181165
--> $DIR/expr-as-stmt-2.rs:27:30
182166
|
183167
LL | if true { bar() } else { bar() } if true { bar() } else { bar() };
184168
| -------------------------^^^^^--
185-
| | |
169+
| | | |
170+
| | | help: consider using a semicolon here: `;`
186171
| | expected `()`, found `bool`
187172
| expected this to be `()`
188-
|
189-
help: consider using a semicolon here
190-
|
191-
LL | if true { bar() } else { bar(); } if true { bar() } else { bar() };
192-
| +
193-
help: consider using a semicolon here
194-
|
195-
LL | if true { bar() } else { bar() }; if true { bar() } else { bar() };
196-
| +
197173

198174
error: aborting due to 13 previous errors
199175

tests/ui/return/issue-82612-return-mutable-reference.stderr

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@ help: consider using a semicolon here
1414
|
1515
LL | value.get_or_insert_with(func);
1616
| +
17-
help: consider using a semicolon here
18-
|
19-
LL | };
20-
| +
2117
help: you might have meant to return this value
2218
|
2319
LL | return value.get_or_insert_with(func);

tests/ui/suggestions/try-operator-dont-suggest-semicolon.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ fn main() -> Result<(), ()> {
1818
//~| NOTE: expected `()`, found integer
1919
//~| HELP: consider using a semicolon here
2020
}
21-
//~^ HELP: consider using a semicolon here
2221

2322
Ok(())
2423
}

tests/ui/suggestions/try-operator-dont-suggest-semicolon.stderr

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,12 @@ error[E0308]: mismatched types
1212
LL | / if true {
1313
LL | |
1414
LL | | x?
15-
| | ^^ expected `()`, found integer
15+
| | ^^- help: consider using a semicolon here: `;`
16+
| | |
17+
| | expected `()`, found integer
1618
... |
1719
LL | | }
1820
| |_____- `if` expressions without `else` arms expect their inner expression to be `()`
19-
|
20-
help: consider using a semicolon here
21-
|
22-
LL | x?;
23-
| +
24-
help: consider using a semicolon here
25-
|
26-
LL | };
27-
| +
2821

2922
error: aborting due to 2 previous errors
3023

tests/ui/typeck/issue-112007-leaked-writeln-macro-internals.stderr

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@ LL | | }
1212
|
1313
= note: expected unit type `()`
1414
found enum `Result<(), std::fmt::Error>`
15-
help: consider using a semicolon here
16-
|
17-
LL | };
18-
| +
1915
help: you might have meant to return this value
2016
|
2117
LL | return writeln!(w, "but not here");
@@ -42,10 +38,6 @@ LL | | }
4238
= note: expected unit type `()`
4339
found enum `Result<(), std::fmt::Error>`
4440
= note: this error originates in the macro `writeln` which comes from the expansion of the macro `baz` (in Nightly builds, run with -Z macro-backtrace for more info)
45-
help: consider using a semicolon here
46-
|
47-
LL | };
48-
| +
4941
help: you might have meant to return this value
5042
|
5143
LL | return baz!(w);

tests/ui/typeck/question-mark-operator-suggestion-span.stderr

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@ LL | | }
1212
|
1313
= note: expected unit type `()`
1414
found enum `Result<(), std::fmt::Error>`
15-
help: consider using a semicolon here
16-
|
17-
LL | };
18-
| +
1915
help: you might have meant to return this value
2016
|
2117
LL | return writeln!(w, "but not here");

0 commit comments

Comments
 (0)