rust-lang / rust-lang/rust-clippy

Lint for large error types

Open
#6,560 2 comments 2 reactions 1 assignee View on GitHub

@Y-Nak is already working on this.

Since Feb 24, 2021.

A-lint E-medium L-perf T-middle
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

What it does

Warn when an error type (probably by detecting that it has impl std::error::Error and is publicly exported) is over some arbitrary threshold, suggesting boxing the inner data to reduce the size.

Categories (optional)
  • Kind: clippy::perf

Large error types result in a lot of redundant memory copying on the success path when the Ok value is small.

Drawbacks

None.

Example
struct Error {
    data: [u8; 512],
}

impl std::error::Error for Error {}

Could be written as:


struct Error {
    data: Box<[u8; 512]>,
}

impl std::error::Error for Error {}
Other

There are two existing related issues: https://github.com/rust-lang/rust-clippy/issues/4652 and https://github.com/rust-lang/rust-clippy/issues/3884. I think this is different because in those cases it detects when you use the error internally with a small success variant; whereas even if internally all usage is with large success variants, a user of the library may be mapping the success variant to something small, ending up hitting one of those lints in their usage and having to box the whole error; while it would be easier for all users if the size was reduced at the source.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.