|
1 | 1 | use clippy_utils::diagnostics::span_lint_and_sugg;
|
2 |
| -use rustc_hir::*; |
3 | 2 | use rustc_lint::{EarlyContext, EarlyLintPass};
|
4 |
| -use rustc_lint::{LateContext, LateLintPass}; |
| 3 | +use rustc_lint_defs::Applicability; |
5 | 4 | use rustc_session::{declare_lint_pass, declare_tool_lint};
|
6 | 5 |
|
7 | 6 | declare_clippy_lint! {
|
@@ -33,55 +32,42 @@ declare_clippy_lint! {
|
33 | 32 | }
|
34 | 33 | declare_lint_pass!(TrailingZeroSizedArrayWithoutReprC => [TRAILING_ZERO_SIZED_ARRAY_WITHOUT_REPR_C]);
|
35 | 34 |
|
36 |
| -impl LateLintPass<'_> for TrailingZeroSizedArrayWithoutReprC { |
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 |
| - } |
46 |
| - |
47 |
| - // span_lint_and_sugg( |
48 |
| - // cx, |
49 |
| - // todo!(), |
50 |
| - // todo!(), |
51 |
| - // todo!(), |
52 |
| - // todo!(), |
53 |
| - // todo!(), |
54 |
| - // rustc_errors::Applicability::MaybeIncorrect, |
55 |
| - // ) |
56 |
| - } |
57 |
| - // https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/sty/enum.TyKind.html#variant.Array in latepass |
58 |
| - // or https://doc.rust-lang.org/nightly/nightly-rustc/rustc_ast/ast/enum.TyKind.html#variant.Array in early pass |
59 |
| - |
60 |
| - // fn check_struct_def_post(&mut self, _: &LateContext<'tcx>, _: &'tcx rustc_hir::VariantData<'tcx>) |
61 |
| - // {} |
62 |
| - |
63 |
| - // fn check_field_def(&mut self, _: &LateContext<'tcx>, _: &'tcx rustc_hir::FieldDef<'tcx>) {} |
64 |
| - |
65 |
| - // fn check_attribute(&mut self, _: &LateContext<'tcx>, _: &'tcx rustc_ast::Attribute) {} |
66 |
| - |
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]) {} |
70 |
| -} |
71 | 35 | //
|
72 | 36 | // TODO: Register the lint pass in `clippy_lints/src/lib.rs`,
|
73 |
| -// e.g. store.register_late_pass(|| |
| 37 | +// e.g. store.register_early_pass(|| |
74 | 38 | // Box::new(trailing_zero_sized_array_without_repr_c::TrailingZeroSizedArrayWithoutReprC));
|
75 | 39 |
|
76 |
| -// fn temp_alert() {} |
77 |
| - |
78 | 40 | impl EarlyLintPass for TrailingZeroSizedArrayWithoutReprC {
|
79 | 41 | 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 ();} |
| 42 | + if is_struct_with_trailing_zero_sized_array(cx, data) && !has_repr_c(cx, data) { |
| 43 | + span_lint_and_sugg( |
| 44 | + cx, |
| 45 | + todo!(), |
| 46 | + todo!(), |
| 47 | + todo!(), |
| 48 | + "try", |
| 49 | + "`#[repr(C)]`".to_string(), |
| 50 | + Applicability::MachineApplicable, |
| 51 | + ) |
| 52 | + } |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +fn is_struct_with_trailing_zero_sized_array(cx: &EarlyContext<'_>, data: &rustc_ast::VariantData) -> bool { |
| 57 | + if_chain! { |
| 58 | + if let rustc_ast::ast::VariantData::Struct(field_defs, some_bool_huh) = data; |
| 59 | + if let Some(last_field) = field_defs.last(); |
| 60 | + if let rustc_ast::ast::TyKind::Array(_, aconst) = &last_field.ty.kind; |
| 61 | + // TODO: if array is zero-sized; |
| 62 | + then { |
| 63 | + dbg!(aconst); |
| 64 | + true |
| 65 | + } else { |
| 66 | + false |
85 | 67 | }
|
86 | 68 | }
|
87 | 69 | }
|
| 70 | + |
| 71 | +fn has_repr_c(cx: &EarlyContext<'_>, data: &rustc_ast::VariantData) -> bool { |
| 72 | + todo!() |
| 73 | +} |
0 commit comments