Skip to content

Fix collapsible_match false negative on if let chains (#16570) - #16624

Open
0x-pankaj wants to merge 1 commit into
rust-lang:masterfrom
0x-pankaj:fix/collapsible-match-if-let-chain-16570
Open

Fix collapsible_match false negative on if let chains (#16570)#16624
0x-pankaj wants to merge 1 commit into
rust-lang:masterfrom
0x-pankaj:fix/collapsible-match-if-let-chain-16570

Conversation

@0x-pankaj

@0x-pankaj 0x-pankaj commented Feb 23, 2026

Copy link
Copy Markdown

Fix collapsible_match false negative when the outer match is inside an if let chain.

Previously, collapsible_match only checked simple if let expressions. This PR adds support for let chains (e.g., if true && let Some(x) = opt { match x { ... } }) by:

  • Adding IfLet::hir_all() to clippy_utils/src/higher.rs to parse all let bindings in an if condition
  • Updating matches/mod.rs to iterate over all let bindings in a chain
  • Passing the full if condition as an outer_guard to check_if_let so the binding isn't incorrectly collapsed when used elsewhere in the chain

fixes #16570

@profetia

changelog: [collapsible_match]: Fix false negative when match is inside an if let chain

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Feb 23, 2026
@rustbot

rustbot commented Feb 23, 2026

Copy link
Copy Markdown
Collaborator

r? @dswij

rustbot has assigned @dswij.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: 7 candidates
  • 7 candidates expanded to 7 candidates
  • Random selection from Jarcho, dswij, llogiq, samueltardieu

@rustbot

This comment has been minimized.

@0x-pankaj
0x-pankaj force-pushed the fix/collapsible-match-if-let-chain-16570 branch from e7fd5f3 to 8c7ccf2 Compare February 23, 2026 21:25
Comment thread tests/ui/issue_16570.rs
@@ -0,0 +1,30 @@
#![allow(clippy::collapsible_else_if, stable_features)]

@dswij dswij Apr 9, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

View changes since the review

Let's put the tests in this file together with the other collapsible_match tests in tests/ui/collapsible_match.rs

});
}
current = lhs;
}

@dswij dswij Apr 9, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

View changes since the review

Should probably factor the Let parsing out, seems to be duplicated here and in ::hir(..)

/// Parses an `if let` expression, but returns an iterator of all let chains in the condition.
/// E.g., `if let Some(x) = y && let Some(z) = w` will return an iterator of both let
/// expressions.
pub fn hir_all(cx: &LateContext<'_>, expr: &Expr<'hir>) -> Option<Vec<Self>> {

@dswij dswij Apr 9, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

View changes since the review

returning Vec<Self> here doesn't seem right since the original struct was written without let-chains in mind.

Perhaps it's better if we just have a new struct LetChain instead.

@dswij dswij added S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Apr 9, 2026
@rustbot

rustbot commented Apr 20, 2026

Copy link
Copy Markdown
Collaborator

☔ The latest upstream changes (possibly #16878) made this pull request unmergeable. Please resolve the merge conflicts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

collapsible_match FN on if let chain

3 participants