More uses of higher::if_block

This commit is contained in:
Manish Goregaokar
2019-05-10 19:34:47 -07:00
parent 69b1da4d82
commit c9ed92ce20
4 changed files with 12 additions and 14 deletions

View File

@@ -72,7 +72,7 @@ const COMPLEX_BLOCK_MESSAGE: &str = "in an 'if' condition, avoid complex blocks
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BlockInIfCondition { impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BlockInIfCondition {
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) { 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 let ExprKind::Block(block, _) = &check.node {
if block.rules == DefaultBlock { if block.rules == DefaultBlock {
if block.stmts.is_empty() { if block.stmts.is_empty() {

View File

@@ -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 crate::utils::{SpanlessEq, SpanlessHash};
use rustc::hir::*; use rustc::hir::*;
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass}; 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) { fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
if !in_macro(expr.span) { if !in_macro(expr.span) {
// skip ifs directly in else, it will be checked in the parent if // skip ifs directly in else, it will be checked in the parent if
if let Some(&Expr { if let Some(expr) = get_parent_expr(cx, expr) {
node: ExprKind::If(_, _, Some(ref else_expr)), if let Some((_, _, Some(ref else_expr))) = higher::if_block(&expr) {
.. if else_expr.hir_id == expr.hir_id {
}) = get_parent_expr(cx, expr) return;
{ }
if else_expr.hir_id == expr.hir_id {
return;
} }
} }
@@ -236,7 +234,7 @@ fn if_sequence(mut expr: &Expr) -> (SmallVec<[&Expr; 1]>, SmallVec<[&Block; 1]>)
let mut conds = SmallVec::new(); let mut conds = SmallVec::new();
let mut blocks: SmallVec<[&Block; 1]> = 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); conds.push(&**cond);
if let ExprKind::Block(ref block, _) = then_expr.node { if let ExprKind::Block(ref block, _) = then_expr.node {
blocks.push(block); blocks.push(block);

View File

@@ -1,5 +1,5 @@
use crate::utils::SpanlessEq; 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 if_chain::if_chain;
use rustc::hir::intravisit::{walk_expr, NestedVisitorMap, Visitor}; use rustc::hir::intravisit::{walk_expr, NestedVisitorMap, Visitor};
use rustc::hir::*; use rustc::hir::*;
@@ -41,7 +41,7 @@ declare_lint_pass!(HashMapPass => [MAP_ENTRY]);
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for HashMapPass { impl<'a, 'tcx> LateLintPass<'a, 'tcx> for HashMapPass {
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) { 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 ExprKind::Unary(UnOp::UnNot, ref check) = check.node {
if let Some((ty, map, key)) = check_cond(cx, check) { if let Some((ty, map, key)) = check_cond(cx, check) {
// in case of `if !m.contains_key(&k) { m.insert(k, v); }` // in case of `if !m.contains_key(&k) { m.insert(k, v); }`

View File

@@ -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 if_chain::if_chain;
use rustc::hir; use rustc::hir;
use rustc::hir::def::Res; use rustc::hir::def::Res;
@@ -63,7 +63,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for LetIfSeq {
if let hir::StmtKind::Local(ref local) = stmt.node; 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::PatKind::Binding(mode, canonical_id, ident, None) = local.pat.node;
if let hir::StmtKind::Expr(ref if_) = expr.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 !used_in_expr(cx, canonical_id, cond);
if let hir::ExprKind::Block(ref then, _) = then.node; if let hir::ExprKind::Block(ref then, _) = then.node;
if let Some(value) = check_assign(cx, canonical_id, &*then); if let Some(value) = check_assign(cx, canonical_id, &*then);