Skip to content

exhaustive-structs wants to add non_exhaustive to structs with default_field_values which does not work #14992

Closed
@matthiaskrgr

Description

@matthiaskrgr

Using the following flags

--force-warn clippy::exhaustive-structs

this code:

#![feature(default_field_values)]

pub struct A {
    pub a: isize = 42,
}

caused the following diagnostics:

    Checking _struct_field_default v0.1.0 (/tmp/icemaker_global_tempdir.o4hDRjvshrmO/icemaker_clippyfix_tempdir.6eDmH3ibcMeG/_struct_field_default)
warning: exported structs should not be exhaustive
 --> src/lib.rs:3:1
  |
3 | / pub struct A {
4 | |     pub a: isize = 42,
5 | | }
  | |_^
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#exhaustive_structs
  = note: requested on the command line with `--force-warn clippy::exhaustive-structs`
help: try adding #[non_exhaustive]
  |
3 + #[non_exhaustive]
4 | pub struct A {
  |

warning: `_struct_field_default` (lib) generated 1 warning
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.11s

However after applying these diagnostics, the resulting code:

#![feature(default_field_values)]

#[non_exhaustive]
pub struct A {
    pub a: isize = 42,
}

no longer compiled:

    Checking _struct_field_default v0.1.0 (/tmp/icemaker_global_tempdir.o4hDRjvshrmO/icemaker_clippyfix_tempdir.6eDmH3ibcMeG/_struct_field_default)
error: `#[non_exhaustive]` can't be used to annotate items with default field values
 --> src/lib.rs:3:1
  |
3 |   #[non_exhaustive]
  |   ^^^^^^^^^^^^^^^^^
4 | / pub struct A {
5 | |     pub a: isize = 42,
6 | | }
  | |_- this struct has default field values

error: could not compile `_struct_field_default` (lib test) due to 1 previous error
warning: build failed, waiting for other jobs to finish...
error: could not compile `_struct_field_default` (lib) due to 1 previous error

Version:

rustc 1.89.0-nightly (cf423712b 2025-06-05)
binary: rustc
commit-hash: cf423712b9e95e9f6ec84b1ecb3d125e55ac8d56
commit-date: 2025-06-05
host: x86_64-unknown-linux-gnu
release: 1.89.0-nightly
LLVM version: 20.1.5

Metadata

Metadata

Assignees

Labels

I-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions