Skip to content

Commit 1b31e14

Browse files
committed
Centralize the decision to suggest patterns vs _
1 parent b878ab6 commit 1b31e14

File tree

1 file changed

+36
-40
lines changed

1 file changed

+36
-40
lines changed

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

+36-40
Original file line numberDiff line numberDiff line change
@@ -939,43 +939,30 @@ fn report_non_exhaustive_match<'p, 'tcx>(
939939
};
940940
// In the case of an empty match, replace the '`_` not covered' diagnostic with something more
941941
// informative.
942-
let mut err;
943-
let pattern;
944-
let patterns_len;
945942
if is_empty_match && !non_empty_enum {
946943
return cx.tcx.dcx().emit_err(NonExhaustivePatternsTypeNotEmpty {
947944
cx,
948945
expr_span,
949946
span: sp,
950947
ty: scrut_ty,
951948
});
952-
} else {
953-
// FIXME: migration of this diagnostic will require list support
954-
let joined_patterns = joined_uncovered_patterns(cx, &witnesses);
955-
err = create_e0004(
956-
cx.tcx.sess,
957-
sp,
958-
format!("non-exhaustive patterns: {joined_patterns} not covered"),
959-
);
960-
err.span_label(
961-
sp,
962-
format!(
963-
"pattern{} {} not covered",
964-
rustc_errors::pluralize!(witnesses.len()),
965-
joined_patterns
966-
),
967-
);
968-
patterns_len = witnesses.len();
969-
pattern = if witnesses.len() < 4 {
970-
witnesses
971-
.iter()
972-
.map(|witness| cx.hoist_witness_pat(witness).to_string())
973-
.collect::<Vec<String>>()
974-
.join(" | ")
975-
} else {
976-
"_".to_string()
977-
};
978-
};
949+
}
950+
951+
// FIXME: migration of this diagnostic will require list support
952+
let joined_patterns = joined_uncovered_patterns(cx, &witnesses);
953+
let mut err = create_e0004(
954+
cx.tcx.sess,
955+
sp,
956+
format!("non-exhaustive patterns: {joined_patterns} not covered"),
957+
);
958+
err.span_label(
959+
sp,
960+
format!(
961+
"pattern{} {} not covered",
962+
rustc_errors::pluralize!(witnesses.len()),
963+
joined_patterns
964+
),
965+
);
979966

980967
// Point at the definition of non-covered `enum` variants.
981968
if let Some(AdtDefinedHere { adt_def_span, ty, variants }) =
@@ -1022,16 +1009,25 @@ fn report_non_exhaustive_match<'p, 'tcx>(
10221009
}
10231010
}
10241011

1025-
let mut suggestion = None;
1026-
let sm = cx.tcx.sess.source_map();
1027-
let suggested_arm = if witnesses.len() < 4
1028-
&& witnesses.iter().all(|p| p.is_never_pattern())
1029-
&& cx.tcx.features().never_patterns
1030-
{
1031-
pattern
1012+
// Whether we suggest the actual missing patterns or `_`.
1013+
let suggest_the_witnesses = witnesses.len() < 4;
1014+
let suggested_arm = if suggest_the_witnesses {
1015+
let pattern = witnesses
1016+
.iter()
1017+
.map(|witness| cx.hoist_witness_pat(witness).to_string())
1018+
.collect::<Vec<String>>()
1019+
.join(" | ");
1020+
if witnesses.iter().all(|p| p.is_never_pattern()) && cx.tcx.features().never_patterns {
1021+
// Arms with a never pattern don't take a body.
1022+
pattern
1023+
} else {
1024+
format!("{pattern} => todo!()")
1025+
}
10321026
} else {
1033-
format!("{pattern} => todo!()")
1027+
format!("_ => todo!()")
10341028
};
1029+
let mut suggestion = None;
1030+
let sm = cx.tcx.sess.source_map();
10351031
match arms {
10361032
[] if sp.eq_ctxt(expr_span) => {
10371033
// Get the span for the empty match body `{}`.
@@ -1102,13 +1098,13 @@ fn report_non_exhaustive_match<'p, 'tcx>(
11021098
let msg = format!(
11031099
"ensure that all possible cases are being handled by adding a match arm with a wildcard \
11041100
pattern{}{}",
1105-
if patterns_len > 1 && patterns_len < 4 && suggestion.is_some() {
1101+
if witnesses.len() > 1 && suggest_the_witnesses && suggestion.is_some() {
11061102
", a match arm with multiple or-patterns"
11071103
} else {
11081104
// we are either not suggesting anything, or suggesting `_`
11091105
""
11101106
},
1111-
match patterns_len {
1107+
match witnesses.len() {
11121108
// non-exhaustive enum case
11131109
0 if suggestion.is_some() => " as shown",
11141110
0 => "",

0 commit comments

Comments
 (0)