Skip to content

Do not make typo suggestions when suggesting pattern matching #103927

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions compiler/rustc_resolve/src/late/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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,
Expand All @@ -954,8 +963,10 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
"let ",
Applicability::MaybeIncorrect,
);
return true;
}
}
false
}

fn get_single_associated_item(
Expand Down
9 changes: 9 additions & 0 deletions src/test/ui/did_you_mean/issue-103909.rs
Original file line number Diff line number Diff line change
@@ -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
}
}
21 changes: 21 additions & 0 deletions src/test/ui/did_you_mean/issue-103909.stderr
Original file line number Diff line number Diff line change
@@ -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`.