Skip to content

Commit 550aed8

Browse files
committed
Use builin_index instead of hand-rolling it
1 parent d76fe15 commit 550aed8

File tree

6 files changed

+11
-21
lines changed

6 files changed

+11
-21
lines changed

compiler/rustc_hir_analysis/src/check/wfcheck.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1048,8 +1048,7 @@ fn check_param_wf(tcx: TyCtxt<'_>, param: &hir::GenericParam<'_>) -> Result<(),
10481048
match ty.kind() {
10491049
ty::Adt(adt_def, ..) => adt_def.did().is_local(),
10501050
// Arrays and slices use the inner type's `ConstParamTy`.
1051-
ty::Array(ty, ..) => ty_is_local(*ty),
1052-
ty::Slice(ty) => ty_is_local(*ty),
1051+
ty::Array(ty, ..) | ty::Slice(ty) => ty_is_local(*ty),
10531052
// `&` references use the inner type's `ConstParamTy`.
10541053
// `&mut` are not supported.
10551054
ty::Ref(_, ty, ast::Mutability::Not) => ty_is_local(*ty),

compiler/rustc_hir_typeck/src/expr.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1793,10 +1793,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
17931793
let element_ty = if !args.is_empty() {
17941794
let coerce_to = expected
17951795
.to_option(self)
1796-
.and_then(|uty| match *self.try_structurally_resolve_type(expr.span, uty).kind() {
1797-
ty::Array(ty, _) | ty::Slice(ty) => Some(ty),
1798-
_ => None,
1799-
})
1796+
.and_then(|uty| self.try_structurally_resolve_type(expr.span, uty).builtin_index())
18001797
.unwrap_or_else(|| self.next_ty_var(expr.span));
18011798
let mut coerce = CoerceMany::with_coercion_sites(coerce_to, args);
18021799
assert_eq!(self.diverges.get(), Diverges::Maybe);
@@ -1874,10 +1871,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
18741871
}
18751872

18761873
let uty = match expected {
1877-
ExpectHasType(uty) => match *uty.kind() {
1878-
ty::Array(ty, _) | ty::Slice(ty) => Some(ty),
1879-
_ => None,
1880-
},
1874+
ExpectHasType(uty) => uty.builtin_index(),
18811875
_ => None,
18821876
};
18831877

compiler/rustc_pattern_analysis/src/rustc.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -249,12 +249,12 @@ impl<'p, 'tcx: 'p> RustcPatCtxt<'p, 'tcx> {
249249
ty::Ref(_, rty, _) => reveal_and_alloc(cx, once(*rty)),
250250
_ => bug!("Unexpected type for `Ref` constructor: {ty:?}"),
251251
},
252-
Slice(slice) => match *ty.kind() {
253-
ty::Slice(ty) | ty::Array(ty, _) => {
252+
Slice(slice) => match ty.builtin_index() {
253+
Some(ty) => {
254254
let arity = slice.arity();
255255
reveal_and_alloc(cx, (0..arity).map(|_| ty))
256256
}
257-
_ => bug!("bad slice pattern {:?} {:?}", ctor, ty),
257+
None => bug!("bad slice pattern {:?} {:?}", ctor, ty),
258258
},
259259
DerefPattern(pointee_ty) => reveal_and_alloc(cx, once(pointee_ty.inner())),
260260
Bool(..) | IntRange(..) | F16Range(..) | F32Range(..) | F64Range(..)

src/tools/clippy/clippy_lints/src/index_refutable_slice.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ use rustc_hir::HirId;
1212
use rustc_hir::intravisit::{self, Visitor};
1313
use rustc_lint::{LateContext, LateLintPass};
1414
use rustc_middle::hir::nested_filter;
15-
use rustc_middle::ty;
1615
use rustc_session::impl_lint_pass;
1716
use rustc_span::Span;
1817
use rustc_span::symbol::Ident;
@@ -109,11 +108,11 @@ fn find_slice_values(cx: &LateContext<'_>, pat: &hir::Pat<'_>) -> FxIndexMap<Hir
109108
}
110109

111110
let bound_ty = cx.typeck_results().node_type(pat.hir_id);
112-
if let ty::Slice(inner_ty) | ty::Array(inner_ty, _) = bound_ty.peel_refs().kind() {
111+
if let Some(inner_ty) = bound_ty.peel_refs().builtin_index() {
113112
// The values need to use the `ref` keyword if they can't be copied.
114113
// This will need to be adjusted if the lint want to support mutable access in the future
115114
let src_is_ref = bound_ty.is_ref() && by_ref == hir::ByRef::No;
116-
let needs_ref = !(src_is_ref || is_copy(cx, *inner_ty));
115+
let needs_ref = !(src_is_ref || is_copy(cx, inner_ty));
117116

118117
let slice_info = slices
119118
.entry(value_hir_id)

src/tools/clippy/clippy_lints/src/tuple_array_conversions.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ impl LateLintPass<'_> for TupleArrayConversions {
6666
}
6767

6868
fn check_array<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>, elements: &'tcx [Expr<'tcx>]) {
69-
let (ty::Array(ty, _) | ty::Slice(ty)) = cx.typeck_results().expr_ty(expr).kind() else {
69+
let Some(ty) = cx.typeck_results().expr_ty(expr).builtin_index() else {
7070
unreachable!("`expr` must be an array or slice due to `ExprKind::Array`");
7171
};
7272

@@ -85,7 +85,7 @@ fn check_array<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>, elements: &
8585
ExprKind::Path(_) => Some(elements.iter().collect()),
8686
_ => None,
8787
})
88-
&& all_bindings_are_for_conv(cx, &[*ty], expr, elements, &locals, ToType::Array)
88+
&& all_bindings_are_for_conv(cx, &[ty], expr, elements, &locals, ToType::Array)
8989
&& !is_from_proc_macro(cx, expr)
9090
{
9191
span_lint_and_help(

src/tools/clippy/clippy_utils/src/consts.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -235,9 +235,7 @@ impl Constant<'_> {
235235
_ => None,
236236
},
237237
(Self::Vec(l), Self::Vec(r)) => {
238-
let (ty::Array(cmp_type, _) | ty::Slice(cmp_type)) = *cmp_type.kind() else {
239-
return None;
240-
};
238+
let cmp_type = cmp_type.builtin_index()?;
241239
iter::zip(l, r)
242240
.map(|(li, ri)| Self::partial_cmp(tcx, cmp_type, li, ri))
243241
.find(|r| r.is_none_or(|o| o != Ordering::Equal))

0 commit comments

Comments
 (0)