review comments

- Remove check for "how many path segments is the pattern"
- Check before suggesting if the path has multiple path segments
This commit is contained in:
Esteban Küber 2024-11-13 21:54:46 +00:00
parent bb37e5d3cd
commit 6480b76e45
2 changed files with 26 additions and 21 deletions

View File

@ -677,6 +677,9 @@ impl<'p, 'tcx> MatchVisitor<'p, 'tcx> {
..
} = pat.kind
&& let DefKind::Const = self.tcx.def_kind(def_id)
&& let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(pat.span)
// We filter out paths with multiple path::segments.
&& snippet.chars().all(|c| c.is_alphanumeric() || c == '_')
{
let span = self.tcx.def_span(def_id);
let variable = self.tcx.item_name(def_id).to_string();
@ -1143,7 +1146,11 @@ fn report_non_exhaustive_match<'p, 'tcx>(
for &arm in arms {
let arm = &thir.arms[arm];
if let PatKind::ExpandedConstant { def_id, is_inline: false, .. } = arm.pattern.kind {
if let PatKind::ExpandedConstant { def_id, is_inline: false, .. } = arm.pattern.kind
&& let Ok(snippet) = cx.tcx.sess.source_map().span_to_snippet(arm.pattern.span)
// We filter out paths with multiple path::segments.
&& snippet.chars().all(|c| c.is_alphanumeric() || c == '_')
{
let const_name = cx.tcx.item_name(def_id);
err.span_label(
arm.pattern.span,

View File

@ -160,13 +160,19 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
}
kind => (kind, None, None),
};
let value = if let PatKind::Constant { value } = kind {
value
} else {
let msg = format!(
"found bad range pattern endpoint `{expr:?}` outside of error recovery"
);
return Err(self.tcx.dcx().span_delayed_bug(expr.span, msg));
let value = match kind {
PatKind::Constant { value } => value,
PatKind::ExpandedConstant { subpattern, .. }
if let PatKind::Constant { value } = subpattern.kind =>
{
value
}
_ => {
let msg = format!(
"found bad range pattern endpoint `{expr:?}` outside of error recovery"
);
return Err(self.tcx.dcx().span_delayed_bug(expr.span, msg));
}
};
Ok((Some(PatRangeBoundary::Finite(value)), ascr, inline_const))
}
@ -570,19 +576,11 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
let args = self.typeck_results.node_args(id);
let c = ty::Const::new_unevaluated(self.tcx, ty::UnevaluatedConst { def: def_id, args });
let subpattern = self.const_to_pat(c, ty, id, span);
let pattern = if let hir::QPath::Resolved(None, path) = qpath
&& path.segments.len() == 1
{
// We only want to mark constants when referenced as bare names that could have been
// new bindings if the `const` didn't exist.
Box::new(Pat {
span,
ty,
kind: PatKind::ExpandedConstant { subpattern, def_id, is_inline: false },
})
} else {
subpattern
};
let pattern = Box::new(Pat {
span,
ty,
kind: PatKind::ExpandedConstant { subpattern, def_id, is_inline: false },
});
if !is_associated_const {
return pattern;