Skip to content

clippy doesn't generate large_enum_variant warning when using bytes::Bytes #11915

Closed
@swanandx

Description

@swanandx

Summary

If we are using Vec, clippy generates warning for large size difference in enum variants as expected, but if we replace Vec with bytes::Bytes, clippy doesn't generate any warning! Even when the size difference of enum variants is much bigger in later case!

Lint Name

large_enum_variant

Reproducer

I tried this code:

use bytes::Bytes;

// size = 296
enum NoWarnings {
    BigBoi(PublishWithBytes),
    _SmallBoi(u8),
}

//  size = 224
enum MakesClippyAngry {
    BigBoi(PublishWithVec),
    _SmallBoi(u8),
}

fn main() {
    let v = MakesClippyAngry::BigBoi(PublishWithVec::default());
    dbg!(std::mem::size_of_val(&v));
    let u = NoWarnings::BigBoi(PublishWithBytes::default());
    dbg!(std::mem::size_of_val(&u));
}

// can't find better way to generate structs with big size fast lol
#[derive(Default, Debug)]
struct PublishWithBytes {
    _dup: bool,
    _retain: bool,
    _topic: Bytes,
    __topic: Bytes,
    ___topic: Bytes,
    ____topic: Bytes,
    _pkid: u16,
    _payload: Bytes,
    __payload: Bytes,
    ___payload: Bytes,
    ____payload: Bytes,
    _____payload: Bytes,
}

#[derive(Default, Debug)]
struct PublishWithVec {
    _dup: bool,
    _retain: bool,
    _topic: Vec<u8>,
    __topic: Vec<u8>,
    ___topic: Vec<u8>,
    ____topic: Vec<u8>,
    _pkid: u16,
    _payload: Vec<u8>,
    __payload: Vec<u8>,
    ___payload: Vec<u8>,
    ____payload: Vec<u8>,
    _____payload: Vec<u8>,
}

I expected to see this happen:

clippy should have generated large_enum_variant warning for both enums NoWarnings ( uses Bytes ) & MakesClippyAngry ( uses Vec ).

Instead, this happened:

Clippy generates warning only for MakesClippyAngry, but doesn't for NoWarnings.

warning: large size difference between variants
  --> src/main.rs:10:1
   |
10 | / enum MakesClippyAngry {
11 | |     BigBoi(PublishWithVec),
   | |     ---------------------- the largest variant contains at least 224 bytes
12 | |     _SmallBoi(u8),
   | |     ------------- the second-largest variant contains at least 1 bytes
13 | | }
   | |_^ the entire enum is at least 224 bytes
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#large_enum_variant
   = note: `#[warn(clippy::large_enum_variant)]` on by default
help: consider boxing the large fields to reduce the total size of the enum
   |
11 |     BigBoi(Box<PublishWithVec>),
   |            ~~~~~~~~~~~~~~~~~~~

Version

rustc 1.74.0 (79e9716c9 2023-11-13)
binary: rustc
commit-hash: 79e9716c980570bfd1f666e3b16ac583f0168962
commit-date: 2023-11-13
host: x86_64-unknown-linux-gnu
release: 1.74.0
LLVM version: 17.0.4

Metadata

Metadata

Assignees

Labels

C-bugCategory: Clippy is not doing the correct thingI-false-negativeIssue: The lint should have been triggered on code, but wasn't

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions