Skip to content

Commit 8c00282

Browse files
lzcuntmejrs
authored and
mejrs
committed
Migrate pattern bindings with variant name lint
1 parent 42a8f37 commit 8c00282

File tree

3 files changed

+24
-23
lines changed

3 files changed

+24
-23
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 & 22 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, DelayDm, 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::*;
@@ -547,32 +547,20 @@ fn check_for_bindings_named_same_as_variants(
547547
})
548548
{
549549
let variant_count = edef.variants().len();
550-
cx.tcx.struct_span_lint_hir(
550+
let ty_path = cx.tcx.def_path_str(edef.did());
551+
cx.tcx.emit_spanned_lint(
551552
BINDINGS_WITH_VARIANT_NAME,
552553
p.hir_id,
553554
p.span,
554-
DelayDm(|| format!(
555-
"pattern binding `{}` is named the same as one \
556-
of the variants of the type `{}`",
557-
ident, cx.tcx.def_path_str(edef.did())
558-
)),
559-
|lint| {
560-
let ty_path = cx.tcx.def_path_str(edef.did());
561-
lint.code(error_code!(E0170));
562-
555+
BindingsWithVariantName {
563556
// If this is an irrefutable pattern, and there's > 1 variant,
564557
// then we can't actually match on this. Applying the below
565558
// suggestion would produce code that breaks on `check_irrefutable`.
566-
if rf == Refutable || variant_count == 1 {
567-
lint.span_suggestion(
568-
p.span,
569-
"to match on the variant, qualify the path",
570-
format!("{}::{}", ty_path, ident),
571-
Applicability::MachineApplicable,
572-
);
573-
}
574-
575-
lint
559+
suggestion: if rf == Refutable || variant_count == 1 {
560+
Some(p.span)
561+
} else { None },
562+
ty_path,
563+
ident,
576564
},
577565
)
578566
}

0 commit comments

Comments
 (0)