Skip to content

Commit 69b0886

Browse files
Fix tests
1 parent 91a458f commit 69b0886

File tree

11 files changed

+23
-27
lines changed

11 files changed

+23
-27
lines changed

clippy_lints/src/indexing_slicing.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ impl<'tcx> LateLintPass<'tcx> for IndexingSlicing {
114114
if let Some(range) = higher::Range::hir(index) {
115115
// Ranged indexes, i.e., &x[n..m], &x[n..], &x[..n] and &x[..]
116116
if let ty::Array(_, s) = ty.kind() {
117-
let size: u128 = if let Some(size) = s.try_eval_target_usize(cx.tcx, cx.param_env) {
117+
let size: u128 = if let Some(size) = s.try_to_target_usize(cx.tcx) {
118118
size.into()
119119
} else {
120120
return;
@@ -183,7 +183,7 @@ impl<'tcx> LateLintPass<'tcx> for IndexingSlicing {
183183
&& let ty::Uint(utype) = cx.typeck_results().expr_ty(index).kind()
184184
&& *utype == ty::UintTy::Usize
185185
&& let ty::Array(_, s) = ty.kind()
186-
&& let Some(size) = s.try_eval_target_usize(cx.tcx, cx.param_env)
186+
&& let Some(size) = s.try_to_target_usize(cx.tcx)
187187
{
188188
// get constant offset and check whether it is in bounds
189189
let off = usize::try_from(off).unwrap();

clippy_lints/src/loops/explicit_iter_loop.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub(super) fn check(
3030
return;
3131
}
3232
} else if count
33-
.try_eval_target_usize(cx.tcx, cx.param_env)
33+
.try_to_target_usize(cx.tcx)
3434
.map_or(true, |x| x > 32)
3535
&& !msrv.meets(msrvs::ARRAY_IMPL_ANY_LEN)
3636
{

clippy_lints/src/loops/manual_memcpy.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ fn is_array_length_equal_to_range(cx: &LateContext<'_>, start: &Expr<'_>, end: &
472472
let arr_ty = cx.typeck_results().expr_ty(arr).peel_refs();
473473

474474
if let ty::Array(_, s) = arr_ty.kind() {
475-
let size: u128 = if let Some(size) = s.try_eval_target_usize(cx.tcx, cx.param_env) {
475+
let size: u128 = if let Some(size) = s.try_to_target_usize(cx.tcx) {
476476
size.into()
477477
} else {
478478
return false;

clippy_lints/src/loops/needless_range_loop.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ fn is_end_eq_array_len<'tcx>(
207207
if let ExprKind::Lit(lit) = end.kind
208208
&& let ast::LitKind::Int(end_int, _) = lit.node
209209
&& let ty::Array(_, arr_len_const) = indexed_ty.kind()
210-
&& let Some(arr_len) = arr_len_const.try_eval_target_usize(cx.tcx, cx.param_env)
210+
&& let Some(arr_len) = arr_len_const.try_to_target_usize(cx.tcx)
211211
{
212212
return match limits {
213213
ast::RangeLimits::Closed => end_int.get() + 1 >= arr_len.into(),

clippy_lints/src/matches/single_match.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc_hir::def::{DefKind, Res};
1111
use rustc_hir::intravisit::{Visitor, walk_pat};
1212
use rustc_hir::{Arm, Expr, ExprKind, HirId, Node, Pat, PatKind, QPath, StmtKind};
1313
use rustc_lint::LateContext;
14-
use rustc_middle::ty::{self, AdtDef, ParamEnv, TyCtxt, TypeckResults, VariantDef};
14+
use rustc_middle::ty::{self, AdtDef, TyCtxt, TypeckResults, VariantDef};
1515
use rustc_span::{Span, sym};
1616

1717
use super::{MATCH_BOOL, SINGLE_MATCH, SINGLE_MATCH_ELSE};
@@ -67,7 +67,6 @@ pub(crate) fn check<'tcx>(cx: &LateContext<'tcx>, ex: &'tcx Expr<'_>, arms: &'tc
6767
if v.has_enum {
6868
let cx = PatCtxt {
6969
tcx: cx.tcx,
70-
param_env: cx.param_env,
7170
typeck,
7271
arena: DroplessArena::default(),
7372
};
@@ -185,7 +184,6 @@ impl<'tcx> Visitor<'tcx> for PatVisitor<'tcx> {
185184
/// The context needed to manipulate a `PatState`.
186185
struct PatCtxt<'tcx> {
187186
tcx: TyCtxt<'tcx>,
188-
param_env: ParamEnv<'tcx>,
189187
typeck: &'tcx TypeckResults<'tcx>,
190188
arena: DroplessArena,
191189
}
@@ -334,7 +332,7 @@ impl<'a> PatState<'a> {
334332
if match *cx.typeck.pat_ty(pat).peel_refs().kind() {
335333
ty::Adt(adt, _) => adt.is_enum() || (adt.is_struct() && !adt.non_enum_variant().fields.is_empty()),
336334
ty::Tuple(tys) => !tys.is_empty(),
337-
ty::Array(_, len) => len.try_eval_target_usize(cx.tcx, cx.param_env) != Some(1),
335+
ty::Array(_, len) => len.try_to_target_usize(cx.tcx) != Some(1),
338336
ty::Slice(..) => true,
339337
_ => false,
340338
} =>
@@ -353,7 +351,7 @@ impl<'a> PatState<'a> {
353351
},
354352
PatKind::Slice([sub_pat], _, []) | PatKind::Slice([], _, [sub_pat])
355353
if let ty::Array(_, len) = *cx.typeck.pat_ty(pat).kind()
356-
&& len.try_eval_target_usize(cx.tcx, cx.param_env) == Some(1) =>
354+
&& len.try_to_target_usize(cx.tcx) == Some(1) =>
357355
{
358356
self.add_pat(cx, sub_pat)
359357
},

clippy_lints/src/methods/iter_out_of_bounds.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ fn get_iterator_length<'tcx>(cx: &LateContext<'tcx>, iter: &'tcx Expr<'tcx>) ->
3131
// parameter.
3232
substs
3333
.const_at(1)
34-
.try_eval_target_usize(cx.tcx, cx.param_env)
34+
.try_to_target_usize(cx.tcx)
3535
.map(u128::from)
3636
} else if cx.tcx.is_diagnostic_item(sym::SliceIter, did)
3737
&& let ExprKind::MethodCall(_, recv, ..) = iter.kind
3838
{
3939
if let ty::Array(_, len) = cx.typeck_results().expr_ty(recv).peel_refs().kind() {
4040
// For slice::Iter<'_, T>, the receiver might be an array literal: [1,2,3].iter().skip(..)
41-
len.try_eval_target_usize(cx.tcx, cx.param_env).map(u128::from)
41+
len.try_to_target_usize(cx.tcx).map(u128::from)
4242
} else if let Some(args) = VecArgs::hir(cx, expr_or_init(cx, recv)) {
4343
match args {
4444
VecArgs::Vec(vec) => vec.len().try_into().ok(),

clippy_lints/src/methods/utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub(super) fn derefs_to_slice<'tcx>(
1818
ty::Slice(_) => true,
1919
ty::Adt(..) if let Some(boxed) = ty.boxed_ty() => may_slice(cx, boxed),
2020
ty::Adt(..) => is_type_diagnostic_item(cx, ty, sym::Vec),
21-
ty::Array(_, size) => size.try_eval_target_usize(cx.tcx, cx.param_env).is_some(),
21+
ty::Array(_, size) => size.try_to_target_usize(cx.tcx).is_some(),
2222
ty::Ref(_, inner, _) => may_slice(cx, *inner),
2323
_ => false,
2424
}

clippy_lints/src/trailing_empty_array.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use clippy_utils::diagnostics::span_lint_and_help;
22
use clippy_utils::has_repr_attr;
33
use rustc_hir::{Item, ItemKind};
44
use rustc_lint::{LateContext, LateLintPass};
5-
use rustc_middle::ty::{Const, FeedConstTy};
5+
use rustc_middle::ty;
66
use rustc_session::declare_lint_pass;
77

88
declare_clippy_lint! {
@@ -55,16 +55,14 @@ impl<'tcx> LateLintPass<'tcx> for TrailingEmptyArray {
5555

5656
fn is_struct_with_trailing_zero_sized_array<'tcx>(cx: &LateContext<'tcx>, item: &Item<'tcx>) -> bool {
5757
if let ItemKind::Struct(data, _) = &item.kind
58-
// First check if last field is an array
5958
&& let Some(last_field) = data.fields().last()
60-
&& let rustc_hir::TyKind::Array(_, rustc_hir::ArrayLen::Body(length)) = last_field.ty.kind
61-
62-
// Then check if that array is zero-sized
63-
&& let length = Const::from_const_arg(cx.tcx, length, FeedConstTy::No)
64-
&& let length = length.try_eval_target_usize(cx.tcx, cx.param_env)
65-
&& let Some(length) = length
59+
&& let field_ty = cx
60+
.tcx
61+
.normalize_erasing_regions(cx.param_env, cx.tcx.type_of(last_field.def_id).instantiate_identity())
62+
&& let ty::Array(_, array_len) = *field_ty.kind()
63+
&& let Some(0) = array_len.try_to_target_usize(cx.tcx)
6664
{
67-
length == 0
65+
true
6866
} else {
6967
false
7068
}

clippy_lints/src/tuple_array_conversions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ fn all_bindings_are_for_conv<'tcx>(
190190
tys.len() == elements.len() && tys.iter().chain(final_tys.iter().copied()).all_equal()
191191
},
192192
(ToType::Tuple, ty::Array(ty, len)) => {
193-
let Some(len) = len.try_eval_target_usize(cx.tcx, cx.param_env) else { return false };
193+
let Some(len) = len.try_to_target_usize(cx.tcx) else { return false };
194194
len as usize == elements.len() && final_tys.iter().chain(once(ty)).all_equal()
195195
},
196196
_ => false,

clippy_utils/src/consts.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ impl<'tcx> ConstEvalCtxt<'tcx> {
472472
ExprKind::Tup(tup) => self.multi(tup).map(Constant::Tuple),
473473
ExprKind::Repeat(value, _) => {
474474
let n = match self.typeck.expr_ty(e).kind() {
475-
ty::Array(_, n) => n.try_eval_target_usize(self.tcx, self.param_env)?,
475+
ty::Array(_, n) => n.try_to_target_usize(self.tcx)?,
476476
_ => span_bug!(e.span, "typeck error"),
477477
};
478478
self.expr(value).map(|v| Constant::Repeat(Box::new(v), n))
@@ -553,7 +553,7 @@ impl<'tcx> ConstEvalCtxt<'tcx> {
553553
ExprKind::Array(vec) => self.multi(vec).map(|v| v.is_empty()),
554554
ExprKind::Repeat(..) => {
555555
if let ty::Array(_, n) = self.typeck.expr_ty(e).kind() {
556-
Some(n.try_eval_target_usize(self.tcx, self.param_env)? == 0)
556+
Some(n.try_to_target_usize(self.tcx)? == 0)
557557
} else {
558558
span_bug!(e.span, "typeck error");
559559
}

clippy_utils/src/ty.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -989,7 +989,7 @@ pub fn approx_ty_size<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> u64 {
989989
(Ok(size), _) => size,
990990
(Err(_), ty::Tuple(list)) => list.iter().map(|t| approx_ty_size(cx, t)).sum(),
991991
(Err(_), ty::Array(t, n)) => {
992-
n.try_eval_target_usize(cx.tcx, cx.param_env).unwrap_or_default() * approx_ty_size(cx, *t)
992+
n.try_to_target_usize(cx.tcx).unwrap_or_default() * approx_ty_size(cx, *t)
993993
},
994994
(Err(_), ty::Adt(def, subst)) if def.is_struct() => def
995995
.variants()
@@ -1207,7 +1207,7 @@ impl<'tcx> InteriorMut<'tcx> {
12071207
let chain = match *ty.kind() {
12081208
ty::RawPtr(inner_ty, _) if !self.ignore_pointers => self.interior_mut_ty_chain(cx, inner_ty),
12091209
ty::Ref(_, inner_ty, _) | ty::Slice(inner_ty) => self.interior_mut_ty_chain(cx, inner_ty),
1210-
ty::Array(inner_ty, size) if size.try_eval_target_usize(cx.tcx, cx.param_env) != Some(0) => {
1210+
ty::Array(inner_ty, size) if size.try_to_target_usize(cx.tcx) != Some(0) => {
12111211
self.interior_mut_ty_chain(cx, inner_ty)
12121212
},
12131213
ty::Tuple(fields) => fields.iter().find_map(|ty| self.interior_mut_ty_chain(cx, ty)),

0 commit comments

Comments
 (0)