Skip to content

Commit d91ff3e

Browse files
committed
Migrate pattern bindings with variant name lint
1 parent d873ee6 commit d91ff3e

File tree

3 files changed

+24
-21
lines changed

3 files changed

+24
-21
lines changed

compiler/rustc_error_messages/locales/en-US/mir_build.ftl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,3 +233,7 @@ mir_build_trailing_irrefutable_let_patterns = trailing irrefutable {$count ->
233233
[one] it
234234
*[other] them
235235
} into the body
236+
237+
mir_build_bindings_with_variant_name =
238+
pattern binding `{$ident}` is named the same as one of the variants of the type `{$ty_path}`
239+
.suggestion = to match on the variant, qualify the path

compiler/rustc_mir_build/src/errors.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use rustc_errors::{error_code, Applicability, DiagnosticBuilder, ErrorGuaranteed
33
use rustc_macros::{LintDiagnostic, SessionDiagnostic, SessionSubdiagnostic};
44
use rustc_middle::ty::{self, Ty};
55
use rustc_session::{parse::ParseSess, SessionDiagnostic};
6-
use rustc_span::Span;
6+
use rustc_span::{symbol::Ident, Span};
77

88
#[derive(LintDiagnostic)]
99
#[diag(mir_build::unconditional_recursion)]
@@ -513,3 +513,12 @@ pub struct LeadingIrrefutableLetPatterns {
513513
pub struct TrailingIrrefutableLetPatterns {
514514
pub count: usize,
515515
}
516+
517+
#[derive(LintDiagnostic)]
518+
#[diag(mir_build::bindings_with_variant_name, code = "E0170")]
519+
pub struct BindingsWithVariantName {
520+
#[suggestion(code = "{ty_path}::{ident}", applicability = "machine-applicable")]
521+
pub suggestion: Option<Span>,
522+
pub ty_path: String,
523+
pub ident: Ident,
524+
}

compiler/rustc_mir_build/src/thir/pattern/check_match.rs

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ use crate::errors::*;
99
use rustc_arena::TypedArena;
1010
use rustc_ast::Mutability;
1111
use rustc_errors::{
12-
error_code, pluralize, struct_span_err, Applicability, Diagnostic, DiagnosticBuilder,
13-
ErrorGuaranteed, MultiSpan,
12+
pluralize, struct_span_err, Applicability, Diagnostic, DiagnosticBuilder, ErrorGuaranteed,
13+
MultiSpan,
1414
};
1515
use rustc_hir as hir;
1616
use rustc_hir::def::*;
@@ -540,30 +540,20 @@ fn check_for_bindings_named_same_as_variants(
540540
})
541541
{
542542
let variant_count = edef.variants().len();
543-
cx.tcx.struct_span_lint_hir(
543+
let ty_path = cx.tcx.def_path_str(edef.did());
544+
cx.tcx.emit_spanned_lint(
544545
BINDINGS_WITH_VARIANT_NAME,
545546
p.hir_id,
546547
p.span,
547-
|lint| {
548-
let ty_path = cx.tcx.def_path_str(edef.did());
549-
let mut err = lint.build(&format!(
550-
"pattern binding `{}` is named the same as one \
551-
of the variants of the type `{}`",
552-
ident, ty_path
553-
));
554-
err.code(error_code!(E0170));
548+
BindingsWithVariantName {
555549
// If this is an irrefutable pattern, and there's > 1 variant,
556550
// then we can't actually match on this. Applying the below
557551
// suggestion would produce code that breaks on `check_irrefutable`.
558-
if rf == Refutable || variant_count == 1 {
559-
err.span_suggestion(
560-
p.span,
561-
"to match on the variant, qualify the path",
562-
format!("{}::{}", ty_path, ident),
563-
Applicability::MachineApplicable,
564-
);
565-
}
566-
err.emit();
552+
suggestion: if rf == Refutable || variant_count == 1 {
553+
Some(p.span)
554+
} else { None },
555+
ty_path,
556+
ident,
567557
},
568558
)
569559
}

0 commit comments

Comments
 (0)