Fixing src/copies.rs and src/entries.rs by using ExprBlock(block) = then.node

This commit is contained in:
Enrico Schmitz 2017-03-31 23:36:45 +02:00 committed by Enrico Schmitz
parent 8f9fb97eb6
commit 8297c19fcc
3 changed files with 23 additions and 14 deletions

View File

@ -223,13 +223,15 @@ fn lint_match_arms(cx: &LateContext, expr: &Expr) {
/// `if a { c } else if b { d } else { e }`.
fn if_sequence(mut expr: &Expr) -> (SmallVector<&Expr>, SmallVector<&Block>) {
let mut conds = SmallVector::new();
let mut blocks = SmallVector::new();
let mut blocks : SmallVector<&Block> = SmallVector::new();
while let ExprIf(ref cond, ref then_expr, ref else_expr) = expr.node {
conds.push(&**cond);
//FIXME
//blocks.push(&**then_expr);
//FIXME
if let ExprBlock(ref block) = then_expr.node {
blocks.push(&block);
} else {
panic!("ExprIf node is not an ExprBlock");
}
if let Some(ref else_expr) = *else_expr {
expr = else_expr;
@ -241,9 +243,7 @@ fn if_sequence(mut expr: &Expr) -> (SmallVector<&Expr>, SmallVector<&Block>) {
// final `else {..}`
if !blocks.is_empty() {
if let ExprBlock(ref block) = expr.node {
//FIXME
//blocks.push(&**block);
//FIXME
blocks.push(&**block);
}
}
@ -315,10 +315,10 @@ fn search_same<T, Hash, Eq>(exprs: &[T], hash: Hash, eq: Eq) -> Option<(&T, &T)>
return None;
} else if exprs.len() == 2 {
return if eq(&exprs[0], &exprs[1]) {
Some((&exprs[0], &exprs[1]))
} else {
None
};
Some((&exprs[0], &exprs[1]))
} else {
None
};
}
let mut map: HashMap<_, Vec<&_>> = HashMap::with_capacity(exprs.len());

View File

@ -46,7 +46,14 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for HashMapLint {
if let Some((ty, map, key)) = check_cond(cx, check) {
// in case of `if !m.contains_key(&k) { m.insert(k, v); }`
// we can give a better error message
let sole_expr = else_block.is_none();
let sole_expr = {
else_block.is_none() &&
if let ExprBlock(ref then_block) = then_block.node {
(then_block.expr.is_some() as usize) + then_block.stmts.len() == 1
} else {
true
}
};
let mut visitor = InsertVisitor {
cx: cx,

View File

@ -69,7 +69,9 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for LetIfSeq {
let hir::StmtExpr(ref if_, _) = expr.node,
let hir::ExprIf(ref cond, ref then, ref else_) = if_.node,
!used_in_expr(cx, def_id, cond),
!used_in_expr(cx, def_id, &**then),
!used_in_expr(cx, def_id, &*then),
let hir::ExprBlock(ref then) = then.node,
let Some(value) = check_assign(cx, def_id, &*then),
], {
let span = Span { lo: stmt.span.lo, hi: if_.span.hi, ctxt: NO_EXPANSION };
@ -104,7 +106,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for LetIfSeq {
mut=mutability,
name=name.node,
cond=snippet(cx, cond.span, "_"),
then={ "" },
then=if then.stmts.len() > 1 { " ..;" } else { "" },
else=if default_multi_stmts { " ..;" } else { "" },
value=snippet(cx, then.span, "<value>"),
default=snippet(cx, default.span, "<default>"),