New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
expending lint [blocks_in_if_conditions
] to check match expr as well
#11853
Conversation
r? @llogiq (rustbot has picked a reviewer for you, use r? to override) |
53fecbd
to
b747ee2
Compare
let Some((cond, keyword)) = higher::If::hir(expr).map(|hif| (hif.cond, "if")).or( | ||
if let ExprKind::Match(match_ex, _, MatchSource::Normal) = expr.kind { | ||
Some((match_ex, "match")) | ||
} else { | ||
None | ||
}, | ||
) else { | ||
return; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmmm, the rendering is messed up by auto formatting,
but here is what has changed, hoping this will make things a bit clearer
@@ -52,88 +52,89 @@ impl<'tcx> LateLintPass<'tcx> for BlocksInIfConditions { | |||
if in_external_macro(cx.sess(), expr.span) { | |||
return; | |||
} | |||
if let Some(higher::If { cond, .. }) = higher::If::hir(expr) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
replaced by the above statement
COMPLEX_BLOCK_MESSAGE, | ||
"try", | ||
format!( | ||
"let res = {}; {keyword} res", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if -> {keyword}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks mostly good, but the error message for matches needs some work. Also we may want to rename the lint to block_in_condition
or some such, because it now isn't restricted to if conditions.
format!( | ||
"let res = {}; if res", | ||
"{}", | ||
snippet_block_with_applicability( | ||
cx, | ||
block.span, | ||
ex.span, | ||
"..", | ||
Some(expr.span), | ||
&mut applicability | ||
), | ||
) | ||
), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This format!("{}", _)
can be reduced to _.to_string()
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks, kinda weird this wasn't detected by useless_format
and... me 😆
@@ -32,5 +32,22 @@ LL | if true && x == 3 { 6 } else { 10 } | |||
= note: `-D clippy::nonminimal-bool` implied by `-D warnings` | |||
= help: to override `-D warnings` add `#[allow(clippy::nonminimal_bool)]` | |||
|
|||
error: aborting due to 3 previous errors | |||
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` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should also change the error message for match
, as there is no if
condition. Instead let's call it match
scrutinee.
@@ -61,4 +61,16 @@ fn block_in_assert() { | |||
); | |||
} | |||
|
|||
// issue #11814 | |||
fn block_in_match_expr(num: i32) -> i32 { | |||
match { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like to see a test with an unsafe block, too (just so be sure it's handled correctly).
I agree, I'll use that name as I couldn't think of a better name~
|
2f8846c
to
6084294
Compare
8a1bcaf
to
40b558a
Compare
add more test cases with `match`; minor fixes in message output regarding review feedback
Thank you, this looks good to merge now. @bors r+ |
☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test |
``` warning: in a `match` scrutinee, avoid complex blocks or closures with blocks; instead, move the block or closure higher and bind it with a `let` --> init/src/main.rs:127:23 | 127 | match (|| { | _______________________^ 128 | | let entry = entry.context("read_dir(/bin) failed")?; 129 | | let path = entry.path(); 130 | | let status = std::process::Command::new(&path) ... | 139 | | } 140 | | })() { | |_____________^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#blocks_in_conditions = note: `#[warn(clippy::blocks_in_conditions)]` on by default ``` rust-lang/rust-clippy#11853 landed in nightly.
closes: #11814
changelog: rename lint
blocks_in_if_conditions
to [blocks_in_conditions
] and expand it to check blocks in match scrutinees