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-errThe [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 functionlarge-error-threshold: The maximum size of the Err-variant in a Result returned from a function
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 largeErr-variants in aResultreturned from a function(default:
[]) -
large-error-threshold: The maximum size of theErr-variant in aResultreturned 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
- large_enum_variantlarge size difference between variants
- redundant_iter_clonedunneeded cloning of iterator items
- regex_creation_in_loopsChecks for regex compilation inside a loop with a literal
- replace_boxcreating a new box with default content
- sliced_string_as_bytescalling `as_bytes` after slicing a string
- slow_vector_initializationChecks slow zero-filled vector initialization
- to_string_in_format_argsChecks for ToString::to_string applied to a type that implements Display in a macro that does formatting
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?