From b9185943697bd5bb503c334576ddab05328f2e00 Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Tue, 21 May 2019 07:43:43 +0200 Subject: [PATCH] Fix fallout from rust-lang/rust PR 60861. --- clippy_lints/src/collapsible_if.rs | 49 +++++++++++++-------------- clippy_lints/src/formatting.rs | 6 ++-- clippy_lints/src/needless_continue.rs | 12 +++---- clippy_lints/src/utils/sugg.rs | 3 +- 4 files changed, 32 insertions(+), 38 deletions(-) diff --git a/clippy_lints/src/collapsible_if.rs b/clippy_lints/src/collapsible_if.rs index 00d7bf3b5a9..12c83873641 100644 --- a/clippy_lints/src/collapsible_if.rs +++ b/clippy_lints/src/collapsible_if.rs @@ -84,18 +84,14 @@ impl EarlyLintPass for CollapsibleIf { } fn check_if(cx: &EarlyContext<'_>, expr: &ast::Expr) { - match expr.node { - ast::ExprKind::If(ref check, ref then, ref else_) => { - if let Some(ref else_) = *else_ { - check_collapsible_maybe_if_let(cx, else_); - } else { - check_collapsible_no_if_let(cx, expr, check, then); - } - }, - ast::ExprKind::IfLet(_, _, _, Some(ref else_)) => { + if let ast::ExprKind::If(check, then, else_) = &expr.node { + if let Some(else_) = else_ { check_collapsible_maybe_if_let(cx, else_); - }, - _ => (), + } else if let ast::ExprKind::Let(..) = check.node { + // Prevent triggering on `if let a = b { if c { .. } }`. + } else { + check_collapsible_no_if_let(cx, expr, check, then); + } } } @@ -113,22 +109,18 @@ fn check_collapsible_maybe_if_let(cx: &EarlyContext<'_>, else_: &ast::Expr) { if !block_starts_with_comment(cx, block); if let Some(else_) = expr_block(block); if !in_macro_or_desugar(else_.span); + if let ast::ExprKind::If(..) = else_.node; then { - match else_.node { - ast::ExprKind::If(..) | ast::ExprKind::IfLet(..) => { - let mut applicability = Applicability::MachineApplicable; - span_lint_and_sugg( - cx, - COLLAPSIBLE_IF, - block.span, - "this `else { if .. }` block can be collapsed", - "try", - snippet_block_with_applicability(cx, else_.span, "..", &mut applicability).into_owned(), - applicability, - ); - } - _ => (), - } + let mut applicability = Applicability::MachineApplicable; + span_lint_and_sugg( + cx, + COLLAPSIBLE_IF, + block.span, + "this `else { if .. }` block can be collapsed", + "try", + snippet_block_with_applicability(cx, else_.span, "..", &mut applicability).into_owned(), + applicability, + ); } } } @@ -139,6 +131,11 @@ fn check_collapsible_no_if_let(cx: &EarlyContext<'_>, expr: &ast::Expr, check: & if let Some(inner) = expr_block(then); if let ast::ExprKind::If(ref check_inner, ref content, None) = inner.node; then { + if let ast::ExprKind::Let(..) = check_inner.node { + // Prevent triggering on `if c { if let a = b { .. } }`. + return; + } + if expr.span.ctxt() != inner.span.ctxt() { return; } diff --git a/clippy_lints/src/formatting.rs b/clippy_lints/src/formatting.rs index 5916a065950..9738a1c4c73 100644 --- a/clippy_lints/src/formatting.rs +++ b/clippy_lints/src/formatting.rs @@ -245,12 +245,10 @@ fn is_block(expr: &ast::Expr) -> bool { } } -/// Match `if` or `if let` expressions and return the `then` and `else` block. +/// Match `if` expressions and return the `then` and `else` block. fn unsugar_if(expr: &ast::Expr) -> Option<(&P, &Option>)> { match expr.node { - ast::ExprKind::If(_, ref then, ref else_) | ast::ExprKind::IfLet(_, _, ref then, ref else_) => { - Some((then, else_)) - }, + ast::ExprKind::If(_, ref then, ref else_) => Some((then, else_)), _ => None, } } diff --git a/clippy_lints/src/needless_continue.rs b/clippy_lints/src/needless_continue.rs index 8835158155b..2ab5940875b 100644 --- a/clippy_lints/src/needless_continue.rs +++ b/clippy_lints/src/needless_continue.rs @@ -209,12 +209,12 @@ fn with_loop_block(expr: &ast::Expr, mut func: F) where F: FnMut(&ast::Block, Option<&ast::Label>), { - match expr.node { - ast::ExprKind::While(_, ref loop_block, ref label) - | ast::ExprKind::WhileLet(_, _, ref loop_block, ref label) - | ast::ExprKind::ForLoop(_, _, ref loop_block, ref label) - | ast::ExprKind::Loop(ref loop_block, ref label) => func(loop_block, label.as_ref()), - _ => {}, + if let ast::ExprKind::While(_, loop_block, label) + | ast::ExprKind::ForLoop(_, _, loop_block, label) + | ast::ExprKind::Loop(loop_block, label) + = &expr.node + { + func(loop_block, label.as_ref()); } } diff --git a/clippy_lints/src/utils/sugg.rs b/clippy_lints/src/utils/sugg.rs index 70286dc95a3..a9643a74085 100644 --- a/clippy_lints/src/utils/sugg.rs +++ b/clippy_lints/src/utils/sugg.rs @@ -135,7 +135,7 @@ impl<'a> Sugg<'a> { | ast::ExprKind::Box(..) | ast::ExprKind::Closure(..) | ast::ExprKind::If(..) - | ast::ExprKind::IfLet(..) + | ast::ExprKind::Let(..) | ast::ExprKind::Unary(..) | ast::ExprKind::Match(..) => Sugg::MaybeParen(snippet), ast::ExprKind::Async(..) @@ -162,7 +162,6 @@ impl<'a> Sugg<'a> { | ast::ExprKind::Tup(..) | ast::ExprKind::Array(..) | ast::ExprKind::While(..) - | ast::ExprKind::WhileLet(..) | ast::ExprKind::Await(..) | ast::ExprKind::Err => Sugg::NonParen(snippet), ast::ExprKind::Range(.., RangeLimits::HalfOpen) => Sugg::BinOp(AssocOp::DotDot, snippet),