Skip to content

Commit 3b57a3e

Browse files
committed
Migrate irrefutable let pattern diagnostics
1 parent d91ff3e commit 3b57a3e

File tree

3 files changed

+104
-53
lines changed

3 files changed

+104
-53
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: 14 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -591,63 +591,24 @@ fn irrefutable_let_patterns(
591591
count: usize,
592592
span: Span,
593593
) {
594-
macro_rules! emit_diag {
595-
(
596-
$lint:expr,
597-
$source_name:expr,
598-
$note_sufix:expr,
599-
$help_sufix:expr
600-
) => {{
601-
let s = pluralize!(count);
602-
let these = pluralize!("this", count);
603-
let mut diag = $lint.build(&format!("irrefutable {} pattern{s}", $source_name));
604-
diag.note(&format!("{these} pattern{s} will always match, so the {}", $note_sufix));
605-
diag.help(concat!("consider ", $help_sufix));
606-
diag.emit()
607-
}};
608-
}
609-
610594
let span = match source {
611595
LetSource::LetElse(span) => span,
612596
_ => span,
613597
};
614-
tcx.struct_span_lint_hir(IRREFUTABLE_LET_PATTERNS, id, span, |lint| match source {
615-
LetSource::GenericLet => {
616-
emit_diag!(lint, "`let`", "`let` is useless", "removing `let`");
617-
}
618-
LetSource::IfLet => {
619-
emit_diag!(
620-
lint,
621-
"`if let`",
622-
"`if let` is useless",
623-
"replacing the `if let` with a `let`"
624-
);
625-
}
626-
LetSource::IfLetGuard => {
627-
emit_diag!(
628-
lint,
629-
"`if let` guard",
630-
"guard is useless",
631-
"removing the guard and adding a `let` inside the match arm"
632-
);
633-
}
634-
LetSource::LetElse(..) => {
635-
emit_diag!(
636-
lint,
637-
"`let...else`",
638-
"`else` clause is useless",
639-
"removing the `else` clause"
640-
);
641-
}
642-
LetSource::WhileLet => {
643-
emit_diag!(
644-
lint,
645-
"`while let`",
646-
"loop will never exit",
647-
"instead using a `loop { ... }` with a `let` inside it"
648-
);
649-
}
650-
});
598+
599+
macro_rules! emit_diag {
600+
($lint:tt) => {{
601+
tcx.emit_spanned_lint(IRREFUTABLE_LET_PATTERNS, id, span, $lint { count });
602+
}};
603+
}
604+
605+
match source {
606+
LetSource::GenericLet => emit_diag!(IrrefutableLetPatternsGenericLet),
607+
LetSource::IfLet => emit_diag!(IrrefutableLetPatternsIfLet),
608+
LetSource::IfLetGuard => emit_diag!(IrrefutableLetPatternsIfLetGuard),
609+
LetSource::LetElse(..) => emit_diag!(IrrefutableLetPatternsLetElse),
610+
LetSource::WhileLet => emit_diag!(IrrefutableLetPatternsWhileLet),
611+
}
651612
}
652613

653614
fn is_let_irrefutable<'p, 'tcx>(

0 commit comments

Comments
 (0)