Use builin_index instead of hand-rolling it

This commit is contained in:
Oli Scherer 2025-05-26 10:38:02 +00:00
parent ce0adf08c9
commit 013ab6cdb1
3 changed files with 5 additions and 8 deletions

View file

@ -12,7 +12,6 @@ use rustc_hir::HirId;
use rustc_hir::intravisit::{self, Visitor};
use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::hir::nested_filter;
use rustc_middle::ty;
use rustc_session::impl_lint_pass;
use rustc_span::Span;
use rustc_span::symbol::Ident;
@ -109,11 +108,11 @@ fn find_slice_values(cx: &LateContext<'_>, pat: &hir::Pat<'_>) -> FxIndexMap<Hir
}
let bound_ty = cx.typeck_results().node_type(pat.hir_id);
if let ty::Slice(inner_ty) | ty::Array(inner_ty, _) = bound_ty.peel_refs().kind() {
if let Some(inner_ty) = bound_ty.peel_refs().builtin_index() {
// The values need to use the `ref` keyword if they can't be copied.
// This will need to be adjusted if the lint want to support mutable access in the future
let src_is_ref = bound_ty.is_ref() && by_ref == hir::ByRef::No;
let needs_ref = !(src_is_ref || is_copy(cx, *inner_ty));
let needs_ref = !(src_is_ref || is_copy(cx, inner_ty));
let slice_info = slices
.entry(value_hir_id)

View file

@ -66,7 +66,7 @@ impl LateLintPass<'_> for TupleArrayConversions {
}
fn check_array<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>, elements: &'tcx [Expr<'tcx>]) {
let (ty::Array(ty, _) | ty::Slice(ty)) = cx.typeck_results().expr_ty(expr).kind() else {
let Some(ty) = cx.typeck_results().expr_ty(expr).builtin_index() else {
unreachable!("`expr` must be an array or slice due to `ExprKind::Array`");
};
@ -85,7 +85,7 @@ fn check_array<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>, elements: &
ExprKind::Path(_) => Some(elements.iter().collect()),
_ => None,
})
&& all_bindings_are_for_conv(cx, &[*ty], expr, elements, &locals, ToType::Array)
&& all_bindings_are_for_conv(cx, &[ty], expr, elements, &locals, ToType::Array)
&& !is_from_proc_macro(cx, expr)
{
span_lint_and_help(

View file

@ -235,9 +235,7 @@ impl Constant<'_> {
_ => None,
},
(Self::Vec(l), Self::Vec(r)) => {
let (ty::Array(cmp_type, _) | ty::Slice(cmp_type)) = *cmp_type.kind() else {
return None;
};
let cmp_type = cmp_type.builtin_index()?;
iter::zip(l, r)
.map(|(li, ri)| Self::partial_cmp(tcx, cmp_type, li, ri))
.find(|r| r.is_none_or(|o| o != Ordering::Equal))