This commit is contained in:
Oliver Schneider 2016-02-01 11:28:39 +01:00
parent d6c0435c81
commit 07ace32ac9
3 changed files with 45 additions and 68 deletions

View file

@ -54,43 +54,38 @@ impl LateLintPass for CollapsibleIf {
fn check_if(cx: &LateContext, e: &Expr) { fn check_if(cx: &LateContext, e: &Expr) {
if let ExprIf(ref check, ref then, ref else_) = e.node { if let ExprIf(ref check, ref then, ref else_) = e.node {
match *else_ { if let Some(ref else_) = *else_ {
Some(ref else_) => { if_let_chain! {[
if_let_chain! {[ let ExprBlock(ref block) = else_.node,
let ExprBlock(ref block) = else_.node, block.stmts.is_empty(),
block.stmts.is_empty(), block.rules == BlockCheckMode::DefaultBlock,
block.rules == BlockCheckMode::DefaultBlock, let Some(ref else_) = block.expr,
let Some(ref else_) = block.expr, let ExprIf(_, _, _) = else_.node
let ExprIf(_, _, _) = else_.node ], {
], { span_lint_and_then(cx,
span_lint_and_then(cx, COLLAPSIBLE_IF,
COLLAPSIBLE_IF, block.span,
block.span, "this `else { if .. }` block can be collapsed", |db| {
"this `else { if .. }` block can be collapsed", |db| { db.span_suggestion(block.span, "try",
db.span_suggestion(block.span, "try", format!("else {}",
format!("else {}", snippet_block(cx, else_.span, "..")));
snippet_block(cx, else_.span, ".."))); });
}); }}
}} } else if let Some(&Expr{ node: ExprIf(ref check_inner, ref content, None), span: sp, ..}) =
}
None => {
if let Some(&Expr{ node: ExprIf(ref check_inner, ref content, None), span: sp, ..}) =
single_stmt_of_block(then) { single_stmt_of_block(then) {
if e.span.expn_id != sp.expn_id { if e.span.expn_id != sp.expn_id {
return; return;
}
span_lint_and_then(cx,
COLLAPSIBLE_IF,
e.span,
"this if statement can be collapsed", |db| {
db.span_suggestion(e.span, "try",
format!("if {} && {} {}",
check_to_string(cx, check),
check_to_string(cx, check_inner),
snippet_block(cx, content.span, "..")));
});
}
} }
span_lint_and_then(cx,
COLLAPSIBLE_IF,
e.span,
"this if statement can be collapsed", |db| {
db.span_suggestion(e.span, "try",
format!("if {} && {} {}",
check_to_string(cx, check),
check_to_string(cx, check_inner),
snippet_block(cx, content.span, "..")));
});
} }
} }
} }

View file

@ -33,27 +33,19 @@ impl LateLintPass for UnnecessaryMutPassed {
let borrowed_table = cx.tcx.tables.borrow(); let borrowed_table = cx.tcx.tables.borrow();
match e.node { match e.node {
ExprCall(ref fn_expr, ref arguments) => { ExprCall(ref fn_expr, ref arguments) => {
match borrowed_table.node_types.get(&fn_expr.id) { let function_type = borrowed_table.node_types
Some(function_type) => { .get(&fn_expr.id)
if let ExprPath(_, ref path) = fn_expr.node { .expect("A function with an unknown type is called. \
check_arguments(cx, &arguments, function_type, &format!("{}", path)); If this happened, the compiler would have \
} aborted the compilation long ago");
} if let ExprPath(_, ref path) = fn_expr.node {
None => unreachable!(), // A function with unknown type is called. check_arguments(cx, &arguments, function_type, &format!("{}", path));
// If this happened the compiler would have aborted the }
// compilation long ago.
};
} }
ExprMethodCall(ref name, _, ref arguments) => { ExprMethodCall(ref name, _, ref arguments) => {
let method_call = MethodCall::expr(e.id); let method_call = MethodCall::expr(e.id);
match borrowed_table.method_map.get(&method_call) { let method_type = borrowed_table.method_map.get(&method_call).expect("This should never happen.");
Some(method_type) => { check_arguments(cx, &arguments, method_type.ty, &format!("{}", name.node.as_str()))
check_arguments(cx, &arguments, method_type.ty, &format!("{}", name.node.as_str()))
}
None => unreachable!(), // Just like above, this should never happen.
};
} }
_ => {} _ => {}
} }

View file

@ -657,20 +657,10 @@ pub fn is_expn_of(cx: &LateContext, mut span: Span, name: &str) -> Option<Span>
(ei.callee.name(), ei.call_site) (ei.callee.name(), ei.call_site)
}) })
}); });
match span_name_span {
return match span_name_span { Some((mac_name, new_span)) if mac_name.as_str() == name => return Some(new_span),
Some((mac_name, new_span)) => { None => return None,
if mac_name.as_str() == name { Some((_, new_span)) => span = new_span,
Some(new_span) }
}
else {
span = new_span;
continue;
}
}
None => {
None
}
};
} }
} }