Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 4b4db59

Browse files
committed
output looks fantastic
1 parent e53a4da commit 4b4db59

File tree

2 files changed

+96
-69
lines changed

2 files changed

+96
-69
lines changed

clippy_lints/src/trailing_zero_sized_array_without_repr_c.rs

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
use clippy_utils::diagnostics::span_lint_and_sugg;
2-
// use clippy_utils::is_integer_const;
31
use clippy_utils::consts::{miri_to_const, Constant};
2+
use clippy_utils::diagnostics::span_lint_and_sugg;
3+
use clippy_utils::source::snippet;
44
use rustc_errors::Applicability;
5-
use rustc_hir::{HirId, Item, ItemKind, TyKind, VariantData};
5+
use rustc_hir::def_id::LocalDefId;
6+
use rustc_hir::{Item, ItemKind, TyKind, VariantData};
67
use rustc_lint::{LateContext, LateLintPass};
78
use rustc_session::{declare_lint_pass, declare_tool_lint};
89
use rustc_span::sym;
@@ -46,26 +47,14 @@ impl<'tcx> LateLintPass<'tcx> for TrailingZeroSizedArrayWithoutReprC {
4647
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'tcx>) {
4748
dbg!(item.ident);
4849

49-
let hir_id = cx.tcx.hir().local_def_id_to_hir_id(item.def_id);
50-
let hir_id2 = item.hir_id();
51-
dbg!(hir_id);
52-
dbg!(hir_id2);
53-
dbg!(hir_id == hir_id2);
54-
55-
let span1 = cx.tcx.hir().span(hir_id);
56-
let span2 = item.span;
57-
dbg!(span1);
58-
dbg!(span2);
59-
dbg!(span1 == span2);
60-
61-
if is_struct_with_trailing_zero_sized_array(cx, item) && !has_repr_c(cx, hir_id) {
50+
if is_struct_with_trailing_zero_sized_array(cx, item) && !has_repr_c(cx, item.def_id) {
6251
span_lint_and_sugg(
6352
cx,
6453
TRAILING_ZERO_SIZED_ARRAY_WITHOUT_REPR_C,
65-
span2,
66-
"trailing zero-sized array in a struct which isn't marked `#[repr(C)]`",
54+
item.span,
55+
"trailing zero-sized array in a struct which is not marked `#[repr(C)]`",
6756
"try",
68-
"#[repr(C)]".to_string(),
57+
format!("#[repr(C)]\n{}", snippet(cx, item.span, "..")),
6958
Applicability::MaybeIncorrect,
7059
);
7160
}
@@ -101,13 +90,14 @@ fn is_struct_with_trailing_zero_sized_array(cx: &LateContext<'tcx>, item: &'tcx
10190
false
10291
}
10392

104-
fn has_repr_c(cx: &LateContext<'tcx>, hir_id: HirId) -> bool {
93+
fn has_repr_c(cx: &LateContext<'tcx>, def_id: LocalDefId) -> bool {
94+
let hir_id = cx.tcx.hir().local_def_id_to_hir_id(def_id);
10595
let attrs = cx.tcx.hir().attrs(hir_id);
96+
10697
// NOTE: Can there ever be more than one `repr` attribute?
10798
// other `repr` syms: repr, repr128, repr_align, repr_align_enum, repr_no_niche, repr_packed,
10899
// repr_simd, repr_transparent
109-
110-
if let Some(repr_attr) = attrs.iter().find(|attr| attr.has_name(sym::repr)) {
100+
if let Some(_repr_attr) = attrs.iter().find(|attr| attr.has_name(sym::repr)) {
111101
// eprintln!("repr: true");
112102
true
113103
} else {
Lines changed: 84 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#![warn(clippy::trailing_zero_sized_array_without_repr_c)]
22

3+
// #![feature(const_generics_defaults)]
4+
35
struct RarelyUseful {
46
field: i32,
57
last: [usize; 0],
@@ -15,62 +17,97 @@ struct OnlyFieldIsZeroSizeArray {
1517
first_and_last: [usize; 0],
1618
}
1719

18-
// struct GenericArrayType<T> {
19-
// field: i32,
20-
// last: [T; 0],
21-
// }
20+
struct GenericArrayType<T> {
21+
field: i32,
22+
last: [T; 0],
23+
}
2224

23-
// struct SizedArray {
24-
// field: i32,
25-
// last: [usize; 1],
26-
// }
25+
struct SizedArray {
26+
field: i32,
27+
last: [usize; 1],
28+
}
2729

28-
// const ZERO: usize = 0;
29-
// struct ZeroSizedFromExternalConst {
30-
// field: i32,
31-
// last: [usize; ZERO],
32-
// }
30+
const ZERO: usize = 0;
31+
struct ZeroSizedFromExternalConst {
32+
field: i32,
33+
last: [usize; ZERO],
34+
}
35+
36+
const ONE: usize = 1;
37+
struct NonZeroSizedFromExternalConst {
38+
field: i32,
39+
last: [usize; ONE],
40+
}
3341

34-
// const ONE: usize = 1;
35-
// struct NonZeroSizedFromExternalConst {
42+
#[allow(clippy::eq_op)] // lmao im impressed
43+
const fn compute_zero() -> usize {
44+
(4 + 6) - (2 * 5)
45+
}
46+
struct UsingFunction {
47+
field: i32,
48+
last: [usize; compute_zero()],
49+
}
50+
51+
// #[repr(C)]
52+
// struct ConstParamOk<const N: usize = 0> {
3653
// field: i32,
37-
// last: [usize; ONE],
54+
// last: [usize; N]
3855
// }
3956

40-
// #[allow(clippy::eq_op)] // lmao im impressed
41-
// const fn compute_zero() -> usize {
42-
// (4 + 6) - (2 * 5)
43-
// }
44-
// struct UsingFunction {
57+
// struct ConstParamLint<const N: usize = 0> {
4558
// field: i32,
46-
// last: [usize; compute_zero()],
59+
// last: [usize; N]
4760
// }
4861

49-
// // TODO: same
50-
// #[repr(packed)]
51-
// struct ReprPacked {
52-
// small: u8,
53-
// medium: i32,
54-
// weird: [u64; 0],
55-
// }
5662

57-
// // TODO: actually, uh,,
58-
// #[repr(align(64))]
59-
// struct ReprAlign {
60-
// field: i32,
61-
// last: [usize; 0],
62-
// }
63-
// #[repr(C, align(64))]
64-
// struct ReprCAlign {
65-
// field: i32,
66-
// last: [usize; 0],
67-
// }
63+
// TODO: actually, uh,,
64+
#[repr(packed)]
65+
struct ReprPacked {
66+
small: u8,
67+
medium: i32,
68+
weird: [u64; 0],
69+
}
6870

69-
// #[repr(C)]
70-
// enum DontLintAnonymousStructsFromDesuraging {
71-
// A(u32),
72-
// B(f32, [u64; 0]),
73-
// C { x: u32, y: [u64; 0] },
74-
// }
71+
// same
72+
#[repr(align(64))]
73+
struct ReprAlign {
74+
field: i32,
75+
last: [usize; 0],
76+
}
7577

76-
fn main() {}
78+
// same
79+
#[repr(C, align(64))]
80+
struct ReprCAlign {
81+
field: i32,
82+
last: [usize; 0],
83+
}
84+
85+
#[repr(C)]
86+
enum DontLintAnonymousStructsFromDesuraging {
87+
A(u32),
88+
B(f32, [u64; 0]),
89+
C { x: u32, y: [u64; 0] },
90+
}
91+
92+
struct LotsOfFields {
93+
f1: u32,
94+
f2: u32,
95+
f3: u32,
96+
f4: u32,
97+
f5: u32,
98+
f6: u32,
99+
f7: u32,
100+
f8: u32,
101+
f9: u32,
102+
f10: u32,
103+
f11: u32,
104+
f12: u32,
105+
f13: u32,
106+
f14: u32,
107+
f15: u32,
108+
f16: u32,
109+
last: [usize; 0],
110+
}
111+
112+
fn main() {
113+
}

0 commit comments

Comments
 (0)