Verify that an if condition block returns a value

This commit is contained in:
Esteban Küber
2017-08-17 16:51:52 -07:00
parent 20a2716206
commit f06323337d
4 changed files with 51 additions and 5 deletions

View File

@@ -844,6 +844,32 @@ pub struct Expr {
pub attrs: ThinVec<Attribute>
}
impl Expr {
/// Wether this expression would be valid somewhere that expects a value, for example, an `if`
/// condition.
pub fn returns(&self) -> bool {
if let ExprKind::Block(ref block) = self.node {
match block.stmts.last().map(|last_stmt| &last_stmt.node) {
// implicit return
Some(&StmtKind::Expr(_)) => true,
Some(&StmtKind::Semi(ref expr)) => {
if let ExprKind::Ret(_) = expr.node {
// last statement is explicit return
true
} else {
false
}
}
// This is a block that doesn't end in either an implicit or explicit return
_ => false,
}
} else {
// This is not a block, it is a value
true
}
}
}
impl fmt::Debug for Expr {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "expr({}: {})", self.id, pprust::expr_to_string(self))