Skip to content

Conversation

y21
Copy link
Member

@y21 y21 commented Jul 17, 2023

Fixes #11174

changelog: [redundant_pattern_matching]: include guard in suggestion

@rustbot
Copy link
Collaborator

rustbot commented Jul 17, 2023

r? @Manishearth

(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 Jul 17, 2023
@Centri3
Copy link
Member

Centri3 commented Jul 17, 2023

This could probably add the guard (like && !boolean) to the suggestion, if it's not an if let guard ofc. This may have issues in let chains though where it'll need to be wrapped in parenthesis if there's a ||

Comment on lines +218 to +223
// wow, the HIR for match guards in `PAT if let PAT = expr && expr => ...` is annoying!
// `guard` here is `Guard::If` with the let expression somewhere deep in the tree of exprs,
// counter to the intuition that it should be `Guard::IfLet`, so we need another check
// to see that there aren't any let chains anywhere in the guard, as that would break
// if we suggest `t.is_none() && (let X = y && z)` for:
// `match t { None if let X = y && z => true, _ => false }`
Copy link
Member Author

@y21 y21 Jul 17, 2023

Choose a reason for hiding this comment

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

I feel like this comment might be a bit confusing. If you ( the reviewer(s) ) already understand what this means, feel free to ignore.
Looking at the HIR here: https://godbolt.org/z/8413T1rjP (line 521-522), the guard is not Guard::IfLet, but Guard::If, with the let expression being potentially somewhere deep in the expression tree, so just checking Guard::If is not enough -- it may still be an if let guard despite the guard not being Guard::IfLet

Copy link
Member

@Centri3 Centri3 Jul 17, 2023

Choose a reason for hiding this comment

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

This is perhaps because it's desugared to something akin a let-chain, or at least something very similar, which is like And<Let, And<Expr, And<Expr, Let>>> or something in the HIR, with Expr being a regular && and Let being && let, so you can probably check the first in Binary::And afaik but perhaps this would have issues on something like if true && let Some(x) = x, I'm not sure. Better safe than sorry though so the current way is fine

Just if let Some(_) = x on its own is IfLet. I suppose it's because IfLet only allows one Let, while it needs a BinaryKind::And for a let chain to be represented

@y21 y21 changed the title [redundant_pattern_matching]: don't lint if match guards are present [redundant_pattern_matching]: include guard in suggestion Jul 17, 2023
@Manishearth
Copy link
Member

@bors r+

comment seems fine to me. it's confusing but the situation is confusing

@bors
Copy link
Contributor

bors commented Jul 18, 2023

📌 Commit e7fd44f has been approved by Manishearth

It is now in the queue for this repository.

@bors
Copy link
Contributor

bors commented Jul 18, 2023

⌛ Testing commit e7fd44f with merge b0126c3...

bors added a commit that referenced this pull request Jul 18, 2023
[`redundant_pattern_matching`]: include guard in suggestion

Fixes #11174

[`redundant_pattern_matching`]: include guard in suggestion
@bors
Copy link
Contributor

bors commented Jul 18, 2023

💔 Test failed - checks-action_test

@y21
Copy link
Member Author

y21 commented Jul 18, 2023

oops, I must have deleted the "changelog:" word from my PR description somehow. I edited it in now.

@Manishearth
Copy link
Member

@bors retry

@bors
Copy link
Contributor

bors commented Jul 18, 2023

⌛ Testing commit e7fd44f with merge d4a6634...

@bors
Copy link
Contributor

bors commented Jul 18, 2023

☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test
Approved by: Manishearth
Pushing d4a6634 to master...

@bors bors merged commit d4a6634 into rust-lang:master Jul 18, 2023
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.

Invalid warning on redundant_pattern_matching
5 participants