about summary refs log tree commit diff
diff options
context:
space:
mode:
authorNathaniel Hamovitz <18648574+nhamovitz@users.noreply.github.com>2021-10-18 17:41:27 -0700
committerNathaniel Hamovitz <18648574+nhamovitz@users.noreply.github.com>2021-10-18 17:41:27 -0700
commitc654cc56da81e9bebc37dd6c476b40c07d297cbe (patch)
treea88d1dfec0f939edcf4b2162212335f83dbc8d8b
parent6303d2d075131641ef325344124c71052d2bd3eb (diff)
downloadrust-c654cc56da81e9bebc37dd6c476b40c07d297cbe.tar.gz
rust-c654cc56da81e9bebc37dd6c476b40c07d297cbe.zip
One more test + final tidying
-rw-r--r--clippy_lints/src/trailing_zero_sized_array_without_repr.rs55
-rw-r--r--tests/ui/trailing_zero_sized_array_without_repr.rs8
-rw-r--r--tests/ui/trailing_zero_sized_array_without_repr.stderr44
3 files changed, 53 insertions, 54 deletions
diff --git a/clippy_lints/src/trailing_zero_sized_array_without_repr.rs b/clippy_lints/src/trailing_zero_sized_array_without_repr.rs
index 0c9066fda82..88ec471184c 100644
--- a/clippy_lints/src/trailing_zero_sized_array_without_repr.rs
+++ b/clippy_lints/src/trailing_zero_sized_array_without_repr.rs
@@ -1,11 +1,7 @@
-use clippy_utils::{
-    diagnostics::{span_lint_and_help, span_lint_and_sugg, span_lint_and_then},
-    source::{indent_of, snippet},
-};
-use rustc_errors::Applicability;
+use clippy_utils::diagnostics::span_lint_and_help;
 use rustc_hir::{HirId, Item, ItemKind};
 use rustc_lint::{LateContext, LateLintPass};
-use rustc_middle::ty::{Const, TyS};
+use rustc_middle::ty::Const;
 use rustc_session::{declare_lint_pass, declare_tool_lint};
 use rustc_span::sym;
 
