Skip to content

Unit-like structs should not trigger exhaustive_structs #12675

Closed as not planned
@kpreid

Description

@kpreid

Summary

By using unit-like struct syntax, one is opting into behavior which is incompatible with ever adding any fields (the constant named after the struct). Therefore, adding #[non_exhaustive] does not provide any forward-compatibility benefit — adding fields is always a breaking change. Therefore, Clippy should not recommend #[non_exhaustive] on unit-like structs.

Lint Name

exhaustive_structs

Reproducer

I tried this code:

#![warn(clippy::exhaustive_structs)]
pub struct Foo;

I saw this happen:

warning: exported structs should not be exhaustive
 --> src/lib.rs:2:1
  |
2 | pub struct Foo;
  | ^^^^^^^^^^^^^^^
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#exhaustive_structs
note: the lint level is defined here
 --> src/lib.rs:1:9
  |
1 | #![warn(clippy::exhaustive_structs)]
  |         ^^^^^^^^^^^^^^^^^^^^^^^^^^
help: try adding #[non_exhaustive]
  |
2 + #[non_exhaustive]
3 | pub struct Foo;
  |

I expected to see this happen: No lint

Version

rustc 1.77.1 (7cf61ebde 2024-03-27)
binary: rustc
commit-hash: 7cf61ebde7b22796c69757901dd346d0fe70bd97
commit-date: 2024-03-27
host: x86_64-apple-darwin
release: 1.77.1
LLVM version: 17.0.6

Additional Labels

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't have

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions