Skip to content

Make collapsible_if recognize the let_chains feature #14481

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

Merged
merged 3 commits into from
Mar 28, 2025

Conversation

samueltardieu
Copy link
Contributor

Until if let chains are stabilized, we do not collapse them together or with other if expressions unless the let_chains feature is enabled. This is the case for example in Clippy sources.

This was made possible by converting the collapsible_if to a late lint to get access to the set of enabled features. This allows this PR to supersede #14455 and no longer require an additional configuration option.

The three commits are, in order:

  • a conversion of the existing early lint to a late lint, with no new feature or tests changes
  • the addition of the let_chains feature detection and action, and tests
  • the application of the enhanced lint to Clippy sources (136 files modified)

changelog: [collapsible_if]: recognize the rust compiler let_chains feature

r? @flip1995

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Mar 26, 2025
Copy link
Member

@flip1995 flip1995 left a comment

Choose a reason for hiding this comment

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

Was the 3rd commit auto-applied with dogfood fix?

Comment on lines 205 to 214
/// If `block` is a block with either one expression or a statement containing an expression,
/// return the expression.
fn expr_block<'tcx>(block: &Block<'tcx>) -> Option<&'tcx Expr<'tcx>> {
match block.stmts {
[] => block.expr,
[stmt] => {
if let StmtKind::Semi(expr) = stmt.kind {
Some(expr)
} else {
None
}
},
_ => None,
}
}
Copy link
Member

Choose a reason for hiding this comment

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

Could this better be implemented with clippy_utils::peel_blocks and then just "unpack" the statement or return the expression?

Or are you intentionally only removing 1 block?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am intentionally removing only one block, the top-level one in the then parts, as multiple blocks would probably indicate a design choice, and ought to be flagged by another lint if this is not needed.

Copy link
Member

Choose a reason for hiding this comment

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

Makes sense. In that case, please extend the comment to note that this is intentionally not using peel_blocks

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

Self {
let_chains_enabled: tcx.features().enabled(sym::let_chains),
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
let_chains_enabled: tcx.features().enabled(sym::let_chains),
let_chains_enabled: tcx.features().let_chains(),

I think this is the better approach. With that, the person stabilizing this feature in rustc is also forced to remove this check.

@samueltardieu
Copy link
Contributor Author

Was the 3rd commit auto-applied with dogfood fix?

Yes, totally automated, with the addition of this additional whitespace reformatting on top at a few places where rustfmt didn't reindent.

@samueltardieu samueltardieu force-pushed the push-mkzzlozzzxup branch 3 times, most recently from 1bf1a03 to dee46f2 Compare March 27, 2025 12:26
@samueltardieu samueltardieu requested a review from flip1995 March 27, 2025 12:36
Until `if let` chains are stabilized, we do not collapse them together
or with other `if` expressions unless the `let_chains` feature is
enabled. This is the case for example in Clippy sources.
Since Clippy uses the `let_chains` feature, there are many occasions to
collapse `if` and `if let` statements.
Copy link
Member

@flip1995 flip1995 left a comment

Choose a reason for hiding this comment

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

Thanks!

@flip1995 flip1995 added this pull request to the merge queue Mar 28, 2025
Merged via the queue into rust-lang:master with commit c2922d1 Mar 28, 2025
11 checks passed
@samueltardieu samueltardieu deleted the push-mkzzlozzzxup branch March 28, 2025 11:18
@y21 y21 mentioned this pull request Apr 23, 2025
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.

3 participants