-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Suggest if let
on let
refutable binding
#65248
Merged
Merged
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,12 +62,13 @@ impl<'tcx> Visitor<'tcx> for MatchVisitor<'_, 'tcx> { | |
fn visit_local(&mut self, loc: &'tcx hir::Local) { | ||
intravisit::walk_local(self, loc); | ||
|
||
self.check_irrefutable(&loc.pat, match loc.source { | ||
hir::LocalSource::Normal => "local binding", | ||
hir::LocalSource::ForLoopDesugar => "`for` loop binding", | ||
hir::LocalSource::AsyncFn => "async fn binding", | ||
hir::LocalSource::AwaitDesugar => "`await` future binding", | ||
}); | ||
let (msg, sp) = match loc.source { | ||
hir::LocalSource::Normal => ("local binding", Some(loc.span)), | ||
hir::LocalSource::ForLoopDesugar => ("`for` loop binding", None), | ||
hir::LocalSource::AsyncFn => ("async fn binding", None), | ||
hir::LocalSource::AwaitDesugar => ("`await` future binding", None), | ||
}; | ||
self.check_irrefutable(&loc.pat, msg, sp); | ||
|
||
// Check legality of move bindings and `@` patterns. | ||
self.check_patterns(false, &loc.pat); | ||
|
@@ -77,7 +78,7 @@ impl<'tcx> Visitor<'tcx> for MatchVisitor<'_, 'tcx> { | |
intravisit::walk_body(self, body); | ||
|
||
for param in &body.params { | ||
self.check_irrefutable(¶m.pat, "function argument"); | ||
self.check_irrefutable(¶m.pat, "function argument", None); | ||
self.check_patterns(false, ¶m.pat); | ||
} | ||
} | ||
|
@@ -242,7 +243,7 @@ impl<'tcx> MatchVisitor<'_, 'tcx> { | |
}) | ||
} | ||
|
||
fn check_irrefutable(&self, pat: &'tcx Pat, origin: &str) { | ||
fn check_irrefutable(&self, pat: &'tcx Pat, origin: &str, sp: Option<Span>) { | ||
let module = self.tcx.hir().get_module_parent(pat.hir_id); | ||
MatchCheckCtxt::create_and_enter(self.tcx, self.param_env, module, |ref mut cx| { | ||
let mut patcx = PatCtxt::new(self.tcx, | ||
|
@@ -266,18 +267,35 @@ impl<'tcx> MatchVisitor<'_, 'tcx> { | |
"refutable pattern in {}: {} not covered", | ||
origin, joined_patterns | ||
); | ||
match &pat.kind { | ||
let suggest_if_let = match &pat.kind { | ||
hir::PatKind::Path(hir::QPath::Resolved(None, path)) | ||
if path.segments.len() == 1 && path.segments[0].args.is_none() => | ||
{ | ||
const_not_var(&mut err, cx.tcx, pat, path); | ||
false | ||
} | ||
_ => { | ||
err.span_label( | ||
pat.span, | ||
pattern_not_covered_label(&witnesses, &joined_patterns), | ||
); | ||
true | ||
} | ||
}; | ||
|
||
if let (Some(span), true) = (sp, suggest_if_let) { | ||
err.note("`let` bindings require an \"irrefutable pattern\", like a `struct` or \ | ||
an `enum` with only one variant"); | ||
if let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(span) { | ||
err.span_suggestion( | ||
span, | ||
"you might want to use `if let` to ignore the variant that isn't matched", | ||
format!("if {} {{ /* */ }}", &snippet[..snippet.len() - 1]), | ||
Applicability::HasPlaceholders, | ||
); | ||
} | ||
err.note("for more information, visit \ | ||
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. Should this note perhaps be in the |
||
https://doc.rust-lang.org/book/ch18-02-refutability.html"); | ||
} | ||
|
||
adt_defined_here(cx, &mut err, pattern_ty, &witnesses); | ||
|
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
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
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
16 changes: 16 additions & 0 deletions
16
src/test/ui/feature-gates/feature-gate-exhaustive-patterns.nll.stderr
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,16 @@ | ||
error[E0005]: refutable pattern in local binding: `Err(_)` not covered | ||
--> $DIR/feature-gate-exhaustive-patterns.rs:7:9 | ||
| | ||
LL | let Ok(_x) = foo(); | ||
| ^^^^^^ pattern `Err(_)` not covered | ||
| | ||
= note: `let` bindings require an "irrefutable pattern", like a `struct` or an `enum` with only one variant | ||
= note: for more information, visit https://doc.rust-lang.org/book/ch18-02-refutability.html | ||
help: you might want to use `if let` to ignore the variant that isn't matched | ||
| | ||
LL | if let Ok(_x) = foo() { /* */ } | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0005`. |
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
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
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
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
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.