about summary refs log tree commit diff
path: root/compiler
diff options
context:
space:
mode:
authorEsteban Kuber <esteban@kuber.com.ar>2021-12-16 02:28:09 +0000
committerEsteban Kuber <esteban@kuber.com.ar>2022-03-08 00:18:24 +0000
commit084ca79e7c721e5b670eb4e4da4b45519c0822cb (patch)
tree4e6caa6b77d1e538e75be51515c4165d621f7081 /compiler
parent2383858f34989f7c6c87da857bd038f5ce0a66b0 (diff)
When finding a match expr with multiple arms that requires more, suggest it
Given

```rust
match Some(42) {
    Some(0) => {}
    Some(1) => {}
}
```

suggest

```rust
match Some(42) {
    Some(0) => {}
    Some(1) => {}
    None | Some(_) => todo!(),
}
```
Diffstat (limited to 'compiler')
-rw-r--r--compiler/rustc_mir_build/src/thir/pattern/check_match.rs15
1 files changed, 15 insertions, 0 deletions
diff --git a/compiler/rustc_mir_build/src/thir/pattern/check_match.rs b/compiler/rustc_mir_build/src/thir/pattern/check_match.rs
index f1c2ec08c3c..a0325101c41 100644
--- a/compiler/rustc_mir_build/src/thir/pattern/check_match.rs
+++ b/compiler/rustc_mir_build/src/thir/pattern/check_match.rs
@@ -604,6 +604,21 @@ fn non_exhaustive_match<'p, 'tcx>(
                 format!("{}{}{} => todo!()", comma, pre_indentation, pattern),
             ));
         }
+        [.., prev, last] if prev.span.ctxt() == last.span.ctxt() => {
+            if let Ok(snippet) = sm.span_to_snippet(prev.span.between(last.span)) {
+                let comma =
+                    if matches!(last.body.kind, hir::ExprKind::Block(..)) { "" } else { "," };
+                suggestion = Some((
+                    last.span.shrink_to_hi(),
+                    format!(
+                        "{}{}{} => todo!()",
+                        comma,
+                        snippet.strip_prefix(",").unwrap_or(&snippet),
+                        pattern
+                    ),
+                ));
+            }
+        }
         _ => {}
     }