Skip to content
Open
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
15 changes: 14 additions & 1 deletion compiler/rustc_hir_typeck/src/coercion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1880,7 +1880,20 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
hir::ExprKind::Match(.., hir::MatchSource::TryDesugar(_))
)
{
err.span_label(cond_expr.span, "expected this to be `()`");
if let ObligationCauseCode::BlockTailExpression(hir_id, hir::MatchSource::Normal) =
cause.code()
&& let hir::Node::Block(block) = fcx.tcx.hir_node(*hir_id)
&& let hir::Node::Expr(expr) = fcx.tcx.parent_hir_node(block.hir_id)
&& let hir::Node::Expr(if_expr) = fcx.tcx.parent_hir_node(expr.hir_id)
&& let hir::ExprKind::If(_cond, _then, None) = if_expr.kind
{
err.span_label(
cond_expr.span,
"expected this `if` expression to be `()` because it has no `else` arm",
);
} else {
err.span_label(cond_expr.span, "expected this to be `()`");
}
if expr.can_have_side_effects() {
fcx.suggest_semicolon_at_end(cond_expr.span, &mut err);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ LL | | false
| | ^^^^^ expected `()`, found `bool`
LL | |
LL | | }
| |_________- expected this to be `()`
| |_________- expected this `if` expression to be `()` because it has no `else` arm
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aren't we pointing at the block? It seems slightly off to call it an if expression, although I don't have a good alternative in mind right now.


error[E0308]: mismatched types
--> $DIR/dont-ice-for-type-mismatch-in-closure-in-async.rs:12:9
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/async-await/missing-return-in-async-block.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ LL | / if true {
LL | | Ok(S)
| | ^^^^^ expected `()`, found `Result<S, _>`
LL | | }
| |_________- expected this to be `()`
| |_________- expected this `if` expression to be `()` because it has no `else` arm
|
= note: expected unit type `()`
found enum `Result<S, _>`
Expand All @@ -21,7 +21,7 @@ LL | / if true {
LL | | Ok(S)
| | ^^^^^ expected `()`, found `Result<S, _>`
LL | | }
| |_________- expected this to be `()`
| |_________- expected this `if` expression to be `()` because it has no `else` arm
|
= note: expected unit type `()`
found enum `Result<S, _>`
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/lint/unused/unused-doc-comments-edge-cases.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ LL | / if num == 3 {
LL | | true
| | ^^^^ expected `()`, found `bool`
LL | | }
| |_____- expected this to be `()`
| |_____- expected this `if` expression to be `()` because it has no `else` arm
|
help: you might have meant to return this value
|
Expand Down
8 changes: 4 additions & 4 deletions tests/ui/loops/dont-suggest-break-thru-item.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ LL | | Err(1)
| | ^^^^^^ expected `()`, found `Result<_, {integer}>`
... |
LL | | }
| |_____________- expected this to be `()`
| |_____________- expected this `if` expression to be `()` because it has no `else` arm
|
= note: expected unit type `()`
found enum `Result<_, {integer}>`
Expand All @@ -23,7 +23,7 @@ LL | | Err(1)
| | ^^^^^^ expected `()`, found `Result<_, {integer}>`
... |
LL | | }
| |_____________- expected this to be `()`
| |_____________- expected this `if` expression to be `()` because it has no `else` arm
|
= note: expected unit type `()`
found enum `Result<_, {integer}>`
Expand All @@ -40,7 +40,7 @@ LL | | Err(1)
| | ^^^^^^ expected `()`, found `Result<_, {integer}>`
LL | |
LL | | }
| |_____________- expected this to be `()`
| |_____________- expected this `if` expression to be `()` because it has no `else` arm
|
= note: expected unit type `()`
found enum `Result<_, {integer}>`
Expand All @@ -53,7 +53,7 @@ LL | | Err(1)
| | ^^^^^^ expected `()`, found `Result<_, {integer}>`
LL | |
LL | | }
| |_____________- expected this to be `()`
| |_____________- expected this `if` expression to be `()` because it has no `else` arm
|
= note: expected unit type `()`
found enum `Result<_, {integer}>`
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/parser/struct-literals-in-invalid-places.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ LL | if x == E::V { field } {}
| ---------------^^^^^--
| | |
| | expected `()`, found `bool`
| expected this to be `()`
| expected this `if` expression to be `()` because it has no `else` arm
|
help: you might have meant to return this value
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ LL | | let value = unsafe { self.values.get_unchecked_mut(index) };
LL | | value.get_or_insert_with(func)
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `()`, found `&mut V`
LL | | }
| |_________- expected this to be `()`
| |_________- expected this `if` expression to be `()` because it has no `else` arm
|
= note: expected unit type `()`
found mutable reference `&mut V`
Expand Down
6 changes: 3 additions & 3 deletions tests/ui/return/tail-expr-as-potential-return.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ LL | | Err(42)
| | ^^^^^^^ expected `()`, found `Result<_, {integer}>`
... |
LL | | }
| |_____- expected this to be `()`
| |_____- expected this `if` expression to be `()` because it has no `else` arm
|
= note: expected unit type `()`
found enum `Result<_, {integer}>`
Expand All @@ -23,7 +23,7 @@ LL | | 1i32
| | ^^^^ expected `()`, found `i32`
... |
LL | | }
| |_____- expected this to be `()`
| |_____- expected this `if` expression to be `()` because it has no `else` arm
|
help: you might have meant to return this value
|
Expand All @@ -38,7 +38,7 @@ LL | | Err(42)
| | ^^^^^^^ expected `()`, found `Result<_, {integer}>`
... |
LL | | }
| |_____- expected this to be `()`
| |_____- expected this `if` expression to be `()` because it has no `else` arm
|
= note: expected unit type `()`
found enum `Result<_, {integer}>`
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/return/tail-expr-if-as-return.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ LL | / if true {
LL | | ""
| | ^^ expected `()`, found `&str`
LL | | }
| |_____- expected this to be `()`
| |_____- expected this `if` expression to be `()` because it has no `else` arm

error: aborting due to 1 previous error

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fn main() -> Result<(), ()> {
// Here, we do want to suggest a semicolon:
let x = Ok(42);
if true {
//~^ NOTE: expected this to be `()`
//~^ NOTE: expected this `if` expression to be `()` because it has no `else` arm
x?
//~^ ERROR: mismatched types [E0308]
//~| NOTE: expected `()`, found integer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ LL | | x?
| | ^^ expected `()`, found integer
... |
LL | | }
| |_____- expected this to be `()`
| |_____- expected this `if` expression to be `()` because it has no `else` arm
|
help: consider using a semicolon here
|
Expand Down
Loading