Skip to content

Commit c5d3167

Browse files
committed
update testsuite and expand if_chain
1 parent a3420f7 commit c5d3167

File tree

2 files changed

+63
-24
lines changed

2 files changed

+63
-24
lines changed

clippy_lints/src/trailing_zero_sized_array_without_repr_c.rs

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -64,35 +64,42 @@ impl<'tcx> LateLintPass<'tcx> for TrailingZeroSizedArrayWithoutReprC {
6464
}
6565

6666
fn is_struct_with_trailing_zero_sized_array(cx: &LateContext<'tcx>, item: &'tcx Item<'tcx>) -> bool {
67-
if_chain! {
68-
// First check if last field is an array
69-
if let ItemKind::Struct(data, _) = &item.kind;
70-
if let VariantData::Struct(field_defs, _) = data;
71-
if let Some(last_field) = field_defs.last();
72-
if let rustc_hir::TyKind::Array(_, length) = last_field.ty.kind;
67+
// First check if last field is an array
68+
if let ItemKind::Struct(data, _) = &item.kind {
69+
if let VariantData::Struct(field_defs, _) = data {
70+
if let Some(last_field) = field_defs.last() {
71+
if let rustc_hir::TyKind::Array(_, length) = last_field.ty.kind {
72+
// Then check if that that array zero-sized
7373

74-
// Then check if that that array zero-sized
74+
// This is pretty much copied from `enum_clike.rs` and I don't fully understand it, so let me know
75+
// if there's a better way. I tried `Const::from_anon_const` but it didn't fold in the values
76+
// on the `ZeroSizedWithConst` and `ZeroSizedWithConstFunction` tests.
7577

76-
// This is pretty much copied from `enum_clike.rs` and I don't fully understand it, so let me know
77-
// if there's a better way. I tried `Const::from_anon_const` but it didn't fold in the values
78-
// on the `ZeroSizedWithConst` and `ZeroSizedWithConstFunction` tests.
79-
80-
// This line in particular seems convoluted.
81-
let length_did = cx.tcx.hir().body_owner_def_id(length.body).to_def_id();
82-
let length_ty = cx.tcx.type_of(length_did);
83-
let length = cx
84-
.tcx
85-
.const_eval_poly(length_did)
86-
.ok()
87-
.map(|val| Const::from_value(cx.tcx, val, length_ty))
88-
.and_then(miri_to_const);
89-
if let Some(Constant::Int(length)) = length;
90-
if length == 0;
91-
then {
92-
true
78+
// This line in particular seems convoluted.
79+
let length_did = cx.tcx.hir().body_owner_def_id(length.body).to_def_id();
80+
let length_ty = cx.tcx.type_of(length_did);
81+
let length = cx
82+
.tcx
83+
.const_eval_poly(length_did)
84+
.ok()
85+
.map(|val| Const::from_value(cx.tcx, val, length_ty))
86+
.and_then(miri_to_const);
87+
if let Some(Constant::Int(length)) = length {
88+
length == 0
89+
} else {
90+
false
91+
}
92+
} else {
93+
false
94+
}
95+
} else {
96+
false
97+
}
9398
} else {
9499
false
95100
}
101+
} else {
102+
false
96103
}
97104
}
98105

tests/ui/trailing_zero_sized_array_without_repr_c.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#![warn(clippy::trailing_zero_sized_array_without_repr_c)]
2+
#![feature(const_generics_defaults)]
23

34
// Do lint:
45

@@ -45,6 +46,10 @@ struct ZeroSizedWithConstFunction {
4546
last: [usize; compute_zero()],
4647
}
4748

49+
struct ZeroSizedArrayWrapper([usize; 0]);
50+
51+
struct TupleStruct(i32, [usize; 0]);
52+
4853
struct LotsOfFields {
4954
f1: u32,
5055
f2: u32,
@@ -140,4 +145,31 @@ enum DontLintAnonymousStructsFromDesuraging {
140145
C { x: u32, y: [u64; 0] },
141146
}
142147

148+
#[repr(C)]
149+
struct TupleStructReprC(i32, [usize; 0]);
150+
151+
type NamedTuple = (i32, [usize; 0]);
152+
153+
#[rustfmt::skip] // [rustfmt#4995](https://github.com/rust-lang/rustfmt/issues/4995)
154+
struct ConstParamZeroDefault<const N: usize = 0> {
155+
field: i32,
156+
last: [usize; N],
157+
}
158+
159+
struct ConstParamNoDefault<const N: usize> {
160+
field: i32,
161+
last: [usize; N],
162+
}
163+
164+
#[rustfmt::skip]
165+
struct ConstParamNonZeroDefault<const N: usize = 1> {
166+
field: i32,
167+
last: [usize; N],
168+
}
169+
170+
type A = ConstParamZeroDefault;
171+
type B = ConstParamZeroDefault<0>;
172+
type C = ConstParamNoDefault<0>;
173+
type D = ConstParamNonZeroDefault<0>;
174+
143175
fn main() {}

0 commit comments

Comments
 (0)