about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2022-11-05 18:06:06 +0100
committerGitHub <noreply@github.com>2022-11-05 18:06:06 +0100
commit51287f264ca92285108d7866901a2d29eee49725 (patch)
treebaada37a1d517a2fc40fa139df0d8a71d5a03b9a
parent305cb7133f7e68e9ecad6319363909dc371b6fa9 (diff)
parentb1994ce80662c6cdebdf42cb64e87572f0ab8839 (diff)
downloadrust-51287f264ca92285108d7866901a2d29eee49725.tar.gz
rust-51287f264ca92285108d7866901a2d29eee49725.zip
Rollup merge of #103927 - fee1-dead-contrib:E0425-no-typo-when-pattern-matching, r=cjgillot
Do not make typo suggestions when suggesting pattern matching

Fixes #103909.
-rw-r--r--compiler/rustc_resolve/src/late/diagnostics.rs15
-rw-r--r--src/test/ui/did_you_mean/issue-103909.rs9
-rw-r--r--src/test/ui/did_you_mean/issue-103909.stderr21
3 files changed, 43 insertions, 2 deletions
diff --git a/compiler/rustc_resolve/src/late/diagnostics.rs b/compiler/rustc_resolve/src/late/diagnostics.rs
index 103187b00d1..a1338dcd477 100644
--- a/compiler/rustc_resolve/src/late/diagnostics.rs
+++ b/compiler/rustc_resolve/src/late/diagnostics.rs
@@ -322,7 +322,12 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
         }
 
         self.suggest_bare_struct_literal(&mut err);
-        self.suggest_pattern_match_with_let(&mut err, source, span);
+
+        if self.suggest_pattern_match_with_let(&mut err, source, span) {
+            // Fallback label.
+            err.span_label(base_error.span, &base_error.fallback_label);
+            return (err, Vec::new());
+        }
 
         self.suggest_self_or_self_ref(&mut err, path, span);
         self.detect_assoct_type_constraint_meant_as_path(&mut err, &base_error);
@@ -341,7 +346,11 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
         if !self.type_ascription_suggestion(&mut err, base_error.span) {
             let mut fallback =
                 self.suggest_trait_and_bounds(&mut err, source, res, span, &base_error);
+
+            // if we have suggested using pattern matching, then don't add needless suggestions
+            // for typos.
             fallback |= self.suggest_typo(&mut err, source, path, span, &base_error);
+
             if fallback {
                 // Fallback label.
                 err.span_label(base_error.span, &base_error.fallback_label);
@@ -937,7 +946,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
         err: &mut Diagnostic,
         source: PathSource<'_>,
         span: Span,
-    ) {
+    ) -> bool {
         if let PathSource::Expr(_) = source &&
         let Some(Expr {
                     span: expr_span,
@@ -954,8 +963,10 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
                     "let ",
                     Applicability::MaybeIncorrect,
                 );
+                return true;
             }
         }
+        false
     }
 
     fn get_single_associated_item(
diff --git a/src/test/ui/did_you_mean/issue-103909.rs b/src/test/ui/did_you_mean/issue-103909.rs
new file mode 100644
index 00000000000..20b67cd102d
--- /dev/null
+++ b/src/test/ui/did_you_mean/issue-103909.rs
@@ -0,0 +1,9 @@
+#![allow(unused_variables)]
+use std::fs::File;
+
+fn main() {
+    if Err(err) = File::open("hello.txt") {
+        //~^ ERROR: cannot find value `err` in this scope
+        //~| ERROR: mismatched types
+    }
+}
diff --git a/src/test/ui/did_you_mean/issue-103909.stderr b/src/test/ui/did_you_mean/issue-103909.stderr
new file mode 100644
index 00000000000..a28914051b9
--- /dev/null
+++ b/src/test/ui/did_you_mean/issue-103909.stderr
@@ -0,0 +1,21 @@
+error[E0425]: cannot find value `err` in this scope
+  --> $DIR/issue-103909.rs:5:12
+   |
+LL |     if Err(err) = File::open("hello.txt") {
+   |            ^^^ not found in this scope
+   |
+help: you might have meant to use pattern matching
+   |
+LL |     if let Err(err) = File::open("hello.txt") {
+   |        +++
+
+error[E0308]: mismatched types
+  --> $DIR/issue-103909.rs:5:8
+   |
+LL |     if Err(err) = File::open("hello.txt") {
+   |        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `bool`, found `()`
+
+error: aborting due to 2 previous errors
+
+Some errors have detailed explanations: E0308, E0425.
+For more information about an error, try `rustc --explain E0308`.