Skip to content

Commit 2a5a4f0

Browse files
committed
Refactor ZS array detection again and this one seems great 👍
1 parent c5d3167 commit 2a5a4f0

File tree

1 file changed

+9
-17
lines changed

1 file changed

+9
-17
lines changed

clippy_lints/src/trailing_zero_sized_array_without_repr_c.rs

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use clippy_utils::consts::{miri_to_const, Constant};
1+
use clippy_utils::consts::{constant, miri_to_const, ConstEvalLateContext, Constant};
22
use clippy_utils::diagnostics::span_lint_and_help;
33
use rustc_ast::Attribute;
44
use rustc_hir::{Item, ItemKind, VariantData};
@@ -38,9 +38,11 @@ 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);
4142
if is_struct_with_trailing_zero_sized_array(cx, item) {
4243
// NOTE: This is to include attributes on the definition when we print the lint. If the convention
43-
// is to not do that with struct definitions (I'm not sure), then this isn't necessary.
44+
// is to not do that with struct definitions (I'm not sure), then this isn't necessary. (note: if
45+
// you don't get rid of this, change `has_repr_attr` to `includes_repr_attr`).
4446
let attrs = cx.tcx.get_attrs(item.def_id.to_def_id());
4547
let first_attr = attrs.iter().min_by_key(|attr| attr.span.lo());
4648
let lint_span = if let Some(first_attr) = first_attr {
@@ -70,21 +72,11 @@ fn is_struct_with_trailing_zero_sized_array(cx: &LateContext<'tcx>, item: &'tcx
7072
if let Some(last_field) = field_defs.last() {
7173
if let rustc_hir::TyKind::Array(_, length) = last_field.ty.kind {
7274
// Then check if that that array zero-sized
73-
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.
77-
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 {
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 {
8880
length == 0
8981
} else {
9082
false

0 commit comments

Comments
 (0)