Skip to content

Suggest adding an appropriate missing pattern excluding comments #100305

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions compiler/rustc_mir_build/src/thir/pattern/check_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -849,22 +849,22 @@ fn non_exhaustive_match<'p, 'tcx>(
));
}
[.., prev, last] if prev.span.eq_ctxt(last.span) => {
if let Ok(snippet) = sm.span_to_snippet(prev.span.between(last.span)) {
let comma = if matches!(last.body.kind, hir::ExprKind::Block(..))
&& last.span.eq_ctxt(last.body.span)
{
""
} else {
","
};
let comma = if matches!(last.body.kind, hir::ExprKind::Block(..))
&& last.span.eq_ctxt(last.body.span)
{
""
} else {
","
};
let spacing = if sm.is_multiline(prev.span.between(last.span)) {
sm.indentation_before(last.span).map(|indent| format!("\n{indent}"))
} else {
Some(" ".to_string())
};
if let Some(spacing) = spacing {
suggestion = Some((
last.span.shrink_to_hi(),
format!(
"{}{}{} => todo!()",
comma,
snippet.strip_prefix(',').unwrap_or(&snippet),
pattern
),
format!("{}{}{} => todo!()", comma, spacing, pattern),
));
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// run-rustfix

fn main() {
match Some(1) { //~ ERROR non-exhaustive patterns: `None` not covered
Some(1) => {}
// hello
Some(_) => {}
None => todo!()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// run-rustfix

fn main() {
match Some(1) { //~ ERROR non-exhaustive patterns: `None` not covered
Some(1) => {}
// hello
Some(_) => {}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
error[E0004]: non-exhaustive patterns: `None` not covered
--> $DIR/suggest-adding-appropriate-missing-pattern-excluding-comments.rs:4:11
|
LL | match Some(1) {
| ^^^^^^^ pattern `None` not covered
|
note: `Option<i32>` defined here
--> $SRC_DIR/core/src/option.rs:LL:COL
|
LL | pub enum Option<T> {
| ------------------
...
LL | None,
| ^^^^ not covered
= note: the matched value is of type `Option<i32>`
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
|
LL ~ Some(_) => {}
LL + None => todo!()
|

error: aborting due to previous error

For more information about this error, try `rustc --explain E0004`.