-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Fix incorrect pattern warning "unreachable pattern" #70413
Merged
bors
merged 2 commits into
rust-lang:master
from
AminArria:match-pattern-incorrect-warning
Mar 27, 2020
Merged
Changes from all commits
Commits
Show all changes
2 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 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 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 |
---|---|---|
|
@@ -360,7 +360,7 @@ fn check_arms<'p, 'tcx>( | |
let mut catchall = None; | ||
for (arm_index, (pat, id, has_guard)) in arms.iter().copied().enumerate() { | ||
let v = PatStack::from_pattern(pat); | ||
match is_useful(cx, &seen, &v, LeaveOutWitness, id, true) { | ||
match is_useful(cx, &seen, &v, LeaveOutWitness, id, has_guard, true) { | ||
NotUseful => { | ||
match source { | ||
hir::MatchSource::IfDesugar { .. } | hir::MatchSource::WhileDesugar => bug!(), | ||
|
@@ -410,7 +410,10 @@ fn check_not_useful<'p, 'tcx>( | |
) -> Result<(), Vec<super::Pat<'tcx>>> { | ||
let wild_pattern = cx.pattern_arena.alloc(super::Pat::wildcard_from_ty(ty)); | ||
let v = PatStack::from_pattern(wild_pattern); | ||
match is_useful(cx, matrix, &v, ConstructWitness, hir_id, true) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you leave a note re. the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added in 4b1aa9fa |
||
|
||
// false is given for `is_under_guard` argument due to the wildcard | ||
// pattern not having a guard | ||
match is_useful(cx, matrix, &v, ConstructWitness, hir_id, false, true) { | ||
NotUseful => Ok(()), // This is good, wildcard pattern isn't reachable. | ||
UsefulWithWitness(pats) => Err(if pats.is_empty() { | ||
bug!("Exhaustiveness check returned no witnesses") | ||
|
22 changes: 22 additions & 0 deletions
22
src/test/ui/or-patterns/issue-70413-no-unreachable-pat-and-guard.rs
This file contains 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,22 @@ | ||
// check-pass | ||
|
||
#![deny(unreachable_patterns)] | ||
|
||
#![feature(or_patterns)] | ||
fn main() { | ||
match (3,42) { | ||
(a,_) | (_,a) if a > 10 => {println!("{}", a)} | ||
_ => () | ||
} | ||
|
||
match Some((3,42)) { | ||
Some((a, _)) | Some((_, a)) if a > 10 => {println!("{}", a)} | ||
_ => () | ||
|
||
} | ||
|
||
match Some((3,42)) { | ||
Some((a, _) | (_, a)) if a > 10 => {println!("{}", a)} | ||
_ => () | ||
} | ||
} |
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.
Maybe add a comment here as well re. the purpose of this check.
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.
Added in 4b1aa9fa