Fix FP with `WHILE_LET_LOOP` and break expressions

This commit is contained in:
mcarton 2016-06-16 16:19:17 +02:00
parent 06fd2468b2
commit 836554387a
No known key found for this signature in database
GPG Key ID: 5E427C794CBA45E8
2 changed files with 20 additions and 6 deletions

View File

@ -249,7 +249,8 @@ impl LateLintPass for LoopsPass {
match *source {
MatchSource::Normal |
MatchSource::IfLetDesugar { .. } => {
if arms.len() == 2 && arms[0].pats.len() == 1 && arms[0].guard.is_none() &&
if arms.len() == 2 &&
arms[0].pats.len() == 1 && arms[0].guard.is_none() &&
arms[1].pats.len() == 1 && arms[1].guard.is_none() &&
is_break_expr(&arms[1].body) {
if in_external_macro(cx, expr.span) {
@ -787,12 +788,11 @@ fn extract_expr_from_first_stmt(block: &Block) -> Option<&Expr> {
/// If a block begins with an expression (with or without semicolon), return it.
fn extract_first_expr(block: &Block) -> Option<&Expr> {
match block.expr {
Some(ref expr) => Some(expr),
Some(ref expr) if block.stmts.is_empty() => Some(expr),
None if !block.stmts.is_empty() => {
match block.stmts[0].node {
StmtExpr(ref expr, _) |
StmtSemi(ref expr, _) => Some(expr),
_ => None,
StmtExpr(ref expr, _) | StmtSemi(ref expr, _) => Some(expr),
StmtDecl(..) => None,
}
}
_ => None,
@ -803,7 +803,6 @@ fn extract_first_expr(block: &Block) -> Option<&Expr> {
fn is_break_expr(expr: &Expr) -> bool {
match expr.node {
ExprBreak(None) => true,
// there won't be a `let <pat> = break` and so we can safely ignore the StmtDecl case
ExprBlock(ref b) => {
match extract_first_expr(b) {
Some(ref subexpr) => is_break_expr(subexpr),

View File

@ -150,3 +150,18 @@ fn no_panic<T>(slice: &[T]) {
loop {} //~ERROR empty `loop {}` detected.
}
}
fn issue1017() {
let r: Result<u32, u32> = Ok(42);
let mut len = 1337;
loop {
match r {
Err(_) => len = 0,
Ok(length) => {
len = length;
break
}
}
}
}