From 07ace32ac91875be65c40a8957eb0982c027bd16 Mon Sep 17 00:00:00 2001 From: Oliver Schneider Date: Mon, 1 Feb 2016 11:28:39 +0100 Subject: [PATCH] fallout --- src/collapsible_if.rs | 65 ++++++++++++++++++++----------------------- src/mut_reference.rs | 28 +++++++------------ src/utils.rs | 20 ++++--------- 3 files changed, 45 insertions(+), 68 deletions(-) diff --git a/src/collapsible_if.rs b/src/collapsible_if.rs index dd89b22a4..4b3c4174d 100644 --- a/src/collapsible_if.rs +++ b/src/collapsible_if.rs @@ -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, ".."))); + }); } } } diff --git a/src/mut_reference.rs b/src/mut_reference.rs index d92e449e7..15e8d310c 100644 --- a/src/mut_reference.rs +++ b/src/mut_reference.rs @@ -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())) } _ => {} } diff --git a/src/utils.rs b/src/utils.rs index b53559194..ff0c59ab2 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -657,20 +657,10 @@ pub fn is_expn_of(cx: &LateContext, mut span: Span, name: &str) -> Option (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, + } } }