Skip to content

empty_enum_variants_with_brackets is arguably not redundant #12551

Closed
@detly

Description

@detly

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

Metadata

Metadata

Assignees

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