Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/hir-ty/src/diagnostics/expr.rs')
-rw-r--r--crates/hir-ty/src/diagnostics/expr.rs8
1 files changed, 7 insertions, 1 deletions
diff --git a/crates/hir-ty/src/diagnostics/expr.rs b/crates/hir-ty/src/diagnostics/expr.rs
index 0c5d639961..571f01dde2 100644
--- a/crates/hir-ty/src/diagnostics/expr.rs
+++ b/crates/hir-ty/src/diagnostics/expr.rs
@@ -338,7 +338,13 @@ impl ExprValidator {
fn check_for_unnecessary_else(&mut self, id: ExprId, expr: &Expr, body: &Body) {
if let Expr::If { condition: _, then_branch, else_branch } = expr {
- if else_branch.is_none() {
+ if let Some(else_branch) = else_branch {
+ // If else branch has a tail, it is an "expression" that produces a value,
+ // e.g. `let a = if { ... } else { ... };` and this `else` is not unnecessary
+ if let Expr::Block { tail: Some(_), .. } = body.exprs[*else_branch] {
+ return;
+ }
+ } else {
return;
}
if let Expr::Block { statements, tail, .. } = &body.exprs[*then_branch] {