mirror of
https://github.com/rust-lang/rust.git
synced 2025-05-14 02:49:40 +00:00
suggest adding an appropriate missing pattern excluding comments
This commit is contained in:
parent
93ab13b4e8
commit
56ec5bec1e
@ -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),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,10 @@
|
||||
// run-rustfix
|
||||
|
||||
fn main() {
|
||||
match Some(1) { //~ ERROR non-exhaustive patterns: `None` not covered
|
||||
Some(1) => {}
|
||||
// hello
|
||||
Some(_) => {}
|
||||
None => todo!()
|
||||
}
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
// run-rustfix
|
||||
|
||||
fn main() {
|
||||
match Some(1) { //~ ERROR non-exhaustive patterns: `None` not covered
|
||||
Some(1) => {}
|
||||
// hello
|
||||
Some(_) => {}
|
||||
}
|
||||
}
|
@ -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`.
|
Loading…
Reference in New Issue
Block a user