about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--clippy_lints/src/lib.rs2
-rw-r--r--clippy_lints/src/trailing_zero_sized_array_without_repr_c.rs76
2 files changed, 33 insertions, 45 deletions
diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs
index 17bffc08309..6624ee93101 100644
--- a/clippy_lints/src/lib.rs
+++ b/clippy_lints/src/lib.rs
@@ -30,6 +30,7 @@ extern crate rustc_index;
 extern crate rustc_infer;
 extern crate rustc_lexer;
 extern crate rustc_lint;
+extern crate rustc_lint_defs;
 extern crate rustc_middle;
 extern crate rustc_mir_dataflow;
 extern crate rustc_parse;
@@ -487,6 +488,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
         store.register_late_pass(|| Box::new(utils::internal_lints::OuterExpnDataPass));
     }
 
+    store.register_early_pass(||        Box::new(trailing_zero_sized_array_without_repr_c::TrailingZeroSizedArrayWithoutReprC));
     store.register_late_pass(|| Box::new(utils::author::Author));
     store.register_late_pass(|| Box::new(await_holding_invalid::AwaitHolding));
     store.register_late_pass(|| Box::new(serde_api::SerdeApi));
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 507d9b40412..a96b8792295 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,7 +1,6 @@
 use clippy_utils::diagnostics::span_lint_and_sugg;
-use rustc_hir::*;
 use rustc_lint::{EarlyContext, EarlyLintPass};
-use rustc_lint::{LateContext, LateLintPass};
+use rustc_lint_defs::Applicability;
 use rustc_session::{declare_lint_pass, declare_tool_lint};
 
 declare_clippy_lint! {
@@ -33,55 +32,42 @@ declare_clippy_lint! {
 }
 declare_lint_pass!(TrailingZeroSizedArrayWithoutReprC => [TRAILING_ZERO_SIZED_ARRAY_WITHOUT_REPR_C]);
 
-impl LateLintPass<'_> for TrailingZeroSizedArrayWithoutReprC {
-    fn check_struct_def(&mut self, cx: &LateContext<'tcx>, data: &'tcx rustc_hir::VariantData<'tcx>) {
-        dbg!("in check_struct_def");
-        if_chain! {
-            if let Some(def) = data.fields().last();
-            if let rustc_hir::TyKind::Array(ty, acost) = def.ty.kind;
-            then {
-                // is the AnonConst `0`
-            }
-        }
-
-        // span_lint_and_sugg(
-        //     cx,
-        //     todo!(),
-        //     todo!(),
-        //     todo!(),
-        //     todo!(),
-        //     todo!(),
-        //     rustc_errors::Applicability::MaybeIncorrect,
-        // )
-    }
-    // https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/sty/enum.TyKind.html#variant.Array in latepass
-    // or https://doc.rust-lang.org/nightly/nightly-rustc/rustc_ast/ast/enum.TyKind.html#variant.Array in early pass
-
-    // fn check_struct_def_post(&mut self, _: &LateContext<'tcx>, _: &'tcx rustc_hir::VariantData<'tcx>)
-    // {}
-
-    // fn check_field_def(&mut self, _: &LateContext<'tcx>, _: &'tcx rustc_hir::FieldDef<'tcx>) {}
-
-    // fn check_attribute(&mut self, _: &LateContext<'tcx>, _: &'tcx rustc_ast::Attribute) {}
-
-    // fn enter_lint_attrs(&mut self, _: &LateContext<'tcx>, _: &'tcx [rustc_ast::Attribute]) {}
-
-    // fn exit_lint_attrs(&mut self, _: &LateContext<'tcx>, _: &'tcx [rustc_ast::Attribute]) {}
-}
 //
 // TODO: Register the lint pass in `clippy_lints/src/lib.rs`,
-//       e.g. store.register_late_pass(||
+//       e.g. store.register_early_pass(||
 // Box::new(trailing_zero_sized_array_without_repr_c::TrailingZeroSizedArrayWithoutReprC));
 
-// fn temp_alert() {}
-
 impl EarlyLintPass for TrailingZeroSizedArrayWithoutReprC {
     fn check_struct_def(&mut self, cx: &EarlyContext<'_>, data: &rustc_ast::VariantData) {
-        if_chain! {
-            if let rustc_ast::ast::VariantData::Struct(field_defs, some_bool_huh) = data;
-            if let Some(last_field) = field_defs.last();
-            if let rustc_ast::ast::TyKind::Array(_, aconst) = &last_field.ty.kind;            
-            then {dbg!(aconst); return ();}
+        if is_struct_with_trailing_zero_sized_array(cx, data) && !has_repr_c(cx, data) {
+            span_lint_and_sugg(
+                cx,
+                todo!(),
+                todo!(),
+                todo!(),
+                "try",
+                "`#[repr(C)]`".to_string(),
+                Applicability::MachineApplicable,
+            )
+        }
+    }
+}
+
+fn is_struct_with_trailing_zero_sized_array(cx: &EarlyContext<'_>, data: &rustc_ast::VariantData) -> bool {
+    if_chain! {
+        if let rustc_ast::ast::VariantData::Struct(field_defs, some_bool_huh) = data;
+        if let Some(last_field) = field_defs.last();
+        if let rustc_ast::ast::TyKind::Array(_, aconst) = &last_field.ty.kind;
+        // TODO: if array is zero-sized;
+        then {
+            dbg!(aconst);
+            true
+        } else {
+            false
         }
     }
 }
+
+fn has_repr_c(cx: &EarlyContext<'_>, data: &rustc_ast::VariantData) -> bool {
+    todo!()
+}