Skip to content

Commit 51287f2

Browse files
authored
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.
2 parents 305cb71 + b1994ce commit 51287f2

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

compiler/rustc_resolve/src/late/diagnostics.rs

+13-2
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,12 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
322322
}
323323

324324
self.suggest_bare_struct_literal(&mut err);
325-
self.suggest_pattern_match_with_let(&mut err, source, span);
325+
326+
if self.suggest_pattern_match_with_let(&mut err, source, span) {
327+
// Fallback label.
328+
err.span_label(base_error.span, &base_error.fallback_label);
329+
return (err, Vec::new());
330+
}
326331

327332
self.suggest_self_or_self_ref(&mut err, path, span);
328333
self.detect_assoct_type_constraint_meant_as_path(&mut err, &base_error);
@@ -341,7 +346,11 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
341346
if !self.type_ascription_suggestion(&mut err, base_error.span) {
342347
let mut fallback =
343348
self.suggest_trait_and_bounds(&mut err, source, res, span, &base_error);
349+
350+
// if we have suggested using pattern matching, then don't add needless suggestions
351+
// for typos.
344352
fallback |= self.suggest_typo(&mut err, source, path, span, &base_error);
353+
345354
if fallback {
346355
// Fallback label.
347356
err.span_label(base_error.span, &base_error.fallback_label);
@@ -937,7 +946,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
937946
err: &mut Diagnostic,
938947
source: PathSource<'_>,
939948
span: Span,
940-
) {
949+
) -> bool {
941950
if let PathSource::Expr(_) = source &&
942951
let Some(Expr {
943952
span: expr_span,
@@ -954,8 +963,10 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
954963
"let ",
955964
Applicability::MaybeIncorrect,
956965
);
966+
return true;
957967
}
958968
}
969+
false
959970
}
960971

961972
fn get_single_associated_item(
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#![allow(unused_variables)]
2+
use std::fs::File;
3+
4+
fn main() {
5+
if Err(err) = File::open("hello.txt") {
6+
//~^ ERROR: cannot find value `err` in this scope
7+
//~| ERROR: mismatched types
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
error[E0425]: cannot find value `err` in this scope
2+
--> $DIR/issue-103909.rs:5:12
3+
|
4+
LL | if Err(err) = File::open("hello.txt") {
5+
| ^^^ not found in this scope
6+
|
7+
help: you might have meant to use pattern matching
8+
|
9+
LL | if let Err(err) = File::open("hello.txt") {
10+
| +++
11+
12+
error[E0308]: mismatched types
13+
--> $DIR/issue-103909.rs:5:8
14+
|
15+
LL | if Err(err) = File::open("hello.txt") {
16+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `bool`, found `()`
17+
18+
error: aborting due to 2 previous errors
19+
20+
Some errors have detailed explanations: E0308, E0425.
21+
For more information about an error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)