about summary refs log tree commit diff
diff options
context:
space:
mode:
authorllogiq <bogusandre@gmail.com>2024-12-29 00:10:58 +0000
committerGitHub <noreply@github.com>2024-12-29 00:10:58 +0000
commit786f090bb9a62ab73102dbd7a4b9bf05f4fedd2d (patch)
tree2ae02e17a3f97534f2cd85f594c71dd978f55f55
parent998c7801260007b8e36fe8749f3c90b25b3e4df3 (diff)
parenta5d5edc581be1ba1c598f9313e309263bc7360b3 (diff)
downloadrust-786f090bb9a62ab73102dbd7a4b9bf05f4fedd2d.tar.gz
rust-786f090bb9a62ab73102dbd7a4b9bf05f4fedd2d.zip
Do not trigger `trailing_empty_array` in tests (#13844)
Close #13837

changelog: [`trailing_empty_array`]: do not trigger on tests
-rw-r--r--clippy_lints/src/trailing_empty_array.rs7
-rw-r--r--tests/ui/trailing_empty_array.rs14
2 files changed, 19 insertions, 2 deletions
diff --git a/clippy_lints/src/trailing_empty_array.rs b/clippy_lints/src/trailing_empty_array.rs
index a1d92c3ac71..82cc5155380 100644
--- a/clippy_lints/src/trailing_empty_array.rs
+++ b/clippy_lints/src/trailing_empty_array.rs
@@ -1,5 +1,5 @@
 use clippy_utils::diagnostics::span_lint_and_help;
-use clippy_utils::has_repr_attr;
+use clippy_utils::{has_repr_attr, is_in_test};
 use rustc_hir::{Item, ItemKind};
 use rustc_lint::{LateContext, LateLintPass};
 use rustc_middle::ty;
@@ -37,7 +37,10 @@ declare_lint_pass!(TrailingEmptyArray => [TRAILING_EMPTY_ARRAY]);
 
 impl<'tcx> LateLintPass<'tcx> for TrailingEmptyArray {
     fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'tcx>) {
-        if is_struct_with_trailing_zero_sized_array(cx, item) && !has_repr_attr(cx, item.hir_id()) {
+        if is_struct_with_trailing_zero_sized_array(cx, item)
+            && !has_repr_attr(cx, item.hir_id())
+            && !is_in_test(cx.tcx, item.hir_id())
+        {
             span_lint_and_help(
                 cx,
                 TRAILING_EMPTY_ARRAY,
diff --git a/tests/ui/trailing_empty_array.rs b/tests/ui/trailing_empty_array.rs
index 309a5920dfd..ea3b8ff01af 100644
--- a/tests/ui/trailing_empty_array.rs
+++ b/tests/ui/trailing_empty_array.rs
@@ -193,3 +193,17 @@ type C = ConstParamNoDefault<0>;
 type D = ConstParamNonZeroDefault<0>;
 
 fn main() {}
+
+#[cfg(test)]
+mod tests {
+    pub struct Friend {
+        age: u8,
+    }
+
+    #[test]
+    fn oldest_empty_is_none() {
+        struct Michael {
+            friends: [Friend; 0],
+        }
+    }
+}