From d36c785344bdbfffa3c0424c0459abe921c63270 Mon Sep 17 00:00:00 2001 From: flip1995 Date: Tue, 27 Aug 2019 09:43:03 +0200 Subject: [PATCH 1/2] Allow block_in_if_{stmt,expr} in external macro --- clippy_lints/src/block_in_if_condition.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/clippy_lints/src/block_in_if_condition.rs b/clippy_lints/src/block_in_if_condition.rs index c2ff8c83373b..f9660ce5efed 100644 --- a/clippy_lints/src/block_in_if_condition.rs +++ b/clippy_lints/src/block_in_if_condition.rs @@ -2,7 +2,7 @@ use crate::utils::*; use matches::matches; use rustc::hir::intravisit::{walk_expr, NestedVisitorMap, Visitor}; use rustc::hir::*; -use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass}; +use rustc::lint::{in_external_macro, LateContext, LateLintPass, LintArray, LintContext, LintPass}; use rustc::{declare_lint_pass, declare_tool_lint}; declare_clippy_lint! { @@ -72,6 +72,9 @@ const COMPLEX_BLOCK_MESSAGE: &str = "in an 'if' condition, avoid complex blocks impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BlockInIfCondition { fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) { + if in_external_macro(cx.sess(), expr.span) { + return; + } if let Some((check, then, _)) = higher::if_block(&expr) { if let ExprKind::Block(block, _) = &check.node { if block.rules == DefaultBlock { From e7d8cf85117c680f3e6dccc87b210a85512b9850 Mon Sep 17 00:00:00 2001 From: flip1995 Date: Tue, 27 Aug 2019 09:43:19 +0200 Subject: [PATCH 2/2] Add test for external macro --- tests/ui/block_in_if_condition.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/ui/block_in_if_condition.rs b/tests/ui/block_in_if_condition.rs index 10342ed28b57..50f238814a31 100644 --- a/tests/ui/block_in_if_condition.rs +++ b/tests/ui/block_in_if_condition.rs @@ -103,3 +103,15 @@ fn macro_in_closure() { unimplemented!() } } + +fn block_in_assert() { + let opt = Some(42); + assert!(opt + .as_ref() + .and_then(|val| { + let mut v = val * 2; + v -= 1; + Some(v * 3) + }) + .is_some()); +}