|
1 |
| -use clippy_utils::consts::{miri_to_const, Constant}; |
| 1 | +use clippy_utils::consts::{constant, miri_to_const, ConstEvalLateContext, Constant}; |
2 | 2 | use clippy_utils::diagnostics::span_lint_and_help;
|
3 | 3 | use rustc_ast::Attribute;
|
4 | 4 | use rustc_hir::{Item, ItemKind, VariantData};
|
@@ -38,9 +38,11 @@ declare_lint_pass!(TrailingZeroSizedArrayWithoutReprC => [TRAILING_ZERO_SIZED_AR
|
38 | 38 |
|
39 | 39 | impl<'tcx> LateLintPass<'tcx> for TrailingZeroSizedArrayWithoutReprC {
|
40 | 40 | fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'tcx>) {
|
| 41 | + dbg!(item.ident); |
41 | 42 | if is_struct_with_trailing_zero_sized_array(cx, item) {
|
42 | 43 | // 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`). |
44 | 46 | let attrs = cx.tcx.get_attrs(item.def_id.to_def_id());
|
45 | 47 | let first_attr = attrs.iter().min_by_key(|attr| attr.span.lo());
|
46 | 48 | 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
|
70 | 72 | if let Some(last_field) = field_defs.last() {
|
71 | 73 | if let rustc_hir::TyKind::Array(_, length) = last_field.ty.kind {
|
72 | 74 | // 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 { |
88 | 80 | length == 0
|
89 | 81 | } else {
|
90 | 82 | false
|
|
0 commit comments