Repository: rust-lang/rust-clippy (Rust)

Issue:
trailing_empty_array in tests
### Summary

Empty arrays are not unreasonable in test code. I noticed this with proptest-generated structs, but it applies equally to user-written code, so I think it makes sense to allow this for all test code, rather than getting proptest to put `#[allow(clippy::trailing_empty_array)]` on its generated code.

### Lint Name

trailing_empty_array

### Reproducer

I tried this code:

```rust
pub struct Friend {
    age: u8,
}

pub trait Friendly<const N: usize> {
    fn friends(&self) -> &[Friend; N];

    fn oldest_friend(&self) -> Option<&Friend> {
        self.friends().iter().max_by_key(|x| x.age)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn oldest_empty_is_none() {
        struct Michael {
            friends: [Friend; 0],
        }

        impl Friendly<0> for Michael {
            fn friends(&self) -> &[Friend; 0] {
                &self.friends
            }
        }

        let michael_the_loneliest_boy_in_town = Michael { friends: [] };

        assert!(michael_the_loneliest_boy_in_town.oldest_friend().is_none());
    }
}
```

I saw this happen:

```bash
$ clippy-driver --crate-type=lib -W clippy::trailing_empty_array --test a.rs
warning: trailing zero-sized array in a struct which is not marked with a `repr` attribute
  --> a.rs:19:9
   |
19 | /         struct Michael {
20 | |             friends: [Friend; 0],
21 | |         }
   | |_________^
   |
   = help: consider annotating `tests::oldest_empty_is_none::Michael` with `#[repr(C)]` or another `repr` attribute
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#trailing_empty_array
   = note: requested on the command line with `-W clippy::trailing-empty-array`

warning: 1 warning emitted
```

I expected to see this happen: [no error]


### Version

```text
rustc 1.85.0-nightly (c26db435b 2024-12-15)
binary: rustc
commit-hash: c26db435bf8aee2efc397aab50f3a21eb351d6e5
commit-date: 2024-12-15
host: x86_64-unknown-linux-gnu
release: 1.85.0-nightly
LLVM version: 19.1.5
```
and
```text
rustc 1.83.0 (90b35a623 2024-11-26)
binary: rustc
commit-hash: 90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf
commit-date: 2024-11-26
host: x86_64-unknown-linux-gnu
release: 1.83.0
LLVM version: 19.1.1
```

### Additional Labels

_No response_

<!-- TRIAGEBOT_START -->

<!-- TRIAGEBOT_ASSIGN_START -->

<!-- TRIAGEBOT_ASSIGN_DATA_START$${"user":"alex-semenyuk"}$$TRIAGEBOT_ASSIGN_DATA_END -->

<!-- TRIAGEBOT_ASSIGN_END -->
<!-- TRIAGEBOT_END -->


Discussion:
@rustbot claim



Patch:
diff --git a/clippy_lints/src/trailing_empty_array.rs b/clippy_lints/src/trailing_empty_array.rs
index a1d92c3ac71f..82cc5155380e 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,


Test patch:
diff --git a/tests/ui/trailing_empty_array.rs b/tests/ui/trailing_empty_array.rs
index 3d06c2621681..560438b882cf 100644
--- a/tests/ui/trailing_empty_array.rs
+++ b/tests/ui/trailing_empty_array.rs
@@ -192,3 +192,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],
+        }
+    }
+}