Skip to content

Commit 467e7aa

Browse files
authored
Rollup merge of #100305 - TaKO8Ki:suggest-adding-appropriate-missing-pattern-excluding-comments, r=compiler-errors
Suggest adding an appropriate missing pattern excluding comments fixes #100272
2 parents d7f414d + 56ec5be commit 467e7aa

4 files changed

+57
-14
lines changed

Diff for: compiler/rustc_mir_build/src/thir/pattern/check_match.rs

+14-14
Original file line numberDiff line numberDiff line change
@@ -849,22 +849,22 @@ fn non_exhaustive_match<'p, 'tcx>(
849849
));
850850
}
851851
[.., prev, last] if prev.span.eq_ctxt(last.span) => {
852-
if let Ok(snippet) = sm.span_to_snippet(prev.span.between(last.span)) {
853-
let comma = if matches!(last.body.kind, hir::ExprKind::Block(..))
854-
&& last.span.eq_ctxt(last.body.span)
855-
{
856-
""
857-
} else {
858-
","
859-
};
852+
let comma = if matches!(last.body.kind, hir::ExprKind::Block(..))
853+
&& last.span.eq_ctxt(last.body.span)
854+
{
855+
""
856+
} else {
857+
","
858+
};
859+
let spacing = if sm.is_multiline(prev.span.between(last.span)) {
860+
sm.indentation_before(last.span).map(|indent| format!("\n{indent}"))
861+
} else {
862+
Some(" ".to_string())
863+
};
864+
if let Some(spacing) = spacing {
860865
suggestion = Some((
861866
last.span.shrink_to_hi(),
862-
format!(
863-
"{}{}{} => todo!()",
864-
comma,
865-
snippet.strip_prefix(',').unwrap_or(&snippet),
866-
pattern
867-
),
867+
format!("{}{}{} => todo!()", comma, spacing, pattern),
868868
));
869869
}
870870
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// run-rustfix
2+
3+
fn main() {
4+
match Some(1) { //~ ERROR non-exhaustive patterns: `None` not covered
5+
Some(1) => {}
6+
// hello
7+
Some(_) => {}
8+
None => todo!()
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// run-rustfix
2+
3+
fn main() {
4+
match Some(1) { //~ ERROR non-exhaustive patterns: `None` not covered
5+
Some(1) => {}
6+
// hello
7+
Some(_) => {}
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
error[E0004]: non-exhaustive patterns: `None` not covered
2+
--> $DIR/suggest-adding-appropriate-missing-pattern-excluding-comments.rs:4:11
3+
|
4+
LL | match Some(1) {
5+
| ^^^^^^^ pattern `None` not covered
6+
|
7+
note: `Option<i32>` defined here
8+
--> $SRC_DIR/core/src/option.rs:LL:COL
9+
|
10+
LL | pub enum Option<T> {
11+
| ------------------
12+
...
13+
LL | None,
14+
| ^^^^ not covered
15+
= note: the matched value is of type `Option<i32>`
16+
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
17+
|
18+
LL ~ Some(_) => {}
19+
LL + None => todo!()
20+
|
21+
22+
error: aborting due to previous error
23+
24+
For more information about this error, try `rustc --explain E0004`.

0 commit comments

Comments
 (0)