Trouble with unused enum variants: must_construct

Consider this code:

/// Errors produced by this crate
pub enum Error {
    Foo(Foo),
    Bar(u32),
    // ...
}

Let's say someone crated such type and forgot to construct a variant. Because the type is public it will not trigger a warning even though the author intended to produce every variant in the crate defining it. I actually encountered this in a real crate.

It'd be cool to have something like #[must_construct] that would cause a warning if the crate defining the enum doesn't construct a variant. Deriving of Clone or Deserialize should not be considered use though.

Does anyone see a problem with this?

3 Likes

It's not quite the same thing as your suggestion, but I'd like to see a more general opt-in "warn me about unusedness as if this item was private, even though it's public", to catch cases where an item being unused internally is a sign of a semantic problem, or when the item is public-but-hidden for cases like macros and special tests.

14 Likes

I often write bin crates with lots of path dependencies on local crates which exist mainly for speeding up incremental compile times. Those local crates are not really meant for use outside of building the bin crate. That means that if something is unused in the bin it is not used at all.

So, I would find it useful to enable checking for unused pub things from path dependencies at the workspace level.

warnalyzer helps with this, but the false positives and the complicated to automate usage mean that it doesn’t get run as often as say, clippy.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.