Skip to content

empty_enum_variants_with_brackets is arguably not redundant #12551

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

Closed
detly opened this issue Mar 25, 2024 · 4 comments · Fixed by #12971
Closed

empty_enum_variants_with_brackets is arguably not redundant #12551

detly opened this issue Mar 25, 2024 · 4 comments · Fixed by #12971
Assignees
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

@detly
Copy link

detly commented Mar 25, 2024

Description

I noticed that Clippy in Rust 1.77 includes a new lint empty_enum_variants_with_brackets (from #12047). The reasoning is that these are redundant.

Functionally, sure. But there is a feature of Rust where tuple-like enum variants get a kind of static function generated as a constructor for them, so you can do things like this:

pub enum Symbol {
    Number(i32),
    Text(String),
    Nothing(),
    Whitespace(),
}

pub fn parse_only_as_number(text: &str) -> Symbol {
    text.parse().ok().map_or_else(Symbol::Nothing, Symbol::Number)
}

This is extremely contrived, yes, but illustrates that (a) not every enum with empty variants will have a natural Default implementation, and (b) removing the () has effects outside of that change alone eg. any API requiring and receiving a FnOnce() -> Symbol now has to be called with a closure (|| Symbol::Nothing) or a static constructor has to be explicitly written.

I do not have strong feelings about it, but I do use this sometimes and I wanted to raise it in case it hadn't been considered. (Ideally I'd like Rust to extend the automatic static constructor thing to any empty-equivalent enum variant ie. Empty, Empty(), Empty(()) and Empty {} would all have it, but I'm not writing any RFCs any time soon.)

Version

rustc 1.77.0 (aedd173a2 2024-03-17)
binary: rustc
commit-hash: aedd173a2c086e558c2b66d3743b344f977621a7
commit-date: 2024-03-17
host: x86_64-unknown-linux-gnu
release: 1.77.0
LLVM version: 17.0.6
@ARandomDev99
Copy link
Contributor

ARandomDev99 commented Apr 1, 2024

Ideally I'd like Rust to extend the automatic static constructor thing to any empty-equivalent enum variant ie. Empty, Empty(), Empty(()) and Empty {}

Regarding this, https://rust-lang.zulipchat.com/#narrow/stream/122651-general/topic/.E2.9C.94.20RFC.20idea/near/430262583
About the lint itself, the ideal solution would be to check if the variant is used as a function anywhere.

@J-ZhengLi J-ZhengLi 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 3, 2024
@detly
Copy link
Author

detly commented Apr 10, 2024

About the lint itself, the ideal solution would be to check if the variant is used as a function anywhere.

What if it's pub?

@ARandomDev99
Copy link
Contributor

@rustbot claim

@ARandomDev99
Copy link
Contributor

github-merge-queue bot pushed a commit that referenced this issue Apr 18, 2025
…enum variants used as functions in the same crate (#12971)

Fixes #12551

changelog: [`empty_enum_variants_with_brackets`]: Do not lint reachable
enums or enums which are used as functions within the same crate.

r? @xFrednet
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
3 participants