From c9ed92ce20fbb2871311d493148523814cca7f1d Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Fri, 10 May 2019 19:34:47 -0700 Subject: [PATCH] More uses of higher::if_block --- clippy_lints/src/block_in_if_condition.rs | 2 +- clippy_lints/src/copies.rs | 16 +++++++--------- clippy_lints/src/entry.rs | 4 ++-- clippy_lints/src/let_if_seq.rs | 4 ++-- 4 files changed, 12 insertions(+), 14 deletions(-) diff --git a/clippy_lints/src/block_in_if_condition.rs b/clippy_lints/src/block_in_if_condition.rs index 1f20f5c41fc..05d3ea6b274 100644 --- a/clippy_lints/src/block_in_if_condition.rs +++ b/clippy_lints/src/block_in_if_condition.rs @@ -72,7 +72,7 @@ fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> { impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BlockInIfCondition { fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) { - if let ExprKind::If(check, then, _) = &expr.node { + if let Some((check, then, _)) = higher::if_block(&expr) { if let ExprKind::Block(block, _) = &check.node { if block.rules == DefaultBlock { if block.stmts.is_empty() { diff --git a/clippy_lints/src/copies.rs b/clippy_lints/src/copies.rs index d1163e10279..c08d6851aa0 100644 --- a/clippy_lints/src/copies.rs +++ b/clippy_lints/src/copies.rs @@ -1,4 +1,4 @@ -use crate::utils::{get_parent_expr, in_macro, snippet, span_lint_and_then, span_note_and_lint}; +use crate::utils::{get_parent_expr, higher, in_macro, snippet, span_lint_and_then, span_note_and_lint}; use crate::utils::{SpanlessEq, SpanlessHash}; use rustc::hir::*; use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass}; @@ -109,13 +109,11 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for CopyAndPaste { fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) { if !in_macro(expr.span) { // skip ifs directly in else, it will be checked in the parent if - if let Some(&Expr { - node: ExprKind::If(_, _, Some(ref else_expr)), - .. - }) = get_parent_expr(cx, expr) - { - if else_expr.hir_id == expr.hir_id { - return; + if let Some(expr) = get_parent_expr(cx, expr) { + if let Some((_, _, Some(ref else_expr))) = higher::if_block(&expr) { + if else_expr.hir_id == expr.hir_id { + return; + } } } @@ -236,7 +234,7 @@ fn lint_match_arms(cx: &LateContext<'_, '_>, expr: &Expr) { let mut conds = SmallVec::new(); let mut blocks: SmallVec<[&Block; 1]> = SmallVec::new(); - while let ExprKind::If(ref cond, ref then_expr, ref else_expr) = expr.node { + while let Some((ref cond, ref then_expr, ref else_expr)) = higher::if_block(&expr) { conds.push(&**cond); if let ExprKind::Block(ref block, _) = then_expr.node { blocks.push(block); diff --git a/clippy_lints/src/entry.rs b/clippy_lints/src/entry.rs index c74232a906e..551e0bb848d 100644 --- a/clippy_lints/src/entry.rs +++ b/clippy_lints/src/entry.rs @@ -1,5 +1,5 @@ use crate::utils::SpanlessEq; -use crate::utils::{get_item_name, match_type, paths, snippet, span_lint_and_then, walk_ptrs_ty}; +use crate::utils::{get_item_name, higher, match_type, paths, snippet, span_lint_and_then, walk_ptrs_ty}; use if_chain::if_chain; use rustc::hir::intravisit::{walk_expr, NestedVisitorMap, Visitor}; use rustc::hir::*; @@ -41,7 +41,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for HashMapPass { fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) { - if let ExprKind::If(ref check, ref then_block, ref else_block) = expr.node { + if let Some((ref check, ref then_block, ref else_block)) = higher::if_block(&expr) { if let ExprKind::Unary(UnOp::UnNot, ref check) = check.node { if let Some((ty, map, key)) = check_cond(cx, check) { // in case of `if !m.contains_key(&k) { m.insert(k, v); }` diff --git a/clippy_lints/src/let_if_seq.rs b/clippy_lints/src/let_if_seq.rs index 8a6456925a3..990d8facd13 100644 --- a/clippy_lints/src/let_if_seq.rs +++ b/clippy_lints/src/let_if_seq.rs @@ -1,4 +1,4 @@ -use crate::utils::{snippet, span_lint_and_then}; +use crate::utils::{higher, snippet, span_lint_and_then}; use if_chain::if_chain; use rustc::hir; use rustc::hir::def::Res; @@ -63,7 +63,7 @@ fn check_block(&mut self, cx: &LateContext<'a, 'tcx>, block: &'tcx hir::Block) { if let hir::StmtKind::Local(ref local) = stmt.node; if let hir::PatKind::Binding(mode, canonical_id, ident, None) = local.pat.node; if let hir::StmtKind::Expr(ref if_) = expr.node; - if let hir::ExprKind::If(ref cond, ref then, ref else_) = if_.node; + if let Some((ref cond, ref then, ref else_)) = higher::if_block(&if_); if !used_in_expr(cx, canonical_id, cond); if let hir::ExprKind::Block(ref then, _) = then.node; if let Some(value) = check_assign(cx, canonical_id, &*then);