-
-
Notifications
You must be signed in to change notification settings - Fork 14.4k
Not linting irrefutable_let_patterns on let chains #146832
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
Open
Natural-selection1
wants to merge
4
commits into
rust-lang:main
Choose a base branch
from
Natural-selection1:not-in-chains
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+192
−280
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| // https://github.com/rust-lang/rust/issues/139369 | ||
| // Test that the lint `irrefutable_let_patterns` now | ||
| // only checks single let binding. | ||
| //@ edition: 2024 | ||
| //@ check-pass | ||
|
|
||
| #![feature(if_let_guard)] | ||
|
|
||
| use std::ops::Range; | ||
|
|
||
| fn main() { | ||
| let opt = Some(None..Some(1)); | ||
|
|
||
| // test `if let` | ||
| if let first = &opt {} | ||
| //~^ WARN irrefutable `if let` pattern | ||
|
|
||
| if let first = &opt && let Some(second) = first {} | ||
|
|
||
| if let first = &opt && let (a, b) = (1, 2) {} | ||
|
|
||
| if let first = &opt && let None = Some(1) {} | ||
|
|
||
| if 4 * 2 == 0 && let first = &opt {} | ||
|
|
||
| if let first = &opt | ||
| && let Some(second) = first | ||
| && let None = second.start | ||
| && let v = 0 | ||
| {} | ||
|
|
||
| if let Range { start: local_start, end: _ } = (None..Some(1)) {} | ||
| //~^ WARN irrefutable `if let` pattern | ||
|
|
||
| if let Range { start: local_start, end: _ } = (None..Some(1)) | ||
| && let None = local_start | ||
| {} | ||
|
|
||
| if let (a, b, c) = (Some(1), Some(1), Some(1)) {} | ||
| //~^ WARN irrefutable `if let` pattern | ||
|
|
||
| if let (a, b, c) = (Some(1), Some(1), Some(1)) && let None = Some(1) {} | ||
|
|
||
| if let Some(ref first) = opt | ||
| && let Range { start: local_start, end: _ } = first | ||
| && let None = local_start | ||
| {} | ||
|
|
||
| // test `else if let` | ||
| if opt == Some(None..None) { | ||
| } else if let x = opt.clone().map(|_| 1) { | ||
| //~^ WARN irrefutable `if let` pattern | ||
| } | ||
|
|
||
| if opt == Some(None..None) { | ||
| } else if let x = opt.clone().map(|_| 1) | ||
| && x == Some(1) | ||
| {} | ||
|
|
||
| if opt == Some(None..None) { | ||
| } else if opt.is_some() && let x = &opt | ||
| {} | ||
|
|
||
| if opt == Some(None..None) { | ||
| } else { | ||
| if let x = opt.clone().map(|_| 1) && x == Some(1) | ||
| {} | ||
| } | ||
|
|
||
| // test `if let guard` | ||
| match opt { | ||
| Some(ref first) if let second = first => {} | ||
| //~^ WARN irrefutable `if let` guard pattern | ||
| _ => {} | ||
| } | ||
|
|
||
| match opt { | ||
| Some(ref first) | ||
| if let second = first | ||
| && let _third = second | ||
| && let v = 4 + 4 => {} | ||
| _ => {} | ||
| } | ||
|
|
||
| match opt { | ||
| Some(ref first) | ||
| if let Range { start: local_start, end: _ } = first | ||
| && let None = local_start => {} | ||
| _ => {} | ||
| } | ||
|
|
||
| match opt { | ||
| Some(ref first) | ||
| if let Range { start: Some(_), end: local_end } = first | ||
| && let v = local_end | ||
| && let w = v => {} | ||
| _ => {} | ||
| } | ||
|
|
||
| // test `while let` | ||
| while let first = &opt {} | ||
| //~^ WARN irrefutable `while let` pattern | ||
|
|
||
| while let first = &opt | ||
| && let (a, b) = (1, 2) | ||
| {} | ||
|
|
||
| while let first = &opt | ||
| && let Some(second) = first | ||
| && let None = second.start | ||
| {} | ||
|
|
||
| while let Some(ref first) = opt | ||
| && let second = first | ||
| && let _third = second | ||
| {} | ||
|
|
||
| while let Some(ref first) = opt | ||
| && let Range { start: local_start, end: _ } = first | ||
| && let None = local_start | ||
| {} | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| warning: irrefutable `if let` pattern | ||
| --> $DIR/irrefutable-in-let-chains.rs:15:8 | ||
| | | ||
| LL | if let first = &opt {} | ||
| | ^^^^^^^^^^^^^^^^ | ||
| | | ||
| = note: this pattern will always match, so the `if let` is useless | ||
| = help: consider replacing the `if let` with a `let` | ||
| = note: `#[warn(irrefutable_let_patterns)]` on by default | ||
|
|
||
| warning: irrefutable `if let` pattern | ||
| --> $DIR/irrefutable-in-let-chains.rs:32:8 | ||
| | | ||
| LL | if let Range { start: local_start, end: _ } = (None..Some(1)) {} | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| = note: this pattern will always match, so the `if let` is useless | ||
| = help: consider replacing the `if let` with a `let` | ||
|
|
||
| warning: irrefutable `if let` pattern | ||
| --> $DIR/irrefutable-in-let-chains.rs:39:8 | ||
| | | ||
| LL | if let (a, b, c) = (Some(1), Some(1), Some(1)) {} | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| = note: this pattern will always match, so the `if let` is useless | ||
| = help: consider replacing the `if let` with a `let` | ||
|
|
||
| warning: irrefutable `if let` pattern | ||
| --> $DIR/irrefutable-in-let-chains.rs:51:15 | ||
| | | ||
| LL | } else if let x = opt.clone().map(|_| 1) { | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| = note: this pattern will always match, so the `if let` is useless | ||
| = help: consider replacing the `if let` with a `let` | ||
|
|
||
| warning: irrefutable `if let` guard pattern | ||
| --> $DIR/irrefutable-in-let-chains.rs:72:28 | ||
| | | ||
| LL | Some(ref first) if let second = first => {} | ||
| | ^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| = note: this pattern will always match, so the guard is useless | ||
| = help: consider removing the guard and adding a `let` inside the match arm | ||
|
|
||
| warning: irrefutable `while let` pattern | ||
| --> $DIR/irrefutable-in-let-chains.rs:101:11 | ||
| | | ||
| LL | while let first = &opt {} | ||
| | ^^^^^^^^^^^^^^^^ | ||
| | | ||
| = note: this pattern will always match, so the loop will never exit | ||
| = help: consider instead using a `loop { ... }` with a `let` inside it | ||
|
|
||
| warning: 6 warnings emitted | ||
|
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
What if there are multiple bindings, all irrefutable?
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.
Not linting as long as it is chains. In #139369 , people have nearly reached a consensus on this point.