Skip to content

Commit

Permalink
suggest adding an appropriate missing pattern excluding comments
Browse files Browse the repository at this point in the history
  • Loading branch information
TaKO8Ki committed Aug 9, 2022
1 parent 93ab13b commit 56ec5be
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 14 deletions.
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`.

0 comments on commit 56ec5be

Please sign in to comment.