Skip to content

Commit 1a18ee8

Browse files
lzcuntmejrs
authored and
mejrs
committed
Migrate irrefutable let pattern diagnostics
1 parent 8c00282 commit 1a18ee8

File tree

3 files changed

+103
-57
lines changed

3 files changed

+103
-57
lines changed

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,3 +237,53 @@ mir_build_trailing_irrefutable_let_patterns = trailing irrefutable {$count ->
237237
mir_build_bindings_with_variant_name =
238238
pattern binding `{$ident}` is named the same as one of the variants of the type `{$ty_path}`
239239
.suggestion = to match on the variant, qualify the path
240+
241+
mir_build_irrefutable_let_patterns_generic_let = irrefutable `let` {$count ->
242+
[one] pattern
243+
*[other] patterns
244+
}
245+
.note = {$count ->
246+
[one] this pattern
247+
*[other] these patterns
248+
} will always match, so the `let` is useless
249+
.help = consider removing `let`
250+
251+
mir_build_irrefutable_let_patterns_if_let = irrefutable `if let` {$count ->
252+
[one] pattern
253+
*[other] patterns
254+
}
255+
.note = {$count ->
256+
[one] this pattern
257+
*[other] these patterns
258+
} will always match, so the `if let` is useless
259+
.help = consider replacing the `if let` with a `let`
260+
261+
mir_build_irrefutable_let_patterns_if_let_guard = irrefutable `if let` guard {$count ->
262+
[one] pattern
263+
*[other] patterns
264+
}
265+
.note = {$count ->
266+
[one] this pattern
267+
*[other] these patterns
268+
} will always match, so the guard is useless
269+
.help = consider removing the guard and adding a `let` inside the match arm
270+
271+
mir_build_irrefutable_let_patterns_let_else = irrefutable `let...else` {$count ->
272+
[one] pattern
273+
*[other] patterns
274+
}
275+
.note = {$count ->
276+
[one] this pattern
277+
*[other] these patterns
278+
} will always match, so the `else` clause is useless
279+
.help = consider removing the `else` clause
280+
281+
mir_build_irrefutable_let_patterns_while_let = irrefutable `while let` {$count ->
282+
[one] pattern
283+
*[other] patterns
284+
}
285+
.note = {$count ->
286+
[one] this pattern
287+
*[other] these patterns
288+
} will always match, so the loop will never exit
289+
.help = consider instead using a `loop {"{"} ... {"}"}` with a `let` inside it

compiler/rustc_mir_build/src/errors.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,3 +522,43 @@ pub struct BindingsWithVariantName {
522522
pub ty_path: String,
523523
pub ident: Ident,
524524
}
525+
526+
#[derive(LintDiagnostic)]
527+
#[diag(mir_build::irrefutable_let_patterns_generic_let)]
528+
#[note]
529+
#[help]
530+
pub struct IrrefutableLetPatternsGenericLet {
531+
pub count: usize,
532+
}
533+
534+
#[derive(LintDiagnostic)]
535+
#[diag(mir_build::irrefutable_let_patterns_if_let)]
536+
#[note]
537+
#[help]
538+
pub struct IrrefutableLetPatternsIfLet {
539+
pub count: usize,
540+
}
541+
542+
#[derive(LintDiagnostic)]
543+
#[diag(mir_build::irrefutable_let_patterns_if_let_guard)]
544+
#[note]
545+
#[help]
546+
pub struct IrrefutableLetPatternsIfLetGuard {
547+
pub count: usize,
548+
}
549+
550+
#[derive(LintDiagnostic)]
551+
#[diag(mir_build::irrefutable_let_patterns_let_else)]
552+
#[note]
553+
#[help]
554+
pub struct IrrefutableLetPatternsLetElse {
555+
pub count: usize,
556+
}
557+
558+
#[derive(LintDiagnostic)]
559+
#[diag(mir_build::irrefutable_let_patterns_while_let)]
560+
#[note]
561+
#[help]
562+
pub struct IrrefutableLetPatternsWhileLet {
563+
pub count: usize,
564+
}

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

Lines changed: 13 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -598,68 +598,24 @@ fn irrefutable_let_patterns(
598598
count: usize,
599599
span: Span,
600600
) {
601+
let span = match source {
602+
LetSource::LetElse(span) => span,
603+
_ => span,
604+
};
605+
601606
macro_rules! emit_diag {
602-
(
603-
$lint:expr,
604-
$source_name:expr,
605-
$note_sufix:expr,
606-
$help_sufix:expr
607-
) => {{
608-
let s = pluralize!(count);
609-
let these = pluralize!("this", count);
610-
tcx.struct_span_lint_hir(
611-
IRREFUTABLE_LET_PATTERNS,
612-
id,
613-
span,
614-
format!("irrefutable {} pattern{s}", $source_name),
615-
|lint| {
616-
lint.note(&format!(
617-
"{these} pattern{s} will always match, so the {}",
618-
$note_sufix
619-
))
620-
.help(concat!("consider ", $help_sufix))
621-
},
622-
)
607+
($lint:tt) => {{
608+
tcx.emit_spanned_lint(IRREFUTABLE_LET_PATTERNS, id, span, $lint { count });
623609
}};
624610
}
625611

626612
match source {
627-
LetSource::GenericLet => {
628-
emit_diag!(lint, "`let`", "`let` is useless", "removing `let`");
629-
}
630-
LetSource::IfLet => {
631-
emit_diag!(
632-
lint,
633-
"`if let`",
634-
"`if let` is useless",
635-
"replacing the `if let` with a `let`"
636-
);
637-
}
638-
LetSource::IfLetGuard => {
639-
emit_diag!(
640-
lint,
641-
"`if let` guard",
642-
"guard is useless",
643-
"removing the guard and adding a `let` inside the match arm"
644-
);
645-
}
646-
LetSource::LetElse => {
647-
emit_diag!(
648-
lint,
649-
"`let...else`",
650-
"`else` clause is useless",
651-
"removing the `else` clause"
652-
);
653-
}
654-
LetSource::WhileLet => {
655-
emit_diag!(
656-
lint,
657-
"`while let`",
658-
"loop will never exit",
659-
"instead using a `loop { ... }` with a `let` inside it"
660-
);
661-
}
662-
};
613+
LetSource::GenericLet => emit_diag!(IrrefutableLetPatternsGenericLet),
614+
LetSource::IfLet => emit_diag!(IrrefutableLetPatternsIfLet),
615+
LetSource::IfLetGuard => emit_diag!(IrrefutableLetPatternsIfLetGuard),
616+
LetSource::LetElse(..) => emit_diag!(IrrefutableLetPatternsLetElse),
617+
LetSource::WhileLet => emit_diag!(IrrefutableLetPatternsWhileLet),
618+
}
663619
}
664620

665621
fn is_let_irrefutable<'p, 'tcx>(

0 commit comments

Comments
 (0)