From ca72352e604408142c96a16e1ab9fed4916453de Mon Sep 17 00:00:00 2001 From: mibac138 <5672750+mibac138@users.noreply.github.com> Date: Mon, 4 May 2020 18:33:05 +0200 Subject: [PATCH] Suggest removing semicolon in last expression only if it's type is known --- src/librustc_typeck/check/mod.rs | 4 +++- src/test/ui/issues/issue-43162.stderr | 3 --- src/test/ui/typeck/issue-67971.rs | 9 +++++++++ src/test/ui/typeck/issue-67971.stderr | 18 ++++++++++++++++++ 4 files changed, 30 insertions(+), 4 deletions(-) create mode 100644 src/test/ui/typeck/issue-67971.rs create mode 100644 src/test/ui/typeck/issue-67971.stderr diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index c431745198822..b8f1fb00de9ef 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -5387,7 +5387,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { _ => return None, }; let last_expr_ty = self.node_ty(last_expr.hir_id); - if self.can_sub(self.param_env, last_expr_ty, expected_ty).is_err() { + if matches!(last_expr_ty.kind, ty::Error) + || self.can_sub(self.param_env, last_expr_ty, expected_ty).is_err() + { return None; } let original_span = original_sp(last_stmt.span, blk.span); diff --git a/src/test/ui/issues/issue-43162.stderr b/src/test/ui/issues/issue-43162.stderr index 0ed3d27c65b6e..a443db40732ac 100644 --- a/src/test/ui/issues/issue-43162.stderr +++ b/src/test/ui/issues/issue-43162.stderr @@ -17,9 +17,6 @@ LL | fn foo() -> bool { | --- ^^^^ expected `bool`, found `()` | | | implicitly returns `()` as its body has no tail or `return` expression -LL | -LL | break true; - | - help: consider removing this semicolon error: aborting due to 3 previous errors diff --git a/src/test/ui/typeck/issue-67971.rs b/src/test/ui/typeck/issue-67971.rs new file mode 100644 index 0000000000000..8bf725cb5ee38 --- /dev/null +++ b/src/test/ui/typeck/issue-67971.rs @@ -0,0 +1,9 @@ +struct S {} + +fn foo(ctx: &mut S) -> String { //~ ERROR mismatched types + // Don't suggest to remove semicolon as it won't fix anything + ctx.sleep = 0; + //~^ ERROR no field `sleep` on type `&mut S` +} + +fn main() {} diff --git a/src/test/ui/typeck/issue-67971.stderr b/src/test/ui/typeck/issue-67971.stderr new file mode 100644 index 0000000000000..36ad3fcb342a8 --- /dev/null +++ b/src/test/ui/typeck/issue-67971.stderr @@ -0,0 +1,18 @@ +error[E0609]: no field `sleep` on type `&mut S` + --> $DIR/issue-67971.rs:5:9 + | +LL | ctx.sleep = 0; + | ^^^^^ unknown field + +error[E0308]: mismatched types + --> $DIR/issue-67971.rs:3:24 + | +LL | fn foo(ctx: &mut S) -> String { + | --- ^^^^^^ expected struct `std::string::String`, found `()` + | | + | implicitly returns `()` as its body has no tail or `return` expression + +error: aborting due to 2 previous errors + +Some errors have detailed explanations: E0308, E0609. +For more information about an error, try `rustc --explain E0308`.