Skip to content

Lint for large error types #6560

Open
Open
@Nemo157

Description

@Nemo157

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: #4652 and #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.

Metadata

Metadata

Assignees

Labels

A-lintArea: New lintsE-mediumCall for participation: Medium difficulty level problem and requires some initial experience.L-perfLint: Belongs in the perf lint groupT-middleType: Probably requires verifiying types

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions