Email me

Clippy lints / Performance

Clippy lint · Performance · checked with Clippy 0.1.99

clippy::result_large_err: the `Err`-variant returned from this function is very large

What it meansChecks for functions that return Result with an unusually large Err-variant. It is in the performance group and warns by default.

What you see

warning: the `Err`-variant returned from this function is very large
 --> main.rs:9:19
  |
4 |     UnparsedBytes([u8; 512]),
  |     ------------------------ the largest variant contains at least 512 bytes
...
9 | pub fn parse() -> Result<(), ParseError> {
  |                   ^^^^^^^^^^^^^^^^^^^^^^
  |
  = help: try reducing the size of `main::ParseError`, for example by boxing large elements or replacing it with `Box<main::ParseError>`
  = help: for further information visit https://rust-lang.github.io/rust-clippy/stable/index.html#result_large_err
  = note: `#[warn(clippy::result_large_err)]` on by default

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?

A Result is at least as large as the Err-variant. While we expect that variant to be seldom used, the compiler needs to reserve and move that much memory every single time. Furthermore, errors are often simply passed up the call-stack, making use of the ?-operator and its type-conversion mechanics. If the Err-variant further up the call-stack stores the Err-variant in question (as library code often does), it itself needs to be at least as large, propagating the problem.

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

How to fix it

Clippy does not fix this one automatically. Change the code by hand:

Flagged

pub enum ParseError {
    UnparsedBytes([u8; 512]),
    UnexpectedEof,
}

// The `Result` has at least 512 bytes, even in the `Ok`-case
pub fn parse() -> Result<(), ParseError> {
    Ok(())
}

Write this instead

pub enum ParseError {
    UnparsedBytes(Box<[u8; 512]>),
    UnexpectedEof,
}

// The `Result` is slightly larger than a pointer
pub fn parse() -> Result<(), ParseError> {
    Ok(())
}

Turn it off, or on

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

On one item

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

Whole crate (Cargo.toml)

[lints.clippy]
result_large_err = "allow"

One run

cargo clippy -- -A clippy::result-large-err

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

  • large-error-ignored: A list of paths to types that should be ignored as overly large Err-variants in a Result returned from a function
  • large-error-threshold: The maximum size of the Err-variant in a Result returned from a function
Performancegroup
warndefault level
nofixed by cargo clippy --fix
1.65.0added in Rust

Clippy's own documentation

What it does

Checks for functions that return Result with an unusually large Err-variant.

Why is this bad?

A Result is at least as large as the Err-variant. While we expect that variant to be seldom used, the compiler needs to reserve and move that much memory every single time. Furthermore, errors are often simply passed up the call-stack, making use of the ?-operator and its type-conversion mechanics. If the Err-variant further up the call-stack stores the Err-variant in question (as library code often does), it itself needs to be at least as large, propagating the problem.

Known problems

The size determined by Clippy is platform-dependent.

Examples

pub enum ParseError {
    UnparsedBytes([u8; 512]),
    UnexpectedEof,
}

// The `Result` has at least 512 bytes, even in the `Ok`-case
pub fn parse() -> Result<(), ParseError> {
    Ok(())
}

should be

pub enum ParseError {
    UnparsedBytes(Box<[u8; 512]>),
    UnexpectedEof,
}

// The `Result` is slightly larger than a pointer
pub fn parse() -> Result<(), ParseError> {
    Ok(())
}

Configuration

  • large-error-ignored: A list of paths to types that should be ignored as overly large Err-variants in a Result returned from a function

    (default: [])

  • large-error-threshold: The maximum size of the Err-variant in a Result returned from a function

    (default: 128)

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::large_enum_variantlarge size difference between variantsRead 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