@@ -40,54 +36,39 @@ declare_lint_pass!(TrailingZeroSizedArrayWithoutRepr => [TRAILING_ZERO_SIZED_ARR
 
 impl<'tcx> LateLintPass<'tcx> for TrailingZeroSizedArrayWithoutRepr {
     fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'tcx>) {
-        dbg!(item.ident);
         if is_struct_with_trailing_zero_sized_array(cx, item) && !has_repr_attr(cx, item.hir_id()) {
-            let help_msg = format!(
-                "consider annotating {} with `#[repr(C)]` or another `repr` attribute",
-                cx.tcx
-                    .type_of(item.def_id)
-                    .ty_adt_def()
-                    .map(|adt_def| cx.tcx.def_path_str(adt_def.did))
-                    .unwrap_or_else(
-                        // I don't think this will ever be the case, since we made it through
-                        // `is_struct_with_trailing_zero_sized_array`, but I don't feel comfortable putting an `unwrap`
-                        || "the struct definition".to_string()
-                    )
-            );
-
             span_lint_and_help(
                 cx,
                 TRAILING_ZERO_SIZED_ARRAY_WITHOUT_REPR,
                 item.span,
                 "trailing zero-sized array in a struct which is not marked with a `repr` attribute",
                 None,
-                &help_msg,
+                &format!(
+                    "consider annotating `{}` with `#[repr(C)]` or another `repr` attribute",
+                    cx.tcx.def_path_str(item.def_id.to_def_id())
+                ),
             );
         }
     }
 }
 
 fn is_struct_with_trailing_zero_sized_array(cx: &LateContext<'tcx>, item: &'tcx Item<'tcx>) -> bool {
-    // TODO: when finalized, replace with an `if_chain`. I have it like this because my rust-analyzer
-    // doesn't work when it's an `if_chain`.
+    if_chain! {
+        // First check if last field is an array
+        if let ItemKind::Struct(data, _) = &item.kind;
+        if let Some(last_field) = data.fields().last();
+        if let rustc_hir::TyKind::Array(_, length) = last_field.ty.kind;
 
-    // First check if last field is an array
-    if let ItemKind::Struct(data, _) = &item.kind {
-        if let Some(last_field) = data.fields().last() {
-            if let rustc_hir::TyKind::Array(_, length) = last_field.ty.kind {
-                // Then check if that that array zero-sized
-                let length_ldid = cx.tcx.hir().local_def_id(length.hir_id);
-                let length = Const::from_anon_const(cx.tcx, length_ldid);
-                let length = length.try_eval_usize(cx.tcx, cx.param_env);
-                length == Some(0)
-            } else {
-                false
-            }
+        // Then check if that that array zero-sized
+        let length_ldid = cx.tcx.hir().local_def_id(length.hir_id);
+        let length = Const::from_anon_const(cx.tcx, length_ldid);
+        let length = length.try_eval_usize(cx.tcx, cx.param_env);
+        if let Some(length) = length;
+        then {
+            length == 0
         } else {
             false
         }
-    } else {
-        false
     }
 }
 
diff --git a/tests/ui/trailing_zero_sized_array_without_repr.rs b/tests/ui/trailing_zero_sized_array_without_repr.rs
index 52966c64db7..dee7f811540 100644
--- a/tests/ui/trailing_zero_sized_array_without_repr.rs
+++ b/tests/ui/trailing_zero_sized_array_without_repr.rs
@@ -46,6 +46,14 @@ struct ZeroSizedWithConstFunction {
     last: [usize; compute_zero()],
 }
 
+const fn compute_zero_from_arg(x: usize) -> usize {
+    x - 1
+}
+struct ZeroSizedWithConstFunction2 {
+    field: i32,
+    last: [usize; compute_zero_from_arg(1)],
+}
+
 struct ZeroSizedArrayWrapper([usize; 0]);
 
 struct TupleStruct(i32, [usize; 0]);
diff --git a/tests/ui/trailing_zero_sized_array_without_repr.stderr b/tests/ui/trailing_zero_sized_array_without_repr.stderr
index e5714386c8b..93c607bbf38 100644
--- a/tests/ui/trailing_zero_sized_array_without_repr.stderr
+++ b/tests/ui/trailing_zero_sized_array_without_repr.stderr
@@ -8,7 +8,7 @@ LL | | }
    | |_^
    |
    = note: `-D clippy::trailing-zero-sized-array-without-repr` implied by `-D warnings`
-   = help: consider annotating the struct definition with `#[repr(C)]` or another `repr` attribute
+   = help: consider annotating `RarelyUseful` with `#[repr(C)]` or another `repr` attribute
 
 error: trailing zero-sized array in a struct which is not marked with a `repr` attribute
   --> $DIR/trailing_zero_sized_array_without_repr.rs:11:1
@@ -18,7 +18,7 @@ LL | |     first_and_last: [usize; 0],
 LL | | }
    | |_^
    |
-   = help: consider annotating the struct definition with `#[repr(C)]` or another `repr` attribute
+   = help: consider annotating `OnlyField` with `#[repr(C)]` or another `repr` attribute
 
 error: trailing zero-sized array in a struct which is not marked with a `repr` attribute
   --> $DIR/trailing_zero_sized_array_without_repr.rs:15:1
@@ -29,19 +29,18 @@ LL | |     last: [T; 0],
 LL | | }
    | |_^
    |
-   = help: consider annotating the struct definition with `#[repr(C)]` or another `repr` attribute
+   = help: consider annotating `GenericArrayType` with `#[repr(C)]` or another `repr` attribute
 
 error: trailing zero-sized array in a struct which is not marked with a `repr` attribute
-  --> $DIR/trailing_zero_sized_array_without_repr.rs:20:1
+  --> $DIR/trailing_zero_sized_array_without_repr.rs:21:1
    |
