Skip to content

Commit 9f402b3

Browse files
committed
Check for tuple structs
1 parent 2a5a4f0 commit 9f402b3

File tree

1 file changed

+10
-17
lines changed

1 file changed

+10
-17
lines changed

clippy_lints/src/trailing_zero_sized_array_without_repr_c.rs

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ declare_lint_pass!(TrailingZeroSizedArrayWithoutReprC => [TRAILING_ZERO_SIZED_AR
3838

3939
impl<'tcx> LateLintPass<'tcx> for TrailingZeroSizedArrayWithoutReprC {
4040
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'tcx>) {
41-
dbg!(item.ident);
4241
if is_struct_with_trailing_zero_sized_array(cx, item) {
4342
// NOTE: This is to include attributes on the definition when we print the lint. If the convention
4443
// is to not do that with struct definitions (I'm not sure), then this isn't necessary. (note: if
@@ -66,24 +65,18 @@ impl<'tcx> LateLintPass<'tcx> for TrailingZeroSizedArrayWithoutReprC {
6665
}
6766

6867
fn is_struct_with_trailing_zero_sized_array(cx: &LateContext<'tcx>, item: &'tcx Item<'tcx>) -> bool {
68+
// TODO: when finalized, replace with an `if_chain`. I have it like this because my rust-analyzer doesn't work when it's an `if_chain`
6969
// First check if last field is an array
7070
if let ItemKind::Struct(data, _) = &item.kind {
71-
if let VariantData::Struct(field_defs, _) = data {
72-
if let Some(last_field) = field_defs.last() {
73-
if let rustc_hir::TyKind::Array(_, length) = last_field.ty.kind {
74-
// Then check if that that array zero-sized
75-
let length_ldid = cx.tcx.hir().local_def_id(length.hir_id);
76-
let length = Const::from_anon_const(cx.tcx, length_ldid);
77-
let length = length.try_eval_usize(cx.tcx, cx.param_env);
78-
// if let Some((Constant::Int(length), _)) = length {
79-
if let Some(length) = length {
80-
length == 0
81-
} else {
82-
false
83-
}
84-
} else {
85-
false
86-
}
71+
let field_defs = data.fields();
72+
if let Some(last_field) = field_defs.last() {
73+
if let rustc_hir::TyKind::Array(_, length) = last_field.ty.kind {
74+
// Then check if that that array zero-sized
75+
let length_ldid = cx.tcx.hir().local_def_id(length.hir_id);
76+
let length = Const::from_anon_const(cx.tcx, length_ldid);
77+
let length = length.try_eval_usize(cx.tcx, cx.param_env);
78+
// if let Some((Constant::Int(length), _)) = length {
79+
if let Some(length) = length { length == 0 } else { false }
8780
} else {
8881
false
8982
}

0 commit comments

Comments
 (0)