Skip to content

Commit a6a6936

Browse files
committed
Remove hir::ArrayLen, introduce ConstArgKind::Infer
Remove Node::ArrayLenInfer
1 parent b73a71c commit a6a6936

File tree

6 files changed

+21
-41
lines changed

6 files changed

+21
-41
lines changed

clippy_lints/src/large_stack_arrays.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use clippy_utils::diagnostics::span_lint_and_then;
55
use clippy_utils::is_from_proc_macro;
66
use clippy_utils::macros::macro_backtrace;
77
use clippy_utils::source::snippet;
8-
use rustc_hir::{ArrayLen, Expr, ExprKind, Item, ItemKind, Node};
8+
use rustc_hir::{Expr, ExprKind, Item, ItemKind, Node};
99
use rustc_lint::{LateContext, LateLintPass};
1010
use rustc_middle::ty::layout::LayoutOf;
1111
use rustc_middle::ty::{self, ConstKind};
@@ -118,13 +118,13 @@ impl<'tcx> LateLintPass<'tcx> for LargeStackArrays {
118118

119119
/// Only giving help messages if the expr does not contains macro expanded codes.
120120
fn might_be_expanded<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>) -> bool {
121-
/// Check if the span of `ArrayLen` of a repeat expression is within the expr's span,
121+
/// Check if the span of `ConstArg` of a repeat expression is within the expr's span,
122122
/// if not, meaning this repeat expr is definitely from some proc-macro.
123123
///
124124
/// This is a fail-safe to a case where even the `is_from_proc_macro` is unable to determain the
125125
/// correct result.
126126
fn repeat_expr_might_be_expanded(expr: &Expr<'_>) -> bool {
127-
let ExprKind::Repeat(_, ArrayLen::Body(len_ct)) = expr.kind else {
127+
let ExprKind::Repeat(_, len_ct) = expr.kind else {
128128
return false;
129129
};
130130
!expr.span.contains(len_ct.span())

clippy_lints/src/utils/author.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use rustc_ast::LitIntType;
33
use rustc_ast::ast::{LitFloatType, LitKind};
44
use rustc_data_structures::fx::FxHashMap;
55
use rustc_hir::{
6-
self as hir, ArrayLen, BindingMode, CaptureBy, Closure, ClosureKind, ConstArg, ConstArgKind, CoroutineKind,
6+
self as hir, BindingMode, CaptureBy, Closure, ClosureKind, ConstArg, ConstArgKind, CoroutineKind,
77
ExprKind, FnRetTy, HirId, Lit, PatKind, QPath, StmtKind, TyKind,
88
};
99
use rustc_lint::{LateContext, LateLintPass, LintContext};
@@ -278,6 +278,7 @@ impl<'a, 'tcx> PrintVisitor<'a, 'tcx> {
278278
chain!(self, "let ConstArgKind::Anon({anon_const}) = {const_arg}.kind");
279279
self.body(field!(anon_const.body));
280280
},
281+
ConstArgKind::Infer(..) => chain!(self, "let ConstArgKind::Infer(..) = {const_arg}.kind"),
281282
}
282283
}
283284

@@ -611,14 +612,7 @@ impl<'a, 'tcx> PrintVisitor<'a, 'tcx> {
611612
bind!(self, value, length);
612613
kind!("Repeat({value}, {length})");
613614
self.expr(value);
614-
match length.value {
615-
ArrayLen::Infer(..) => chain!(self, "let ArrayLen::Infer(..) = length"),
616-
ArrayLen::Body(const_arg) => {
617-
bind!(self, const_arg);
618-
chain!(self, "let ArrayLen::Body({const_arg}) = {length}");
619-
self.const_arg(const_arg);
620-
},
621-
}
615+
self.const_arg(length);
622616
},
623617
ExprKind::Err(_) => kind!("Err(_)"),
624618
ExprKind::DropTemps(expr) => {

clippy_lints/src/zero_repeat_side_effects.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use clippy_utils::visitors::for_each_expr_without_closures;
55
use rustc_ast::LitKind;
66
use rustc_data_structures::packed::Pu128;
77
use rustc_errors::Applicability;
8-
use rustc_hir::{ArrayLen, ConstArgKind, ExprKind, Node};
8+
use rustc_hir::{ConstArgKind, ExprKind, Node};
99
use rustc_lint::{LateContext, LateLintPass};
1010
use rustc_middle::ty::Ty;
1111
use rustc_session::declare_lint_pass;
@@ -60,8 +60,7 @@ impl LateLintPass<'_> for ZeroRepeatSideEffects {
6060
// doesn't seem as confusing as `[f(); 0]`. It would also have false positives when eg.
6161
// the const item depends on `#[cfg]s` and has different values in different compilation
6262
// sessions).
63-
else if let ExprKind::Repeat(inner_expr, length) = expr.kind
64-
&& let ArrayLen::Body(const_arg) = length
63+
else if let ExprKind::Repeat(inner_expr, const_arg) = expr.kind
6564
&& let ConstArgKind::Anon(anon_const) = const_arg.kind
6665
&& let length_expr = hir_map.body(anon_const.body).value
6766
&& !length_expr.span.from_expansion()

clippy_utils/src/hir_utils.rs

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_data_structures::fx::FxHasher;
77
use rustc_hir::MatchSource::TryDesugar;
88
use rustc_hir::def::{DefKind, Res};
99
use rustc_hir::{
10-
ArrayLen, AssocItemConstraint, BinOpKind, BindingMode, Block, BodyId, Closure, ConstArg, ConstArgKind, Expr,
10+
AssocItemConstraint, BinOpKind, BindingMode, Block, BodyId, Closure, ConstArg, ConstArgKind, Expr,
1111
ExprField, ExprKind, FnRetTy, GenericArg, GenericArgs, HirId, HirIdMap, InlineAsmOperand, LetExpr, Lifetime,
1212
LifetimeName, Pat, PatField, PatKind, Path, PathSegment, PrimTy, QPath, Stmt, StmtKind, TraitBoundModifiers, Ty,
1313
TyKind,
@@ -266,14 +266,6 @@ impl HirEqInterExpr<'_, '_, '_> {
266266
})
267267
}
268268

