Skip to content

Commit c69387a

Browse files
committed
Well it builds
1 parent 797507c commit c69387a

File tree

2 files changed

+41
-12
lines changed

2 files changed

+41
-12
lines changed

clippy_lints/src/trailing_zero_sized_array_without_repr_c.rs

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
22
use rustc_hir::*;
3+
use rustc_lint::{EarlyContext, EarlyLintPass};
34
use rustc_lint::{LateContext, LateLintPass};
45
use rustc_session::{declare_lint_pass, declare_tool_lint};
56

@@ -33,26 +34,54 @@ declare_clippy_lint! {
3334
declare_lint_pass!(TrailingZeroSizedArrayWithoutReprC => [TRAILING_ZERO_SIZED_ARRAY_WITHOUT_REPR_C]);
3435

3536
impl LateLintPass<'_> for TrailingZeroSizedArrayWithoutReprC {
36-
fn check_struct_def(&mut self, _: &LateContext<'tcx>, _: &'tcx rustc_hir::VariantData<'tcx>) {}
37+
fn check_struct_def(&mut self, cx: &LateContext<'tcx>, data: &'tcx rustc_hir::VariantData<'tcx>) {
38+
dbg!("in check_struct_def");
39+
if_chain! {
40+
if let Some(def) = data.fields().last();
41+
if let rustc_hir::TyKind::Array(ty, acost) = def.ty.kind;
42+
then {
43+
// is the AnonConst `0`
44+
}
45+
}
3746

38-
fn check_struct_def_post(&mut self, _: &LateContext<'tcx>, _: &'tcx rustc_hir::VariantData<'tcx>) {}
47+
// span_lint_and_sugg(
48+
// cx,
49+
// todo!(),
50+
// todo!(),
51+
// todo!(),
52+
// todo!(),
53+
// todo!(),
54+
// rustc_errors::Applicability::MaybeIncorrect,
55+
// )
56+
}
3957
// https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/sty/enum.TyKind.html#variant.Array in latepass
4058
// or https://doc.rust-lang.org/nightly/nightly-rustc/rustc_ast/ast/enum.TyKind.html#variant.Array in early pass
4159

42-
fn check_field_def(&mut self, _: &LateContext<'tcx>, _: &'tcx rustc_hir::FieldDef<'tcx>) {}
60+
// fn check_struct_def_post(&mut self, _: &LateContext<'tcx>, _: &'tcx rustc_hir::VariantData<'tcx>)
61+
// {}
4362

44-
fn check_attribute(&mut self, _: &LateContext<'tcx>, _: &'tcx rustc_ast::Attribute) {}
63+
// fn check_field_def(&mut self, _: &LateContext<'tcx>, _: &'tcx rustc_hir::FieldDef<'tcx>) {}
4564

46-
fn enter_lint_attrs(&mut self, _: &LateContext<'tcx>, _: &'tcx [rustc_ast::Attribute]) {}
65+
// fn check_attribute(&mut self, _: &LateContext<'tcx>, _: &'tcx rustc_ast::Attribute) {}
4766

48-
fn exit_lint_attrs(&mut self, _: &LateContext<'tcx>, _: &'tcx [rustc_ast::Attribute]) {}
67+
// fn enter_lint_attrs(&mut self, _: &LateContext<'tcx>, _: &'tcx [rustc_ast::Attribute]) {}
68+
69+
// fn exit_lint_attrs(&mut self, _: &LateContext<'tcx>, _: &'tcx [rustc_ast::Attribute]) {}
4970
}
5071
//
5172
// TODO: Register the lint pass in `clippy_lints/src/lib.rs`,
5273
// e.g. store.register_late_pass(||
5374
// Box::new(trailing_zero_sized_array_without_repr_c::TrailingZeroSizedArrayWithoutReprC));
5475

76+
// fn temp_alert() {}
5577

56-
fn temp_alert() {
57-
span_lint_and_sugg(cx, lint, sp, msg, help, sugg, applicability)
58-
}
78+
impl EarlyLintPass for TrailingZeroSizedArrayWithoutReprC {
79+
fn check_struct_def(&mut self, cx: &EarlyContext<'_>, data: &rustc_ast::VariantData) {
80+
if_chain! {
81+
if let rustc_ast::ast::VariantData::Struct(field_defs, some_bool_huh) = data;
82+
if let Some(last_field) = field_defs.last();
83+
if let rustc_ast::ast::TyKind::Array(_, aconst) = &last_field.ty.kind;
84+
then {dbg!(aconst); return ();}
85+
}
86+
}
87+
}

tests/ui/trailing_zero_sized_array_without_repr_c.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22

33
struct RarelyUseful {
44
field: i32,
5-
last: [SomeType; 0],
5+
last: [usize; 0],
66
}
77

88
#[repr(C)]
99
struct GoodReason {
1010
field: i32,
11-
last: [SomeType; 0],
11+
last: [usize; 0],
1212
}
1313

1414
struct OnlyFieldIsZeroSizeArray {
15-
first_and_last: [SomeType; 0],
15+
first_and_last: [usize; 0],
1616
}
1717

1818
struct GenericArrayType<T> {

0 commit comments

Comments
 (0)