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) {
if let ExprIf(ref check, ref then, ref else_) = e.node {
match *else_ {
Some(ref else_) => {
if_let_chain! {[
let ExprBlock(ref block) = else_.node,
block.stmts.is_empty(),
block.rules == BlockCheckMode::DefaultBlock,
let Some(ref else_) = block.expr,
let ExprIf(_, _, _) = else_.node
], {
span_lint_and_then(cx,
COLLAPSIBLE_IF,
block.span,
"this `else { if .. }` block can be collapsed", |db| {
db.span_suggestion(block.span, "try",
format!("else {}",
snippet_block(cx, else_.span, "..")));
});
}}
}
None => {
if let Some(&Expr{ node: ExprIf(ref check_inner, ref content, None), span: sp, ..}) =
if let Some(ref else_) = *else_ {
if_let_chain! {[
let ExprBlock(ref block) = else_.node,
block.stmts.is_empty(),
block.rules == BlockCheckMode::DefaultBlock,
let Some(ref else_) = block.expr,
let ExprIf(_, _, _) = else_.node
], {
span_lint_and_then(cx,
COLLAPSIBLE_IF,
block.span,
"this `else { if .. }` block can be collapsed", |db| {
db.span_suggestion(block.span, "try",
format!("else {}",
snippet_block(cx, else_.span, "..")));
});
}}
} else if let Some(&Expr{ node: ExprIf(ref check_inner, ref content, None), span: sp, ..}) =
single_stmt_of_block(then) {
if e.span.expn_id != sp.expn_id {
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, "..")));
});
}
if e.span.expn_id != sp.expn_id {
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, "..")));
});
}
}
}

View file

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

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)
})
});
return match span_name_span {
Some((mac_name, new_span)) => {
if mac_name.as_str() == name {
Some(new_span)
}
else {
span = new_span;
continue;
}
}
None => {
None
}
};
match span_name_span {
Some((mac_name, new_span)) if mac_name.as_str() == name => return Some(new_span),
None => return None,
Some((_, new_span)) => span = new_span,
}
}
}