update testsuite and expand if_chain

This commit is contained in:
Nathaniel Hamovitz 2021-10-17 17:28:45 -07:00
parent a3420f7004
commit c5d3167a23
2 changed files with 63 additions and 24 deletions

View File

@ -64,35 +64,42 @@ impl<'tcx> LateLintPass<'tcx> for TrailingZeroSizedArrayWithoutReprC {
}
fn is_struct_with_trailing_zero_sized_array(cx: &LateContext<'tcx>, item: &'tcx Item<'tcx>) -> bool {
if_chain! {
// First check if last field is an array
if let ItemKind::Struct(data, _) = &item.kind;
if let VariantData::Struct(field_defs, _) = data;
if let Some(last_field) = field_defs.last();
if let rustc_hir::TyKind::Array(_, length) = last_field.ty.kind;
// First check if last field is an array
if let ItemKind::Struct(data, _) = &item.kind {
if let VariantData::Struct(field_defs, _) = data {
if let Some(last_field) = field_defs.last() {
if let rustc_hir::TyKind::Array(_, length) = last_field.ty.kind {
// Then check if that that array zero-sized
// Then check if that that array zero-sized
// This is pretty much copied from `enum_clike.rs` and I don't fully understand it, so let me know
// if there's a better way. I tried `Const::from_anon_const` but it didn't fold in the values
// on the `ZeroSizedWithConst` and `ZeroSizedWithConstFunction` tests.
// This is pretty much copied from `enum_clike.rs` and I don't fully understand it, so let me know
// if there's a better way. I tried `Const::from_anon_const` but it didn't fold in the values
// on the `ZeroSizedWithConst` and `ZeroSizedWithConstFunction` tests.
// This line in particular seems convoluted.
let length_did = cx.tcx.hir().body_owner_def_id(length.body).to_def_id();
let length_ty = cx.tcx.type_of(length_did);
let length = cx
.tcx
.const_eval_poly(length_did)
.ok()
.map(|val| Const::from_value(cx.tcx, val, length_ty))
.and_then(miri_to_const);
if let Some(Constant::Int(length)) = length;
if length == 0;
then {
true
// This line in particular seems convoluted.
let length_did = cx.tcx.hir().body_owner_def_id(length.body).to_def_id();
let length_ty = cx.tcx.type_of(length_did);
let length = cx
.tcx
.const_eval_poly(length_did)
.ok()
.map(|val| Const::from_value(cx.tcx, val, length_ty))
.and_then(miri_to_const);
if let Some(Constant::Int(length)) = length {
length == 0
} else {
false
}
} else {
false
}
} else {
false
}
} else {
false
}
} else {
false
}
}

View File

@ -1,4 +1,5 @@
#![warn(clippy::trailing_zero_sized_array_without_repr_c)]
#![feature(const_generics_defaults)]
// Do lint:
@ -45,6 +46,10 @@ struct ZeroSizedWithConstFunction {
last: [usize; compute_zero()],
}
struct ZeroSizedArrayWrapper([usize; 0]);
struct TupleStruct(i32, [usize; 0]);
struct LotsOfFields {
f1: u32,
f2: u32,
@ -140,4 +145,31 @@ enum DontLintAnonymousStructsFromDesuraging {
C { x: u32, y: [u64; 0] },
}
#[repr(C)]
struct TupleStructReprC(i32, [usize; 0]);
type NamedTuple = (i32, [usize; 0]);
#[rustfmt::skip] // [rustfmt#4995](https://github.com/rust-lang/rustfmt/issues/4995)
struct ConstParamZeroDefault<const N: usize = 0> {
field: i32,
last: [usize; N],
}
struct ConstParamNoDefault<const N: usize> {
field: i32,
last: [usize; N],
}
#[rustfmt::skip]
struct ConstParamNonZeroDefault<const N: usize = 1> {
field: i32,
last: [usize; N],
}
type A = ConstParamZeroDefault;
type B = ConstParamZeroDefault<0>;
type C = ConstParamNoDefault<0>;
type D = ConstParamNonZeroDefault<0>;
fn main() {}