Skip to content
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

Merged
merged 2 commits into from Dec 2, 2023

Conversation

J-ZhengLi
Copy link
Member

@J-ZhengLi J-ZhengLi commented Nov 22, 2023

closes: #11814

changelog: rename lint blocks_in_if_conditions to [blocks_in_conditions] and expand it to check blocks in match scrutinees

@rustbot
Copy link
Collaborator

rustbot commented Nov 22, 2023

r? @llogiq

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Nov 22, 2023
Comment on lines 55 to 63
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;
};
Copy link
Member Author

@J-ZhengLi J-ZhengLi Nov 22, 2023

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) {
Copy link
Member Author

@J-ZhengLi J-ZhengLi Nov 22, 2023

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",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if -> {keyword}

Copy link
Contributor

@llogiq llogiq left a 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.

Comment on lines 80 to 89
format!(
"let res = {}; if res",
"{}",
snippet_block_with_applicability(
cx,
block.span,
ex.span,
"..",
Some(expr.span),
&mut applicability
),
)
),
Copy link
Contributor

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().

Copy link
Member Author

@J-ZhengLi J-ZhengLi Nov 30, 2023

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`
Copy link
Contributor

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 {
Copy link
Contributor

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).

@J-ZhengLi
Copy link
Member Author

J-ZhengLi commented Nov 30, 2023

... we may want to rename the lint to block_in_condition or some such

I agree, I'll use that name as I couldn't think of a better name~

just find it interesting that this lint has been renamed from block_in_if_condition_expr to block_in_if_condition, and now it's becoming block_in_condition 🤣 , maybe one day it'll becomes just "block"

Oof, changing the name causes a lot damage

@J-ZhengLi J-ZhengLi force-pushed the issue11814 branch 4 times, most recently from 8a1bcaf to 40b558a Compare November 30, 2023 07:41
add more test cases with `match`;
minor fixes in message output regarding review feedback
@llogiq
Copy link
Contributor

llogiq commented Dec 2, 2023

Thank you, this looks good to merge now.

@bors r+

@bors
Copy link
Collaborator

bors commented Dec 2, 2023

📌 Commit 40b558a has been approved by llogiq

It is now in the queue for this repository.

@bors
Copy link
Collaborator

bors commented Dec 2, 2023

⌛ Testing commit 40b558a with merge 75bdbfc...

@bors
Copy link
Collaborator

bors commented Dec 2, 2023

☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test
Approved by: llogiq
Pushing 75bdbfc to master...

@bors bors merged commit 75bdbfc into rust-lang:master Dec 2, 2023
8 checks passed
tamird added a commit to tamird/aya that referenced this pull request Dec 18, 2023
```
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties
Projects
None yet
Development

Successfully merging this pull request may close these issues.

expending lint [blocks_in_if_conditions] to check not only if's conditions, but also match's as welll
4 participants