From 4b4db597722ff561515812e2e0cab255a678a41c Mon Sep 17 00:00:00 2001 From: Nathaniel Hamovitz <18648574+nhamovitz@users.noreply.github.com> Date: Sat, 16 Oct 2021 02:01:17 -0700 Subject: [PATCH] output looks fantastic --- ...railing_zero_sized_array_without_repr_c.rs | 34 ++--- ...railing_zero_sized_array_without_repr_c.rs | 139 +++++++++++------- 2 files changed, 100 insertions(+), 73 deletions(-) diff --git a/clippy_lints/src/trailing_zero_sized_array_without_repr_c.rs b/clippy_lints/src/trailing_zero_sized_array_without_repr_c.rs index 62f8ecdc2163..7eeb2914c400 100644 --- a/clippy_lints/src/trailing_zero_sized_array_without_repr_c.rs +++ b/clippy_lints/src/trailing_zero_sized_array_without_repr_c.rs @@ -1,8 +1,9 @@ -use clippy_utils::diagnostics::span_lint_and_sugg; -// use clippy_utils::is_integer_const; use clippy_utils::consts::{miri_to_const, Constant}; +use clippy_utils::diagnostics::span_lint_and_sugg; +use clippy_utils::source::snippet; use rustc_errors::Applicability; -use rustc_hir::{HirId, Item, ItemKind, TyKind, VariantData}; +use rustc_hir::def_id::LocalDefId; +use rustc_hir::{Item, ItemKind, TyKind, VariantData}; use rustc_lint::{LateContext, LateLintPass}; use rustc_session::{declare_lint_pass, declare_tool_lint}; use rustc_span::sym; @@ -46,26 +47,14 @@ impl<'tcx> LateLintPass<'tcx> for TrailingZeroSizedArrayWithoutReprC { fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'tcx>) { dbg!(item.ident); - let hir_id = cx.tcx.hir().local_def_id_to_hir_id(item.def_id); - let hir_id2 = item.hir_id(); - dbg!(hir_id); - dbg!(hir_id2); - dbg!(hir_id == hir_id2); - - let span1 = cx.tcx.hir().span(hir_id); - let span2 = item.span; - dbg!(span1); - dbg!(span2); - dbg!(span1 == span2); - - if is_struct_with_trailing_zero_sized_array(cx, item) && !has_repr_c(cx, hir_id) { + if is_struct_with_trailing_zero_sized_array(cx, item) && !has_repr_c(cx, item.def_id) { span_lint_and_sugg( cx, TRAILING_ZERO_SIZED_ARRAY_WITHOUT_REPR_C, - span2, - "trailing zero-sized array in a struct which isn't marked `#[repr(C)]`", + item.span, + "trailing zero-sized array in a struct which is not marked `#[repr(C)]`", "try", - "#[repr(C)]".to_string(), + format!("#[repr(C)]\n{}", snippet(cx, item.span, "..")), Applicability::MaybeIncorrect, ); } @@ -101,13 +90,14 @@ fn is_struct_with_trailing_zero_sized_array(cx: &LateContext<'tcx>, item: &'tcx false } -fn has_repr_c(cx: &LateContext<'tcx>, hir_id: HirId) -> bool { +fn has_repr_c(cx: &LateContext<'tcx>, def_id: LocalDefId) -> bool { + let hir_id = cx.tcx.hir().local_def_id_to_hir_id(def_id); let attrs = cx.tcx.hir().attrs(hir_id); + // NOTE: Can there ever be more than one `repr` attribute? // other `repr` syms: repr, repr128, repr_align, repr_align_enum, repr_no_niche, repr_packed, // repr_simd, repr_transparent - - if let Some(repr_attr) = attrs.iter().find(|attr| attr.has_name(sym::repr)) { + if let Some(_repr_attr) = attrs.iter().find(|attr| attr.has_name(sym::repr)) { // eprintln!("repr: true"); true } else { diff --git a/tests/ui/trailing_zero_sized_array_without_repr_c.rs b/tests/ui/trailing_zero_sized_array_without_repr_c.rs index 1e3683b2c259..311193fb4a1b 100644 --- a/tests/ui/trailing_zero_sized_array_without_repr_c.rs +++ b/tests/ui/trailing_zero_sized_array_without_repr_c.rs @@ -1,5 +1,7 @@ #![warn(clippy::trailing_zero_sized_array_without_repr_c)] +// #![feature(const_generics_defaults)] + struct RarelyUseful { field: i32, last: [usize; 0], @@ -15,62 +17,97 @@ struct OnlyFieldIsZeroSizeArray { first_and_last: [usize; 0], } -// struct GenericArrayType { -// field: i32, -// last: [T; 0], -// } +struct GenericArrayType { + field: i32, + last: [T; 0], +} -// struct SizedArray { -// field: i32, -// last: [usize; 1], -// } +struct SizedArray { + field: i32, + last: [usize; 1], +} -// const ZERO: usize = 0; -// struct ZeroSizedFromExternalConst { -// field: i32, -// last: [usize; ZERO], -// } +const ZERO: usize = 0; +struct ZeroSizedFromExternalConst { + field: i32, + last: [usize; ZERO], +} -// const ONE: usize = 1; -// struct NonZeroSizedFromExternalConst { -// field: i32, -// last: [usize; ONE], -// } +const ONE: usize = 1; +struct NonZeroSizedFromExternalConst { + field: i32, + last: [usize; ONE], +} -// #[allow(clippy::eq_op)] // lmao im impressed -// const fn compute_zero() -> usize { -// (4 + 6) - (2 * 5) -// } -// struct UsingFunction { -// field: i32, -// last: [usize; compute_zero()], -// } - -// // TODO: same -// #[repr(packed)] -// struct ReprPacked { -// small: u8, -// medium: i32, -// weird: [u64; 0], -// } - -// // TODO: actually, uh,, -// #[repr(align(64))] -// struct ReprAlign { -// field: i32, -// last: [usize; 0], -// } -// #[repr(C, align(64))] -// struct ReprCAlign { -// field: i32, -// last: [usize; 0], -// } +#[allow(clippy::eq_op)] // lmao im impressed +const fn compute_zero() -> usize { + (4 + 6) - (2 * 5) +} +struct UsingFunction { + field: i32, + last: [usize; compute_zero()], +} // #[repr(C)] -// enum DontLintAnonymousStructsFromDesuraging { -// A(u32), -// B(f32, [u64; 0]), -// C { x: u32, y: [u64; 0] }, +// struct ConstParamOk { +// field: i32, +// last: [usize; N] // } -fn main() {} +// struct ConstParamLint { +// field: i32, +// last: [usize; N] +// } + + +// TODO: actually, uh,, +#[repr(packed)] +struct ReprPacked { + small: u8, + medium: i32, + weird: [u64; 0], +} + +// same +#[repr(align(64))] +struct ReprAlign { + field: i32, + last: [usize; 0], +} + +// same +#[repr(C, align(64))] +struct ReprCAlign { + field: i32, + last: [usize; 0], +} + +#[repr(C)] +enum DontLintAnonymousStructsFromDesuraging { + A(u32), + B(f32, [u64; 0]), + C { x: u32, y: [u64; 0] }, +} + +struct LotsOfFields { + f1: u32, + f2: u32, + f3: u32, + f4: u32, + f5: u32, + f6: u32, + f7: u32, + f8: u32, + f9: u32, + f10: u32, + f11: u32, + f12: u32, + f13: u32, + f14: u32, + f15: u32, + f16: u32, + last: [usize; 0], +} + +fn main() { +}