Skip to content

Commit

Permalink
Auto merge of #4458 - flip1995:block_in_if_ext_macro, r=phansch
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bors committed Sep 9, 2019
2 parents 144d940 + e7d8cf8 commit 8af4e09
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
5 changes: 4 additions & 1 deletion clippy_lints/src/block_in_if_condition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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! {
Expand Down Expand Up @@ -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 {
Expand Down
12 changes: 12 additions & 0 deletions tests/ui/block_in_if_condition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

0 comments on commit 8af4e09

Please sign in to comment.