Skip to content

New lint: large Err(E) variant, as compared with Ok(T), for Result<T, E> #3884

Closed
@dekellum

Description

@dekellum

I've found the large_enum_variant lint from clippy quite educational and was hoping it would be able to lint on what I'll call unbalanced Result<T, E> specified types, in particular when the E (Error) type is really large but the T type is quite small. From my reading, this is often a performance concern, e.g. wasting lots of stack space for an infrequently used error path. On the other hand if the T (Ok, happy path) is larger, then the cost of E should be negligible. Make sense?

After not being able to trigger clippy with Result<(), BigError> (where BigError is 3,000 bytes) I tried using my own MyResult, along the lines of:

pub enum MyResult<T, E> {
    Ok(T),
    Err(E),
}

/// MyResult concrete type with zero-bytes `Ok` and 3K bytes `Err` variant,
/// for a fixed stack size of 3001 bytes (asserted).
type Unbalanced = MyResult<(), BigError>;

fn f() -> Unbalanced {
    MyResult::Err(BigError::Large(error::Biggie { blob: [0u8; 3000] }))
}

But no clippy lint at present on either the fully specified type Unbalanced alias or the f() function definition.

Have/would you consider extending this lint to all generic enums, where they are fully specified, or perhaps more practically, adding a new lint specific to core::result::Result? The suggestion to use Box<E> would be applicable in the later case.

I don't claim to well understand the clippy code base, but this was the only hint I could find (an @oli-obk comment) that generic enum's were considered (and are likely ignored) for the original lint:

// don't count generics by filtering out everything

% cargo clippy -V
clippy 0.0.212 (016d92d 2019-03-10)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintArea: New lintsC-enhancementCategory: Enhancement of lints, like adding more cases or adding help messagesT-MIRType: This lint will require working with the MIR

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions