output looks fantastic

This commit is contained in:
Nathaniel Hamovitz 2021-10-16 02:01:17 -07:00
parent e53a4da4a1
commit 4b4db59772
2 changed files with 100 additions and 73 deletions

View File

@ -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 {

View File

@ -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<T> {
// field: i32,
// last: [T; 0],
// }
struct GenericArrayType<T> {
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<const N: usize = 0> {
// field: i32,
// last: [usize; N]
// }
fn main() {}
// struct ConstParamLint<const N: usize = 0> {
// 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() {
}