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:14:17 +0000
committerEsteban Kuber <esteban@kuber.com.ar>2022-03-08 00:18:24 +0000
commit2383858f34989f7c6c87da857bd038f5ce0a66b0 (patch)
treefae94544bea7a6888068b41bb51c1983aeceb813 /compiler
parent02a3830f245d84672db133208c73756eb8778964 (diff)
downloadrust-2383858f34989f7c6c87da857bd038f5ce0a66b0.tar.gz
rust-2383858f34989f7c6c87da857bd038f5ce0a66b0.zip
When finding a match expr with a single arm that requires more, suggest it
Given

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

suggest

```rust
match Some(42) {
    Some(0) => {}
    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 cb057e428c4..f1c2ec08c3c 100644
--- a/compiler/rustc_mir_build/src/thir/pattern/check_match.rs
+++ b/compiler/rustc_mir_build/src/thir/pattern/check_match.rs
@@ -589,6 +589,21 @@ fn non_exhaustive_match<'p, 'tcx>(
                 ),
             ));
         }
+        [only] => {
+            let pre_indentation = if let (Some(snippet), true) = (
+                sm.indentation_before(only.span),
+                sm.is_multiline(sp.shrink_to_hi().with_hi(only.span.lo())),
+            ) {
+                format!("\n{}", snippet)
+            } else {
+                " ".to_string()
+            };
+            let comma = if matches!(only.body.kind, hir::ExprKind::Block(..)) { "" } else { "," };
+            suggestion = Some((
+                only.span.shrink_to_hi(),
+                format!("{}{}{} => todo!()", comma, pre_indentation, pattern),
+            ));
+        }
         _ => {}
     }