Skip to content

Commit 071a590

Browse files
authored
Rollup merge of rust-lang#5466 - phansch:large-enum-variant-output, r=flip1995
large_enum_variant: Report sizes of variants This reports the sizes of the largest and second-largest variants. Closes rust-lang#5459 changelog: `large_enum_variant`: Report the sizes of the largest and second-largest variants.
2 parents 2538e63 + 69c3e9c commit 071a590

File tree

2 files changed

+43
-10
lines changed

2 files changed

+43
-10
lines changed

clippy_lints/src/large_enum_variant.rs

+19-6
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,19 @@ declare_clippy_lint! {
2121
/// measure the change this lint suggests.
2222
///
2323
/// **Example:**
24+
///
2425
/// ```rust
26+
/// // Bad
2527
/// enum Test {
2628
/// A(i32),
2729
/// B([i32; 8000]),
2830
/// }
31+
///
32+
/// // Possibly better
33+
/// enum Test2 {
34+
/// A(i32),
35+
/// B(Box<[i32; 8000]>),
36+
/// }
2937
/// ```
3038
pub LARGE_ENUM_VARIANT,
3139
perf,
@@ -84,12 +92,21 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for LargeEnumVariant {
8492
if difference > self.maximum_size_difference_allowed {
8593
let (i, variant) = largest.1;
8694

95+
let help_text = "consider boxing the large fields to reduce the total size of the enum";
8796
span_lint_and_then(
8897
cx,
8998
LARGE_ENUM_VARIANT,
9099
def.variants[i].span,
91100
"large size difference between variants",
92101
|db| {
102+
db.span_label(
103+
def.variants[(largest.1).0].span,
104+
&format!("this variant is {} bytes", largest.0),
105+
);
106+
db.span_note(
107+
def.variants[(second.1).0].span,
108+
&format!("and the second-largest variant is {} bytes:", second.0),
109+
);
93110
if variant.fields.len() == 1 {
94111
let span = match def.variants[i].data {
95112
VariantData::Struct(ref fields, ..) | VariantData::Tuple(ref fields, ..) => {
@@ -100,18 +117,14 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for LargeEnumVariant {
100117
if let Some(snip) = snippet_opt(cx, span) {
101118
db.span_suggestion(
102119
span,
103-
"consider boxing the large fields to reduce the total size of the \
104-
enum",
120+
help_text,
105121
format!("Box<{}>", snip),
106122
Applicability::MaybeIncorrect,
107123
);
108124
return;
109125
}
110126
}
111-
db.span_help(
112-
def.variants[i].span,
113-
"consider boxing the large fields to reduce the total size of the enum",
114-
);
127+
db.span_help(def.variants[i].span, help_text);
115128
},
116129
);
117130
}

tests/ui/large_enum_variant.stderr

+24-4
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@ error: large size difference between variants
22
--> $DIR/large_enum_variant.rs:7:5
33
|
44
LL | B([i32; 8000]),
5-
| ^^^^^^^^^^^^^^
5+
| ^^^^^^^^^^^^^^ this variant is 32000 bytes
66
|
77
= note: `-D clippy::large-enum-variant` implied by `-D warnings`
8+
note: and the second-largest variant is 4 bytes:
9+
--> $DIR/large_enum_variant.rs:6:5
10+
|
11+
LL | A(i32),
12+
| ^^^^^^
813
help: consider boxing the large fields to reduce the total size of the enum
914
|
1015
LL | B(Box<[i32; 8000]>),
@@ -14,8 +19,13 @@ error: large size difference between variants
1419
--> $DIR/large_enum_variant.rs:31:5
1520
|
1621
LL | ContainingLargeEnum(LargeEnum),
17-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
22+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this variant is 32004 bytes
23+
|
24+
note: and the second-largest variant is 8 bytes:
25+
--> $DIR/large_enum_variant.rs:30:5
1826
|
27+
LL | VariantOk(i32, u32),
28+
| ^^^^^^^^^^^^^^^^^^^
1929
help: consider boxing the large fields to reduce the total size of the enum
2030
|
2131
LL | ContainingLargeEnum(Box<LargeEnum>),
@@ -25,8 +35,13 @@ error: large size difference between variants
2535
--> $DIR/large_enum_variant.rs:41:5
2636
|
2737
LL | StructLikeLarge { x: [i32; 8000], y: i32 },
28-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
38+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this variant is 32004 bytes
2939
|
40+
note: and the second-largest variant is 8 bytes:
41+
--> $DIR/large_enum_variant.rs:40:5
42+
|
43+
LL | VariantOk(i32, u32),
44+
| ^^^^^^^^^^^^^^^^^^^
3045
help: consider boxing the large fields to reduce the total size of the enum
3146
--> $DIR/large_enum_variant.rs:41:5
3247
|
@@ -37,8 +52,13 @@ error: large size difference between variants
3752
--> $DIR/large_enum_variant.rs:46:5
3853
|
3954
LL | StructLikeLarge2 { x: [i32; 8000] },
40-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
55+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this variant is 32000 bytes
56+
|
57+
note: and the second-largest variant is 8 bytes:
58+
--> $DIR/large_enum_variant.rs:45:5
4159
|
60+
LL | VariantOk(i32, u32),
61+
| ^^^^^^^^^^^^^^^^^^^
4262
help: consider boxing the large fields to reduce the total size of the enum
4363
|
4464
LL | StructLikeLarge2 { x: Box<[i32; 8000]> },

0 commit comments

Comments
 (0)