Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit-like structs should not trigger exhaustive_structs #12675

Closed
kpreid opened this issue Apr 13, 2024 · 2 comments
Closed

Unit-like structs should not trigger exhaustive_structs #12675

kpreid opened this issue Apr 13, 2024 · 2 comments
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@kpreid
Copy link
Contributor

kpreid commented Apr 13, 2024

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

@kpreid kpreid added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Apr 13, 2024
@Alexendoo
Copy link
Member

Is there a case #[non_exhaustive] doesn't catch? My understanding is that it prevents using the struct in a way that would reveal it's a unit struct

@kpreid
Copy link
Contributor Author

kpreid commented Apr 14, 2024

My understanding is that it prevents using the struct in a way that would reveal it's a unit struct

Ah, I was not aware of this:

#[non_exhaustive]
pub struct Foo;
use scratchpad::Foo;
fn main() {
    let _: Foo = Foo;
}
error[E0423]: expected value, found struct `Foo`
 --> src/bin/scratchbin.rs:3:18
  |
3 |     let _: Foo = Foo;
  |                  ^^^ constructor is not visible here due to private fields

Sorry for filing an issue with an incorrect assumption.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have
Projects
None yet
Development

No branches or pull requests

2 participants