mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-22 12:43:18 +00:00
Fix tests
This commit is contained in:
parent
91a458f451
commit
69b088626c
11 changed files with 23 additions and 27 deletions
|
@ -114,7 +114,7 @@ impl<'tcx> LateLintPass<'tcx> for IndexingSlicing {
|
|||
if let Some(range) = higher::Range::hir(index) {
|
||||
// Ranged indexes, i.e., &x[n..m], &x[n..], &x[..n] and &x[..]
|
||||
if let ty::Array(_, s) = ty.kind() {
|
||||
let size: u128 = if let Some(size) = s.try_eval_target_usize(cx.tcx, cx.param_env) {
|
||||
let size: u128 = if let Some(size) = s.try_to_target_usize(cx.tcx) {
|
||||
size.into()
|
||||
} else {
|
||||
return;
|
||||
|
@ -183,7 +183,7 @@ impl<'tcx> LateLintPass<'tcx> for IndexingSlicing {
|
|||
&& let ty::Uint(utype) = cx.typeck_results().expr_ty(index).kind()
|
||||
&& *utype == ty::UintTy::Usize
|
||||
&& let ty::Array(_, s) = ty.kind()
|
||||
&& let Some(size) = s.try_eval_target_usize(cx.tcx, cx.param_env)
|
||||
&& let Some(size) = s.try_to_target_usize(cx.tcx)
|
||||
{
|
||||
// get constant offset and check whether it is in bounds
|
||||
let off = usize::try_from(off).unwrap();
|
||||
|
|
|
@ -30,7 +30,7 @@ pub(super) fn check(
|
|||
return;
|
||||
}
|
||||
} else if count
|
||||
.try_eval_target_usize(cx.tcx, cx.param_env)
|
||||
.try_to_target_usize(cx.tcx)
|
||||
.map_or(true, |x| x > 32)
|
||||
&& !msrv.meets(msrvs::ARRAY_IMPL_ANY_LEN)
|
||||
{
|
||||
|
|
|
@ -472,7 +472,7 @@ fn is_array_length_equal_to_range(cx: &LateContext<'_>, start: &Expr<'_>, end: &
|
|||
let arr_ty = cx.typeck_results().expr_ty(arr).peel_refs();
|
||||
|
||||
if let ty::Array(_, s) = arr_ty.kind() {
|
||||
let size: u128 = if let Some(size) = s.try_eval_target_usize(cx.tcx, cx.param_env) {
|
||||
let size: u128 = if let Some(size) = s.try_to_target_usize(cx.tcx) {
|
||||
size.into()
|
||||
} else {
|
||||
return false;
|
||||
|
|
|
@ -207,7 +207,7 @@ fn is_end_eq_array_len<'tcx>(
|
|||
if let ExprKind::Lit(lit) = end.kind
|
||||
&& let ast::LitKind::Int(end_int, _) = lit.node
|
||||
&& let ty::Array(_, arr_len_const) = indexed_ty.kind()
|
||||
&& let Some(arr_len) = arr_len_const.try_eval_target_usize(cx.tcx, cx.param_env)
|
||||
&& let Some(arr_len) = arr_len_const.try_to_target_usize(cx.tcx)
|
||||
{
|
||||
return match limits {
|
||||
ast::RangeLimits::Closed => end_int.get() + 1 >= arr_len.into(),
|
||||
|
|
|
@ -11,7 +11,7 @@ use rustc_hir::def::{DefKind, Res};
|
|||
use rustc_hir::intravisit::{Visitor, walk_pat};
|
||||
use rustc_hir::{Arm, Expr, ExprKind, HirId, Node, Pat, PatKind, QPath, StmtKind};
|
||||
use rustc_lint::LateContext;
|
||||
use rustc_middle::ty::{self, AdtDef, ParamEnv, TyCtxt, TypeckResults, VariantDef};
|
||||
use rustc_middle::ty::{self, AdtDef, TyCtxt, TypeckResults, VariantDef};
|
||||
use rustc_span::{Span, sym};
|
||||
|
||||
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
|
|||
if v.has_enum {
|
||||
let cx = PatCtxt {
|
||||
tcx: cx.tcx,
|
||||
param_env: cx.param_env,
|
||||
typeck,
|
||||
arena: DroplessArena::default(),
|
||||
};
|
||||
|
@ -185,7 +184,6 @@ impl<'tcx> Visitor<'tcx> for PatVisitor<'tcx> {
|
|||
/// The context needed to manipulate a `PatState`.
|
||||
struct PatCtxt<'tcx> {
|
||||
tcx: TyCtxt<'tcx>,
|
||||
param_env: ParamEnv<'tcx>,
|
||||
typeck: &'tcx TypeckResults<'tcx>,
|
||||
arena: DroplessArena,
|
||||
}
|
||||
|
@ -334,7 +332,7 @@ impl<'a> PatState<'a> {
|
|||
if match *cx.typeck.pat_ty(pat).peel_refs().kind() {
|
||||
ty::Adt(adt, _) => adt.is_enum() || (adt.is_struct() && !adt.non_enum_variant().fields.is_empty()),
|
||||
ty::Tuple(tys) => !tys.is_empty(),
|
||||
ty::Array(_, len) => len.try_eval_target_usize(cx.tcx, cx.param_env) != Some(1),
|
||||
ty::Array(_, len) => len.try_to_target_usize(cx.tcx) != Some(1),
|
||||
ty::Slice(..) => true,
|
||||
_ => false,
|
||||
} =>
|
||||
|
@ -353,7 +351,7 @@ impl<'a> PatState<'a> {
|
|||
},
|
||||
PatKind::Slice([sub_pat], _, []) | PatKind::Slice([], _, [sub_pat])
|
||||
if let ty::Array(_, len) = *cx.typeck.pat_ty(pat).kind()
|
||||
&& len.try_eval_target_usize(cx.tcx, cx.param_env) == Some(1) =>
|
||||
&& len.try_to_target_usize(cx.tcx) == Some(1) =>
|
||||
{
|
||||
self.add_pat(cx, sub_pat)
|
||||
},
|
||||
|
|
|
@ -31,14 +31,14 @@ fn get_iterator_length<'tcx>(cx: &LateContext<'tcx>, iter: &'tcx Expr<'tcx>) ->
|
|||
// parameter.
|
||||
substs
|
||||
.const_at(1)
|
||||
.try_eval_target_usize(cx.tcx, cx.param_env)
|
||||
.try_to_target_usize(cx.tcx)
|
||||
.map(u128::from)
|
||||
} else if cx.tcx.is_diagnostic_item(sym::SliceIter, did)
|
||||
&& let ExprKind::MethodCall(_, recv, ..) = iter.kind
|
||||
{
|
||||
if let ty::Array(_, len) = cx.typeck_results().expr_ty(recv).peel_refs().kind() {
|
||||
// For slice::Iter<'_, T>, the receiver might be an array literal: [1,2,3].iter().skip(..)
|
||||
len.try_eval_target_usize(cx.tcx, cx.param_env).map(u128::from)
|
||||
len.try_to_target_usize(cx.tcx).map(u128::from)
|
||||
} else if let Some(args) = VecArgs::hir(cx, expr_or_init(cx, recv)) {
|
||||
match args {
|
||||
VecArgs::Vec(vec) => vec.len().try_into().ok(),
|
||||
|
|
|
@ -18,7 +18,7 @@ pub(super) fn derefs_to_slice<'tcx>(
|
|||
ty::Slice(_) => true,
|
||||
ty::Adt(..) if let Some(boxed) = ty.boxed_ty() => may_slice(cx, boxed),
|
||||
ty::Adt(..) => is_type_diagnostic_item(cx, ty, sym::Vec),
|
||||
ty::Array(_, size) => size.try_eval_target_usize(cx.tcx, cx.param_env).is_some(),
|
||||
ty::Array(_, size) => size.try_to_target_usize(cx.tcx).is_some(),
|
||||
ty::Ref(_, inner, _) => may_slice(cx, *inner),
|
||||
_ => false,
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@ use clippy_utils::diagnostics::span_lint_and_help;
|
|||
use clippy_utils::has_repr_attr;
|
||||
use rustc_hir::{Item, ItemKind};
|
||||
use rustc_lint::{LateContext, LateLintPass};
|
||||
use rustc_middle::ty::{Const, FeedConstTy};
|
||||
use rustc_middle::ty;
|
||||
use rustc_session::declare_lint_pass;
|
||||
|
||||
declare_clippy_lint! {
|
||||
|
@ -55,16 +55,14 @@ impl<'tcx> LateLintPass<'tcx> for TrailingEmptyArray {
|
|||
|
||||
fn is_struct_with_trailing_zero_sized_array<'tcx>(cx: &LateContext<'tcx>, item: &Item<'tcx>) -> bool {
|
||||
if let ItemKind::Struct(data, _) = &item.kind
|
||||
// First check if last field is an array
|
||||
&& let Some(last_field) = data.fields().last()
|
||||
&& let rustc_hir::TyKind::Array(_, rustc_hir::ArrayLen::Body(length)) = last_field.ty.kind
|
||||
|
||||
// Then check if that array is zero-sized
|
||||
&& let length = Const::from_const_arg(cx.tcx, length, FeedConstTy::No)
|
||||
&& let length = length.try_eval_target_usize(cx.tcx, cx.param_env)
|
||||
&& let Some(length) = length
|
||||
&& let field_ty = cx
|
||||
.tcx
|
||||
.normalize_erasing_regions(cx.param_env, cx.tcx.type_of(last_field.def_id).instantiate_identity())
|
||||
&& let ty::Array(_, array_len) = *field_ty.kind()
|
||||
&& let Some(0) = array_len.try_to_target_usize(cx.tcx)
|
||||
{
|
||||
length == 0
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
|
|
|
@ -190,7 +190,7 @@ fn all_bindings_are_for_conv<'tcx>(
|
|||
tys.len() == elements.len() && tys.iter().chain(final_tys.iter().copied()).all_equal()
|
||||
},
|
||||
(ToType::Tuple, ty::Array(ty, len)) => {
|
||||
let Some(len) = len.try_eval_target_usize(cx.tcx, cx.param_env) else { return false };
|
||||
let Some(len) = len.try_to_target_usize(cx.tcx) else { return false };
|
||||
len as usize == elements.len() && final_tys.iter().chain(once(ty)).all_equal()
|
||||
},
|
||||
_ => false,
|
||||
|
|
|
@ -472,7 +472,7 @@ impl<'tcx> ConstEvalCtxt<'tcx> {
|
|||
ExprKind::Tup(tup) => self.multi(tup).map(Constant::Tuple),
|
||||
ExprKind::Repeat(value, _) => {
|
||||
let n = match self.typeck.expr_ty(e).kind() {
|
||||
ty::Array(_, n) => n.try_eval_target_usize(self.tcx, self.param_env)?,
|
||||
ty::Array(_, n) => n.try_to_target_usize(self.tcx)?,
|
||||
_ => span_bug!(e.span, "typeck error"),
|
||||
};
|
||||
self.expr(value).map(|v| Constant::Repeat(Box::new(v), n))
|
||||
|
@ -553,7 +553,7 @@ impl<'tcx> ConstEvalCtxt<'tcx> {
|
|||
ExprKind::Array(vec) => self.multi(vec).map(|v| v.is_empty()),
|
||||
ExprKind::Repeat(..) => {
|
||||
if let ty::Array(_, n) = self.typeck.expr_ty(e).kind() {
|
||||
Some(n.try_eval_target_usize(self.tcx, self.param_env)? == 0)
|
||||
Some(n.try_to_target_usize(self.tcx)? == 0)
|
||||
} else {
|
||||
span_bug!(e.span, "typeck error");
|
||||
}
|
||||
|
|
|
@ -989,7 +989,7 @@ pub fn approx_ty_size<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> u64 {
|
|||
(Ok(size), _) => size,
|
||||
(Err(_), ty::Tuple(list)) => list.iter().map(|t| approx_ty_size(cx, t)).sum(),
|
||||
(Err(_), ty::Array(t, n)) => {
|
||||
n.try_eval_target_usize(cx.tcx, cx.param_env).unwrap_or_default() * approx_ty_size(cx, *t)
|
||||
n.try_to_target_usize(cx.tcx).unwrap_or_default() * approx_ty_size(cx, *t)
|
||||
},
|
||||
(Err(_), ty::Adt(def, subst)) if def.is_struct() => def
|
||||
.variants()
|
||||
|
@ -1207,7 +1207,7 @@ impl<'tcx> InteriorMut<'tcx> {
|
|||
let chain = match *ty.kind() {
|
||||
ty::RawPtr(inner_ty, _) if !self.ignore_pointers => self.interior_mut_ty_chain(cx, inner_ty),
|
||||
ty::Ref(_, inner_ty, _) | ty::Slice(inner_ty) => self.interior_mut_ty_chain(cx, inner_ty),
|
||||
ty::Array(inner_ty, size) if size.try_eval_target_usize(cx.tcx, cx.param_env) != Some(0) => {
|
||||
ty::Array(inner_ty, size) if size.try_to_target_usize(cx.tcx) != Some(0) => {
|
||||
self.interior_mut_ty_chain(cx, inner_ty)
|
||||
},
|
||||
ty::Tuple(fields) => fields.iter().find_map(|ty| self.interior_mut_ty_chain(cx, ty)),
|
||||
|
|
Loading…
Reference in a new issue