Fix false-positive in USELESS_LET_IF_SEQ

This commit is contained in:
mcarton 2016-06-05 21:38:15 +02:00
parent 3436699ed6
commit 158183adf5
No known key found for this signature in database
GPG key ID: 5E427C794CBA45E8
2 changed files with 35 additions and 5 deletions

View file

@ -69,12 +69,9 @@ impl LateLintPass for LetIfSeq {
let Some(def) = cx.tcx.def_map.borrow().get(&decl.pat.id),
let hir::StmtExpr(ref if_, _) = expr.node,
let hir::ExprIf(ref cond, ref then, ref else_) = if_.node,
{
let mut v = UsedVisitor { cx: cx, id: def.def_id(), used: false };
hir::intravisit::walk_expr(&mut v, cond);
!v.used
},
!used_in_expr(cx, def.def_id(), cond),
let Some(value) = check_assign(cx, def.def_id(), then),
!used_in_expr(cx, def.def_id(), value),
], {
let span = codemap::mk_sp(stmt.span.lo, if_.span.hi);
@ -179,3 +176,13 @@ fn check_assign<'e>(cx: &LateContext, decl: hir::def_id::DefId, block: &'e hir::
None
}
fn used_in_expr(cx: &LateContext, id: hir::def_id::DefId, expr: &hir::Expr) -> bool {
let mut v = UsedVisitor {
cx: cx,
id: id,
used: false
};
hir::intravisit::walk_expr(&mut v, expr);
v.used
}

View file

@ -5,6 +5,27 @@
#![deny(useless_let_if_seq)]
fn f() -> bool { true }
fn g(x: i32) -> i32 { x + 1 }
fn issue985() -> i32 {
let mut x = 42;
if f() {
x = g(x);
}
x
}
fn issue985_alt() -> i32 {
let mut x = 42;
if f() {
f();
} else {
x = g(x);
}
x
}
fn issue975() -> String {
let mut udn = "dummy".to_string();
@ -30,6 +51,8 @@ fn early_return() -> u8 {
fn main() {
early_return();
issue975();
issue985();
issue985_alt();
let mut foo = 0;
//~^ ERROR `if _ { .. } else { .. }` is an expression