diff options
| author | Shoyu Vanilla <modulo641@gmail.com> | 2024-02-15 01:29:48 +0900 |
|---|---|---|
| committer | davidsemakula <hello@davidsemakula.com> | 2024-02-19 15:19:27 +0300 |
| commit | d14b22863bd58e78a8e9193db987ae22ba1e57e1 (patch) | |
| tree | 93397869b32efa1a88890f781efbfd3e838bcf5c | |
| parent | e9c80a9c256677d85398d93880eec1e54e226d2a (diff) | |
| download | rust-d14b22863bd58e78a8e9193db987ae22ba1e57e1.tar.gz rust-d14b22863bd58e78a8e9193db987ae22ba1e57e1.zip | |
Handle cases for `else if`
| -rw-r--r-- | crates/hir-ty/src/diagnostics/expr.rs | 19 | ||||
| -rw-r--r-- | crates/ide-diagnostics/src/handlers/remove_unnecessary_else.rs | 12 |
2 files changed, 29 insertions, 2 deletions
diff --git a/crates/hir-ty/src/diagnostics/expr.rs b/crates/hir-ty/src/diagnostics/expr.rs index 571f01dde2e..ff70618ca12 100644 --- a/crates/hir-ty/src/diagnostics/expr.rs +++ b/crates/hir-ty/src/diagnostics/expr.rs @@ -341,8 +341,23 @@ impl ExprValidator { 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; + let mut branch = *else_branch; + loop { + match body.exprs[branch] { + Expr::Block { tail: Some(_), .. } => return, + Expr::If { then_branch, else_branch, .. } => { + if let Expr::Block { tail: Some(_), .. } = body.exprs[then_branch] { + return; + } + if let Some(else_branch) = else_branch { + // Continue checking for branches like `if { ... } else if { ... } else...` + branch = else_branch; + continue; + } + } + _ => break, + } + break; } } else { return; diff --git a/crates/ide-diagnostics/src/handlers/remove_unnecessary_else.rs b/crates/ide-diagnostics/src/handlers/remove_unnecessary_else.rs index 813c07a505d..bbc10e96cef 100644 --- a/crates/ide-diagnostics/src/handlers/remove_unnecessary_else.rs +++ b/crates/ide-diagnostics/src/handlers/remove_unnecessary_else.rs @@ -407,6 +407,18 @@ fn test2(a: bool) -> i32 { 0 } } + +fn test3(a: bool, b: bool, c: bool) { + let _x = if a { + return; + } else if b { + return; + } else if c { + 1 + } else { + return; + }; +} "#, ); } |
