Skip to content

Commit 85f98e0

Browse files
committed
stop linting [blocks_in_conditions] on match with weird attr macro case
1 parent 92537a0 commit 85f98e0

File tree

3 files changed

+23
-12
lines changed

3 files changed

+23
-12
lines changed

clippy_lints/src/blocks_in_conditions.rs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ use clippy_utils::visitors::{for_each_expr, Descend};
55
use clippy_utils::{get_parent_expr, higher};
66
use core::ops::ControlFlow;
77
use rustc_errors::Applicability;
8-
use rustc_hir::{BlockCheckMode, Expr, ExprKind, MatchSource};
8+
use rustc_hir::{Block, BlockCheckMode, Expr, ExprKind, MatchSource};
99
use rustc_lint::{LateContext, LateLintPass, LintContext};
1010
use rustc_middle::lint::in_external_macro;
1111
use rustc_session::declare_lint_pass;
12-
use rustc_span::sym;
12+
use rustc_span::{sym, Span};
1313

1414
declare_clippy_lint! {
1515
/// ### What it does
@@ -93,8 +93,7 @@ impl<'tcx> LateLintPass<'tcx> for BlocksInConditions {
9393
);
9494
}
9595
} else {
96-
let span = block.expr.as_ref().map_or_else(|| block.stmts[0].span, |e| e.span);
97-
if span.from_expansion() || expr.span.from_expansion() {
96+
if block_is_from_expansion(block, expr.span) {
9897
return;
9998
}
10099
// move block higher
@@ -140,3 +139,21 @@ impl<'tcx> LateLintPass<'tcx> for BlocksInConditions {
140139
}
141140
}
142141
}
142+
143+
fn block_is_from_expansion(block: &Block<'_>, block_span: Span) -> bool {
144+
let block_expr_span = block.expr.map(|e| e.span);
145+
let block_first_stmt_span = block.stmts.first().map(|st| st.span);
146+
block_span.from_expansion()
147+
|| matches!(block_expr_span, Some(sp) if sp.from_expansion())
148+
|| matches!(block_first_stmt_span, Some(sp) if sp.from_expansion())
149+
|| {
150+
// HACK: When expr and stmt overlaps, assume it was modified by some macro.
151+
if let Some(expr_span) = block_expr_span
152+
&& let Some(stmt_span) = block_first_stmt_span
153+
{
154+
expr_span.overlaps(stmt_span)
155+
} else {
156+
false
157+
}
158+
}
159+
}

tests/ui/blocks_in_conditions.fixed

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ fn issue_12162() {
104104
mod issue_12016 {
105105
#[proc_macro_attr::fake_desugar_await]
106106
pub async fn await_becomes_block() -> i32 {
107-
let res = await; match res {
107+
match Some(1).await {
108108
Some(1) => 2,
109109
Some(2) => 3,
110110
_ => 0,

tests/ui/blocks_in_conditions.stderr

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,5 @@ LL + opt
5353
LL ~ }; match res {
5454
|
5555

56-
error: in a `match` scrutinee, avoid complex blocks or closures with blocks; instead, move the block or closure higher and bind it with a `let`
57-
--> $DIR/blocks_in_conditions.rs:107:9
58-
|
59-
LL | match Some(1).await {
60-
| ^^^^^^^^^^^^^^^^^^^ help: try: `let res = await; match res`
61-
62-
error: aborting due to 5 previous errors
56+
error: aborting due to 4 previous errors
6357

0 commit comments

Comments
 (0)