Skip to content

Commit 9b20212

Browse files
committed
Auto merge of rust-lang#12040 - J-ZhengLi:issue12016, r=y21
stop linting [`blocks_in_conditions`] on `match` with weird attr macro case should fixes: rust-lang#12016 --- changelog: [`blocks_in_conditions`] - fix FP on `match` with weird attr macro This might not be the best solution, as the root cause (i think?) is the `span` of block was incorrectly given by the compiler? I'm open to better solutions
2 parents ae38a67 + 4cc7b7e commit 9b20212

File tree

5 files changed

+52
-6
lines changed

5 files changed

+52
-6
lines changed

clippy_lints/src/blocks_in_conditions.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use clippy_utils::diagnostics::{span_lint, span_lint_and_sugg};
22
use clippy_utils::source::snippet_block_with_applicability;
33
use clippy_utils::ty::implements_trait;
44
use clippy_utils::visitors::{for_each_expr, Descend};
5-
use clippy_utils::{get_parent_expr, higher};
5+
use clippy_utils::{get_parent_expr, higher, is_from_proc_macro};
66
use core::ops::ControlFlow;
77
use rustc_errors::Applicability;
88
use rustc_hir::{BlockCheckMode, Expr, ExprKind, MatchSource};
@@ -94,7 +94,7 @@ impl<'tcx> LateLintPass<'tcx> for BlocksInConditions {
9494
}
9595
} else {
9696
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() {
97+
if span.from_expansion() || expr.span.from_expansion() || is_from_proc_macro(cx, cond) {
9898
return;
9999
}
100100
// move block higher

tests/ui/auxiliary/proc_macro_attr.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,23 @@ pub fn fake_main(_attr: TokenStream, item: TokenStream) -> TokenStream {
125125
}
126126
.into()
127127
}
128+
129+
#[proc_macro_attribute]
130+
pub fn fake_desugar_await(_args: TokenStream, input: TokenStream) -> TokenStream {
131+
let mut async_fn = syn::parse_macro_input!(input as syn::ItemFn);
132+
133+
for stmt in &mut async_fn.block.stmts {
134+
if let syn::Stmt::Expr(syn::Expr::Match(syn::ExprMatch { expr: scrutinee, .. }), _) = stmt {
135+
if let syn::Expr::Await(syn::ExprAwait { base, await_token, .. }) = scrutinee.as_mut() {
136+
let blc = quote_spanned!( await_token.span => {
137+
#[allow(clippy::let_and_return)]
138+
let __pinned = #base;
139+
__pinned
140+
});
141+
*scrutinee = parse_quote!(#blc);
142+
}
143+
}
144+
}
145+
146+
quote!(#async_fn).into()
147+
}

tests/ui/blocks_in_conditions.fixed

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//@aux-build:proc_macro_attr.rs
2+
13
#![warn(clippy::blocks_in_conditions)]
24
#![allow(unused, clippy::let_and_return, clippy::needless_if)]
35
#![warn(clippy::nonminimal_bool)]
@@ -99,4 +101,15 @@ fn issue_12162() {
99101
}
100102
}
101103

104+
mod issue_12016 {
105+
#[proc_macro_attr::fake_desugar_await]
106+
pub async fn await_becomes_block() -> i32 {
107+
match Some(1).await {
108+
Some(1) => 2,
109+
Some(2) => 3,
110+
_ => 0,
111+
}
112+
}
113+
}
114+
102115
fn main() {}

tests/ui/blocks_in_conditions.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//@aux-build:proc_macro_attr.rs
2+
13
#![warn(clippy::blocks_in_conditions)]
24
#![allow(unused, clippy::let_and_return, clippy::needless_if)]
35
#![warn(clippy::nonminimal_bool)]
@@ -99,4 +101,15 @@ fn issue_12162() {
99101
}
100102
}
101103

104+
mod issue_12016 {
105+
#[proc_macro_attr::fake_desugar_await]
106+
pub async fn await_becomes_block() -> i32 {
107+
match Some(1).await {
108+
Some(1) => 2,
109+
Some(2) => 3,
110+
_ => 0,
111+
}
112+
}
113+
}
114+
102115
fn main() {}

tests/ui/blocks_in_conditions.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
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`
2-
--> $DIR/blocks_in_conditions.rs:23:5
2+
--> $DIR/blocks_in_conditions.rs:25:5
33
|
44
LL | / if {
55
LL | |
@@ -20,13 +20,13 @@ LL ~ }; if res {
2020
|
2121

2222
error: omit braces around single expression condition
23-
--> $DIR/blocks_in_conditions.rs:35:8
23+
--> $DIR/blocks_in_conditions.rs:37:8
2424
|
2525
LL | if { true } { 6 } else { 10 }
2626
| ^^^^^^^^ help: try: `true`
2727

2828
error: this boolean expression can be simplified
29-
--> $DIR/blocks_in_conditions.rs:41:8
29+
--> $DIR/blocks_in_conditions.rs:43:8
3030
|
3131
LL | if true && x == 3 { 6 } else { 10 }
3232
| ^^^^^^^^^^^^^^ help: try: `x == 3`
@@ -35,7 +35,7 @@ LL | if true && x == 3 { 6 } else { 10 }
3535
= help: to override `-D warnings` add `#[allow(clippy::nonminimal_bool)]`
3636

3737
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`
38-
--> $DIR/blocks_in_conditions.rs:68:5
38+
--> $DIR/blocks_in_conditions.rs:70:5
3939
|
4040
LL | / match {
4141
LL | |

0 commit comments

Comments
 (0)