1
Fork 0

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 } = pat.kind
&& let DefKind::Const = self.tcx.def_kind(def_id) && 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 span = self.tcx.def_span(def_id);
let variable = self.tcx.item_name(def_id).to_string(); 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 { for &arm in arms {
let arm = &thir.arms[arm]; 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); let const_name = cx.tcx.item_name(def_id);
err.span_label( err.span_label(
arm.pattern.span, arm.pattern.span,

View file

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