Skip to content

Commit d933dab

Browse files
committed
stop linting [blocks_in_conditions] on match with weird attr macro case
1 parent 817a287 commit d933dab

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
@@ -88,8 +88,7 @@ impl<'tcx> LateLintPass<'tcx> for BlocksInConditions {
8888
);
8989
}
9090
} else {
91-
let span = block.expr.as_ref().map_or_else(|| block.stmts[0].span, |e| e.span);
92-
if span.from_expansion() || expr.span.from_expansion() {
91+
if block_is_from_expansion(&block, expr.span) {
9392
return;
9493
}
9594
// move block higher
@@ -135,3 +134,21 @@ impl<'tcx> LateLintPass<'tcx> for BlocksInConditions {
135134
}
136135
}
137136
}
137+
138+
fn block_is_from_expansion(block: &Block<'_>, block_span: Span) -> bool {
139+
let block_expr_span = block.expr.map(|e| e.span);
140+
let block_first_stmt_span = block.stmts.first().map(|st| st.span);
141+
block_span.from_expansion()
142+
|| matches!(block_expr_span, Some(sp) if sp.from_expansion())
143+
|| matches!(block_first_stmt_span, Some(sp) if sp.from_expansion())
144+
|| {
145+
// HACK: When expr and stmt overlaps, assume it was modified by some macro.
146+
if let Some(expr_span) = block_expr_span
147+
&& let Some(stmt_span) = block_first_stmt_span
148+
{
149+
expr_span.overlaps(stmt_span)
150+
} else {
151+
false
152+
}
153+
}
154+
}

tests/ui/blocks_in_conditions.fixed

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ fn block_in_match_expr(num: i32) -> i32 {
9090
mod issue_12016 {
9191
#[proc_macro_attr::fake_desugar_await]
9292
pub async fn await_becomes_block() -> i32 {
93-
let res = await; match res {
93+
match Some(1).await {
9494
Some(1) => 2,
9595
Some(2) => 3,
9696
_ => 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:93: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)