Skip to content

Do not suggest adding a semicolon after ? #87061

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions compiler/rustc_typeck/src/check/coercion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1456,11 +1456,15 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
expected.is_unit(),
pointing_at_return_type,
) {
// If the block is from an external macro, then do not suggest
// adding a semicolon, because there's nowhere to put it.
// See issue #81943.
// If the block is from an external macro or try (`?`) desugaring, then
// do not suggest adding a semicolon, because there's nowhere to put it.
// See issues #81943 and #87051.
if cond_expr.span.desugaring_kind().is_none()
&& !in_external_macro(fcx.tcx.sess, cond_expr.span)
&& !matches!(
cond_expr.kind,
hir::ExprKind::Match(.., hir::MatchSource::TryDesugar)
)
{
err.span_label(cond_expr.span, "expected this to be `()`");
if expr.can_have_side_effects() {
Expand Down
27 changes: 27 additions & 0 deletions src/test/ui/suggestions/try-operator-dont-suggest-semicolon.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Regression test for #87051, where a double semicolon was erroneously
// suggested after a `?` operator.

fn main() -> Result<(), ()> {
a(|| {
b()
//~^ ERROR: mismatched types [E0308]
//~| NOTE: expected `()`, found `i32`
//~| HELP: consider using a semicolon here
})?;

// Here, we do want to suggest a semicolon:
let x = Ok(42);
if true {
//~^ NOTE: expected this to be `()`
x?
//~^ ERROR: mismatched types [E0308]
//~| NOTE: expected `()`, found integer
//~| HELP: consider using a semicolon here
}
//~^ HELP: consider using a semicolon here

Ok(())
}

fn a<F>(f: F) -> Result<(), ()> where F: FnMut() { Ok(()) }
fn b() -> i32 { 42 }
33 changes: 33 additions & 0 deletions src/test/ui/suggestions/try-operator-dont-suggest-semicolon.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
error[E0308]: mismatched types
--> $DIR/try-operator-dont-suggest-semicolon.rs:6:9
|
LL | b()
| ^^^- help: consider using a semicolon here: `;`
| |
| expected `()`, found `i32`

error[E0308]: mismatched types
--> $DIR/try-operator-dont-suggest-semicolon.rs:16:9
|
LL | / if true {
LL | |
LL | | x?
| | ^^ expected `()`, found integer
LL | |
LL | |
LL | |
LL | | }
| |_____- expected this to be `()`
|
help: consider using a semicolon here
|
LL | x?;
| ^
help: consider using a semicolon here
|
LL | };
| ^

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0308`.