Skip to content

Commit

Permalink
Auto merge of rust-lang#12040 - J-ZhengLi:issue12016, r=y21
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bors committed Feb 11, 2024
2 parents ae38a67 + 4cc7b7e commit 9b20212
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 6 deletions.
4 changes: 2 additions & 2 deletions clippy_lints/src/blocks_in_conditions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use clippy_utils::diagnostics::{span_lint, span_lint_and_sugg};
use clippy_utils::source::snippet_block_with_applicability;
use clippy_utils::ty::implements_trait;
use clippy_utils::visitors::{for_each_expr, Descend};
use clippy_utils::{get_parent_expr, higher};
use clippy_utils::{get_parent_expr, higher, is_from_proc_macro};
use core::ops::ControlFlow;
use rustc_errors::Applicability;
use rustc_hir::{BlockCheckMode, Expr, ExprKind, MatchSource};
Expand Down Expand Up @@ -94,7 +94,7 @@ impl<'tcx> LateLintPass<'tcx> for BlocksInConditions {
}
} else {
let span = block.expr.as_ref().map_or_else(|| block.stmts[0].span, |e| e.span);
if span.from_expansion() || expr.span.from_expansion() {
if span.from_expansion() || expr.span.from_expansion() || is_from_proc_macro(cx, cond) {
return;
}
// move block higher
Expand Down
20 changes: 20 additions & 0 deletions tests/ui/auxiliary/proc_macro_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,23 @@ pub fn fake_main(_attr: TokenStream, item: TokenStream) -> TokenStream {
}
.into()
}

#[proc_macro_attribute]
pub fn fake_desugar_await(_args: TokenStream, input: TokenStream) -> TokenStream {
let mut async_fn = syn::parse_macro_input!(input as syn::ItemFn);

for stmt in &mut async_fn.block.stmts {
if let syn::Stmt::Expr(syn::Expr::Match(syn::ExprMatch { expr: scrutinee, .. }), _) = stmt {
if let syn::Expr::Await(syn::ExprAwait { base, await_token, .. }) = scrutinee.as_mut() {
let blc = quote_spanned!( await_token.span => {
#[allow(clippy::let_and_return)]
let __pinned = #base;
__pinned
});
*scrutinee = parse_quote!(#blc);
}
}
}

quote!(#async_fn).into()
}
13 changes: 13 additions & 0 deletions tests/ui/blocks_in_conditions.fixed
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//@aux-build:proc_macro_attr.rs

#![warn(clippy::blocks_in_conditions)]
#![allow(unused, clippy::let_and_return, clippy::needless_if)]
#![warn(clippy::nonminimal_bool)]
Expand Down Expand Up @@ -99,4 +101,15 @@ fn issue_12162() {
}
}

mod issue_12016 {
#[proc_macro_attr::fake_desugar_await]
pub async fn await_becomes_block() -> i32 {
match Some(1).await {
Some(1) => 2,
Some(2) => 3,
_ => 0,
}
}
}

fn main() {}
13 changes: 13 additions & 0 deletions tests/ui/blocks_in_conditions.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//@aux-build:proc_macro_attr.rs

#![warn(clippy::blocks_in_conditions)]
#![allow(unused, clippy::let_and_return, clippy::needless_if)]
#![warn(clippy::nonminimal_bool)]
Expand Down Expand Up @@ -99,4 +101,15 @@ fn issue_12162() {
}
}

mod issue_12016 {
#[proc_macro_attr::fake_desugar_await]
pub async fn await_becomes_block() -> i32 {
match Some(1).await {
Some(1) => 2,
Some(2) => 3,
_ => 0,
}
}
}

fn main() {}
8 changes: 4 additions & 4 deletions tests/ui/blocks_in_conditions.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
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`
--> $DIR/blocks_in_conditions.rs:23:5
--> $DIR/blocks_in_conditions.rs:25:5
|
LL | / if {
LL | |
Expand All @@ -20,13 +20,13 @@ LL ~ }; if res {
|

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

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

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`
--> $DIR/blocks_in_conditions.rs:68:5
--> $DIR/blocks_in_conditions.rs:70:5
|
LL | / match {
LL | |
Expand Down

0 comments on commit 9b20212

Please sign in to comment.