mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-01 06:11:35 +00:00
Handle cases for else if
This commit is contained in:
parent
e9c80a9c25
commit
d14b22863b
2 changed files with 29 additions and 2 deletions
|
@ -341,8 +341,23 @@ impl ExprValidator {
|
||||||
if let Some(else_branch) = else_branch {
|
if let Some(else_branch) = else_branch {
|
||||||
// If else branch has a tail, it is an "expression" that produces a value,
|
// 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
|
// e.g. `let a = if { ... } else { ... };` and this `else` is not unnecessary
|
||||||
if let Expr::Block { tail: Some(_), .. } = body.exprs[*else_branch] {
|
let mut branch = *else_branch;
|
||||||
return;
|
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 {
|
} else {
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -407,6 +407,18 @@ fn test2(a: bool) -> i32 {
|
||||||
0
|
0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn test3(a: bool, b: bool, c: bool) {
|
||||||
|
let _x = if a {
|
||||||
|
return;
|
||||||
|
} else if b {
|
||||||
|
return;
|
||||||
|
} else if c {
|
||||||
|
1
|
||||||
|
} else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
}
|
||||||
"#,
|
"#,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue