Skip to content

Commit 8af4e09

Browse files
committed
Auto merge of #4458 - flip1995:block_in_if_ext_macro, r=phansch
Allow block_in_if_{stmt,expr} in external macro I found this by running `cargo fix --clippy` on quite a big codebase. You could refactor this assert to ```rust let block_expr = _; assert!(block_expr); ``` but, 1. it doesn't increase the readability IMO 2. That isn't possible in a `debug_assert!` I'm not sure though, if we should allow this for macros in general or just for external macros. changelog: Allow `block_in_if_{stmt,expr}` in external macros
2 parents 144d940 + e7d8cf8 commit 8af4e09

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

clippy_lints/src/block_in_if_condition.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::utils::*;
22
use matches::matches;
33
use rustc::hir::intravisit::{walk_expr, NestedVisitorMap, Visitor};
44
use rustc::hir::*;
5-
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
5+
use rustc::lint::{in_external_macro, LateContext, LateLintPass, LintArray, LintContext, LintPass};
66
use rustc::{declare_lint_pass, declare_tool_lint};
77

88
declare_clippy_lint! {
@@ -72,6 +72,9 @@ const COMPLEX_BLOCK_MESSAGE: &str = "in an 'if' condition, avoid complex blocks
7272

7373
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BlockInIfCondition {
7474
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
75+
if in_external_macro(cx.sess(), expr.span) {
76+
return;
77+
}
7578
if let Some((check, then, _)) = higher::if_block(&expr) {
7679
if let ExprKind::Block(block, _) = &check.node {
7780
if block.rules == DefaultBlock {

tests/ui/block_in_if_condition.rs

+12
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,15 @@ fn macro_in_closure() {
103103
unimplemented!()
104104
}
105105
}
106+
107+
fn block_in_assert() {
108+
let opt = Some(42);
109+
assert!(opt
110+
.as_ref()
111+
.and_then(|val| {
112+
let mut v = val * 2;
113+
v -= 1;
114+
Some(v * 3)
115+
})
116+
.is_some());
117+
}

0 commit comments

Comments
 (0)