269-
pub fn eq_array_length(&mut self, left: ArrayLen<'_>, right: ArrayLen<'_>) -> bool {
270-
match (left, right) {
271-
(ArrayLen::Infer(..), ArrayLen::Infer(..)) => true,
272-
(ArrayLen::Body(l_ct), ArrayLen::Body(r_ct)) => self.eq_const_arg(l_ct, r_ct),
273-
(_, _) => false,
274-
}
275-
}
276-
277269
pub fn eq_body(&mut self, left: BodyId, right: BodyId) -> bool {
278270
// swap out TypeckResults when hashing a body
279271
let old_maybe_typeck_results = self.inner.maybe_typeck_results.replace((
@@ -383,7 +375,7 @@ impl HirEqInterExpr<'_, '_, '_> {
383375
},
384376
(ExprKind::Path(l), ExprKind::Path(r)) => self.eq_qpath(l, r),
385377
(&ExprKind::Repeat(le, ll), &ExprKind::Repeat(re, rl)) => {
386-
self.eq_expr(le, re) && self.eq_array_length(ll, rl)
378+
self.eq_expr(le, re) && self.eq_const_arg(ll, rl)
387379
},
388380
(ExprKind::Ret(l), ExprKind::Ret(r)) => both(l.as_ref(), r.as_ref(), |l, r| self.eq_expr(l, r)),
389381
(&ExprKind::Struct(l_path, lf, ref lo), &ExprKind::Struct(r_path, rf, ref ro)) => {
@@ -469,8 +461,10 @@ impl HirEqInterExpr<'_, '_, '_> {
469461
match (&left.kind, &right.kind) {
470462
(ConstArgKind::Path(l_p), ConstArgKind::Path(r_p)) => self.eq_qpath(l_p, r_p),
471463
(ConstArgKind::Anon(l_an), ConstArgKind::Anon(r_an)) => self.eq_body(l_an.body, r_an.body),
464+
(ConstArgKind::Infer(..), ConstArgKind::Infer(..)) => true,
472465
// Use explicit match for now since ConstArg is undergoing flux.
473-
(ConstArgKind::Path(..), ConstArgKind::Anon(..)) | (ConstArgKind::Anon(..), ConstArgKind::Path(..)) => {
466+
(ConstArgKind::Path(..), ConstArgKind::Anon(..)) | (ConstArgKind::Anon(..), ConstArgKind::Path(..))
467+
| (ConstArgKind::Infer(..), _) | (_, ConstArgKind::Infer(..)) => {
474468
false
475469
},
476470
}
@@ -589,7 +583,7 @@ impl HirEqInterExpr<'_, '_, '_> {
589583
pub fn eq_ty(&mut self, left: &Ty<'_>, right: &Ty<'_>) -> bool {
590584
match (&left.kind, &right.kind) {
591585
(&TyKind::Slice(l_vec), &TyKind::Slice(r_vec)) => self.eq_ty(l_vec, r_vec),
592-
(&TyKind::Array(lt, ll), &TyKind::Array(rt, rl)) => self.eq_ty(lt, rt) && self.eq_array_length(ll, rl),
586+
(&TyKind::Array(lt, ll), &TyKind::Array(rt, rl)) => self.eq_ty(lt, rt) && self.eq_const_arg(ll, rl),
593587
(TyKind::Ptr(l_mut), TyKind::Ptr(r_mut)) => l_mut.mutbl == r_mut.mutbl && self.eq_ty(l_mut.ty, r_mut.ty),
594588
(TyKind::Ref(_, l_rmut), TyKind::Ref(_, r_rmut)) => {
595589
l_rmut.mutbl == r_rmut.mutbl && self.eq_ty(l_rmut.ty, r_rmut.ty)
@@ -1002,7 +996,7 @@ impl<'a, 'tcx> SpanlessHash<'a, 'tcx> {
1002996
},
1003997
ExprKind::Repeat(e, len) => {
1004998
self.hash_expr(e);
1005-
self.hash_array_length(len);
999+
self.hash_const_arg(len);
10061000
},
10071001
ExprKind::Ret(ref e) => {
10081002
if let Some(e) = *e {
@@ -1195,7 +1189,7 @@ impl<'a, 'tcx> SpanlessHash<'a, 'tcx> {
11951189
},
11961190
&TyKind::Array(ty, len) => {
11971191
self.hash_ty(ty);
1198-
self.hash_array_length(len);
1192+
self.hash_const_arg(len);
11991193
},
12001194
TyKind::Pat(ty, pat) => {
12011195
self.hash_ty(ty);
@@ -1246,13 +1240,6 @@ impl<'a, 'tcx> SpanlessHash<'a, 'tcx> {
12461240
}
12471241
}
12481242

1249-
pub fn hash_array_length(&mut self, length: ArrayLen<'_>) {
1250-
match length {
1251-
ArrayLen::Infer(..) => {},
1252-
ArrayLen::Body(ct) => self.hash_const_arg(ct),
1253-
}
1254-
}
1255-
12561243
pub fn hash_body(&mut self, body_id: BodyId) {
12571244
// swap out TypeckResults when hashing a body
12581245
let old_maybe_typeck_results = self.maybe_typeck_results.replace(self.cx.tcx.typeck_body(body_id));
@@ -1264,6 +1251,7 @@ impl<'a, 'tcx> SpanlessHash<'a, 'tcx> {
12641251
match &const_arg.kind {
12651252
ConstArgKind::Path(path) => self.hash_qpath(path),
12661253
ConstArgKind::Anon(anon) => self.hash_body(anon.body),
1254+
ConstArgKind::Infer(..) => {},
12671255
}
12681256
}
12691257

clippy_utils/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ use rustc_hir::definitions::{DefPath, DefPathData};
102102
use rustc_hir::hir_id::{HirIdMap, HirIdSet};
103103
use rustc_hir::intravisit::{FnKind, Visitor, walk_expr};
104104
use rustc_hir::{
105-
self as hir, Arm, ArrayLen, BindingMode, Block, BlockCheckMode, Body, ByRef, Closure, ConstArgKind, ConstContext,
105+
self as hir, Arm, BindingMode, Block, BlockCheckMode, Body, ByRef, Closure, ConstArgKind, ConstContext,
106106
Destination, Expr, ExprField, ExprKind, FnDecl, FnRetTy, GenericArgs, HirId, Impl, ImplItem, ImplItemKind,
107107
ImplItemRef, Item, ItemKind, LangItem, LetStmt, MatchSource, Mutability, Node, OwnerId, OwnerNode, Param, Pat,
108108
PatKind, Path, PathSegment, PrimTy, QPath, Stmt, StmtKind, TraitItem, TraitItemKind, TraitItemRef, TraitRef,
@@ -921,7 +921,7 @@ pub fn is_default_equivalent(cx: &LateContext<'_>, e: &Expr<'_>) -> bool {
921921
_ => false,
922922
},
923923
ExprKind::Tup(items) | ExprKind::Array(items) => items.iter().all(|x| is_default_equivalent(cx, x)),
924-
ExprKind::Repeat(x, ArrayLen::Body(len)) => {
924+
ExprKind::Repeat(x, len) => {
925925
if let ConstArgKind::Anon(anon_const) = len.kind
926926
&& let ExprKind::Lit(const_lit) = cx.tcx.hir().body(anon_const.body).value.kind
927927
&& let LitKind::Int(v, _) = const_lit.node
@@ -951,7 +951,7 @@ fn is_default_equivalent_from(cx: &LateContext<'_>, from_func: &Expr<'_>, arg: &
951951
..
952952
}) => return sym.is_empty() && is_path_lang_item(cx, ty, LangItem::String),
953953
ExprKind::Array([]) => return is_path_diagnostic_item(cx, ty, sym::Vec),
954-
ExprKind::Repeat(_, ArrayLen::Body(len)) => {
954+
ExprKind::Repeat(_, len) => {
955955
if let ConstArgKind::Anon(anon_const) = len.kind
956956
&& let ExprKind::Lit(const_lit) = cx.tcx.hir().body(anon_const.body).value.kind
957957
&& let LitKind::Int(v, _) = const_lit.node

tests/ui/author/repeat.stdout

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
if let ExprKind::Repeat(value, length) = expr.kind
22
&& let ExprKind::Lit(ref lit) = value.kind
33
&& let LitKind::Int(1, LitIntType::Unsigned(UintTy::U8)) = lit.node
4-
&& let ArrayLen::Body(const_arg) = length
5-
&& let ConstArgKind::Anon(anon_const) = const_arg.kind
4+
&& let ConstArgKind::Anon(anon_const) = length.kind
65
&& expr1 = &cx.tcx.hir().body(anon_const.body).value
76
&& let ExprKind::Lit(ref lit1) = expr1.kind
87
&& let LitKind::Int(5, LitIntType::Unsuffixed) = lit1.node

0 commit comments

Comments
 (0)