Email me

Clippy lints / Performance

Clippy lint · Performance · checked with Clippy 0.1.99

clippy::large_enum_variant: large size difference between variants

What it meansChecks for large size differences between variants on enums. It is in the performance group and warns by default.

What you see

warning: large size difference between variants
 --> main.rs:3:1
  |
3 | / enum Test {
4 | |     A(i32),
  | |     ------ the second-largest variant contains at least 4 bytes
5 | |     B([i32; 8000]),
  | |     -------------- the largest variant contains at least 32000 bytes
6 | | }
  | |_^ the entire enum is at least 32004 bytes
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/stable/index.html#large_enum_variant
  = note: `#[warn(clippy::large_enum_variant)]` on by default
help: consider boxing the large fields or introducing indirection in some other way to reduce the total size of the enum
  |
5 -     B([i32; 8000]),
5 +     B(Box<[i32; 8000]>),
  |

Real output of Clippy 0.1.99 (Rust 1.99) on the example below. Names and line numbers in yours will differ.

Does it matter?

Enum size is bounded by the largest variant. Having one large variant can penalize the memory layout of that enum.

Code that could be written to run faster. Warns by default.

How to fix it

Clippy does not fix this one automatically (its suggestion may change behaviour, so it only shows it). Change the code by hand:

Flagged

enum Test {
    A(i32),
    B([i32; 8000]),
}

Write this instead

// Possibly better
enum Test2 {
    A(i32),
    B(Box<[i32; 8000]>),
}

Turn it off, or on

Allow it where it does not fit your code, as narrowly as you can:

On one item

#[allow(clippy::large_enum_variant)]
fn my_function() { /* ... */ }

Whole crate (Cargo.toml)

[lints.clippy]
large_enum_variant = "allow"

One run

cargo clippy -- -A clippy::large-enum-variant

The [lints] table needs Rust 1.74 or newer. In a workspace, put it under [workspace.lints.clippy] and add [lints] workspace = true to each member.

Settings in clippy.toml

  • enum-variant-size-threshold: The maximum size of an enum’s variant to avoid box suggestion
Performancegroup
warndefault level
nofixed by cargo clippy --fix
pre 1.29.0added in Rust

Clippy's own documentation

What it does

Checks for large size differences between variants on enums.

Why is this bad?

Enum size is bounded by the largest variant. Having one large variant can penalize the memory layout of that enum.

Known problems

This lint obviously cannot take the distribution of variants in your running program into account. It is possible that the smaller variants make up less than 1% of all instances, in which case the overhead is negligible and the boxing is counter-productive. Always measure the change this lint suggests.

For types that implement Copy, the suggestion to Box a variant’s data would require removing the trait impl. The types can of course still be Clone, but that is worse ergonomically. Depending on the use case it may be possible to store the large data in an auxiliary structure (e.g. Arena or ECS).

The lint will ignore the impact of generic types to the type layout by assuming every type parameter is zero-sized. Depending on your use case, this may lead to a false positive.

Example

enum Test {
    A(i32),
    B([i32; 8000]),
}

Use instead:

// Possibly better
enum Test2 {
    A(i32),
    B(Box<[i32; 8000]>),
}

Configuration

  • enum-variant-size-threshold: The maximum size of an enum’s variant to avoid box suggestion

    (default: 200)

From the Clippy lint list (0.1.99), part of the Rust project, MIT or Apache-2.0. Official entry.

Related lints

All 847 Clippy lints · Rust compiler errors · Cargo.toml checker

Up next

Related lintclippy::format_in_format_argsDetects format! within the arguments of another macro that does formatting such as format! itself, write! or println!Read it next →

Did this answer your question?

Get the receipts by email.

What my AI agents tried this week, with the real numbers: what worked, what flopped, what made money. One email when there is something worth reading.

No spam, never sold. Every email has a one-click unsubscribe.

Popular in the shop

All 124 tools →

Browse: Bundles and kitsHome, family and schoolMoney and businessAI toolsSports and fitnessMaking and hobbiesFood and drinkIndie dev and launchTrading and quant

Free: Landing page teardown iPhone app cost calculator LLM API cost calculator Paid online: $49 Launch Check App builds Articles FAQ