suggest adding an appropriate missing pattern excluding comments

This commit is contained in:
Takayuki Maeda 2022-08-09 14:27:26 +09:00
parent 93ab13b4e8
commit 56ec5bec1e
4 changed files with 57 additions and 14 deletions

View File

@ -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),
));
}
}

View File

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

View File

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

View File

@ -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`.