From a2ad0c66953cae54d43773fa51fa98e2d68d10d7 Mon Sep 17 00:00:00 2001 From: llogiq Date: Sun, 31 Jan 2016 23:25:10 +0100 Subject: [PATCH] fixed #528 --- src/block_in_if_condition.rs | 4 +++- tests/compile-fail/block_in_if_condition.rs | 13 ++++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/block_in_if_condition.rs b/src/block_in_if_condition.rs index 53f11366c..0162ef92d 100644 --- a/src/block_in_if_condition.rs +++ b/src/block_in_if_condition.rs @@ -92,7 +92,9 @@ impl LateLintPass for BlockInIfCondition { snippet_block(cx, then.span, ".."))); } } else { - if in_macro(cx, expr.span) || differing_macro_contexts(expr.span, block.stmts[0].span) { + let span = block.expr.as_ref().map_or_else(|| block.stmts[0].span, + |e| e.span); + if in_macro(cx, span) || differing_macro_contexts(expr.span, span) { return; } // move block higher diff --git a/tests/compile-fail/block_in_if_condition.rs b/tests/compile-fail/block_in_if_condition.rs index 0a68d80c3..5158e74c1 100644 --- a/tests/compile-fail/block_in_if_condition.rs +++ b/tests/compile-fail/block_in_if_condition.rs @@ -3,17 +3,28 @@ #![deny(block_in_if_condition_expr)] #![deny(block_in_if_condition_stmt)] -#![allow(unused)] +#![allow(unused, let_and_return)] macro_rules! blocky { () => {{true}} } +macro_rules! blocky_too { + () => {{ + let r = true; + r + }} +} + fn macro_if() { if blocky!() { } + + if blocky_too!() { + } } + fn condition_has_block() -> i32 { if { //~ERROR in an 'if' condition, avoid complex blocks or closures with blocks; instead, move the block or closure higher and bind it with a 'let'