-LL | / #[must_use]
-LL | | struct OnlyAnotherAttribute {
+LL | / struct OnlyAnotherAttribute {
 LL | |     field: i32,
 LL | |     last: [usize; 0],
 LL | | }
    | |_^
    |
-   = help: consider annotating the struct definition with `#[repr(C)]` or another `repr` attribute
+   = help: consider annotating `OnlyAnotherAttribute` with `#[repr(C)]` or another `repr` attribute
 
 error: trailing zero-sized array in a struct which is not marked with a `repr` attribute
   --> $DIR/trailing_zero_sized_array_without_repr.rs:29:1
@@ -52,7 +51,7 @@ LL | |     last: [usize; 0],
 LL | | }
    | |_^
    |
-   = help: consider annotating the struct definition with `#[repr(C)]` or another `repr` attribute
+   = help: consider annotating `OnlyADeriveAttribute` with `#[repr(C)]` or another `repr` attribute
 
 error: trailing zero-sized array in a struct which is not marked with a `repr` attribute
   --> $DIR/trailing_zero_sized_array_without_repr.rs:35:1
@@ -63,7 +62,7 @@ LL | |     last: [usize; ZERO],
 LL | | }
    | |_^
    |
-   = help: consider annotating the struct definition with `#[repr(C)]` or another `repr` attribute
+   = help: consider annotating `ZeroSizedWithConst` with `#[repr(C)]` or another `repr` attribute
 
 error: trailing zero-sized array in a struct which is not marked with a `repr` attribute
   --> $DIR/trailing_zero_sized_array_without_repr.rs:44:1
@@ -74,26 +73,37 @@ LL | |     last: [usize; compute_zero()],
 LL | | }
    | |_^
    |
-   = help: consider annotating the struct definition with `#[repr(C)]` or another `repr` attribute
+   = help: consider annotating `ZeroSizedWithConstFunction` with `#[repr(C)]` or another `repr` attribute
 
 error: trailing zero-sized array in a struct which is not marked with a `repr` attribute
-  --> $DIR/trailing_zero_sized_array_without_repr.rs:49:1
+  --> $DIR/trailing_zero_sized_array_without_repr.rs:52:1
+   |
+LL | / struct ZeroSizedWithConstFunction2 {
+LL | |     field: i32,
+LL | |     last: [usize; compute_zero_from_arg(1)],
+LL | | }
+   | |_^
+   |
+   = help: consider annotating `ZeroSizedWithConstFunction2` with `#[repr(C)]` or another `repr` attribute
+
+error: trailing zero-sized array in a struct which is not marked with a `repr` attribute
+  --> $DIR/trailing_zero_sized_array_without_repr.rs:57:1
    |
 LL | struct ZeroSizedArrayWrapper([usize; 0]);
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
-   = help: consider annotating the struct definition with `#[repr(C)]` or another `repr` attribute
+   = help: consider annotating `ZeroSizedArrayWrapper` with `#[repr(C)]` or another `repr` attribute
 
 error: trailing zero-sized array in a struct which is not marked with a `repr` attribute
-  --> $DIR/trailing_zero_sized_array_without_repr.rs:51:1
+  --> $DIR/trailing_zero_sized_array_without_repr.rs:59:1
    |
 LL | struct TupleStruct(i32, [usize; 0]);
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
-   = help: consider annotating the struct definition with `#[repr(C)]` or another `repr` attribute
+   = help: consider annotating `TupleStruct` with `#[repr(C)]` or another `repr` attribute
 
 error: trailing zero-sized array in a struct which is not marked with a `repr` attribute
-  --> $DIR/trailing_zero_sized_array_without_repr.rs:53:1
+  --> $DIR/trailing_zero_sized_array_without_repr.rs:61:1
    |
 LL | / struct LotsOfFields {
 LL | |     f1: u32,
@@ -104,7 +114,7 @@ LL | |     last: [usize; 0],
 LL | | }
    | |_^
    |
-   = help: consider annotating the struct definition with `#[repr(C)]` or another `repr` attribute
+   = help: consider annotating `LotsOfFields` with `#[repr(C)]` or another `repr` attribute
 
-error: aborting due to 10 previous errors
+error: aborting due to 11 previous errors