Skip to content

Commit 9bda699

Browse files
committed
improve messages and add suggestions
1 parent 82dd50d commit 9bda699

File tree

2 files changed

+46
-11
lines changed

2 files changed

+46
-11
lines changed

clippy_lints/src/large_enum_variant.rs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
33
use rustc::lint::*;
44
use rustc::hir::*;
5-
use utils::span_help_and_lint;
5+
use utils::{span_lint_and_then, snippet_opt};
66
use rustc::ty::layout::TargetDataLayout;
77
use rustc::ty::TypeFoldable;
88
use rustc::traits::Reveal;
99

10-
/// **What it does:** Checks for large variants on enums.
10+
/// **What it does:** Checks for large variants on `enum`s.
1111
///
1212
/// **Why is this bad?** Enum size is bounded by the largest variant. Having a large variant
1313
/// can penalize the memory layout of that enum.
@@ -68,11 +68,31 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for LargeEnumVariant {
6868
})
6969
.sum();
7070
if size > self.maximum_variant_size_allowed {
71-
span_help_and_lint(cx,
71+
span_lint_and_then(cx,
7272
LARGE_ENUM_VARIANT,
7373
def.variants[i].span,
74-
&format!("large enum variant found on variant `{}`", variant.name),
75-
"consider boxing the large branches to reduce the total size of the enum");
74+
"large enum variant found",
75+
|db| {
76+
if variant.fields.len() == 1 {
77+
let span = match def.variants[i].node.data {
78+
VariantData::Struct(ref fields, _) |
79+
VariantData::Tuple(ref fields, _) => fields[0].ty.span,
80+
VariantData::Unit(_) => unreachable!(),
81+
};
82+
if let Some(snip) = snippet_opt(cx, span) {
83+
db.span_suggestion(
84+
span,
85+
"consider boxing the large fields to reduce the total size of the enum",
86+
format!("Box<{}>", snip),
87+
);
88+
return;
89+
}
90+
}
91+
db.span_help(
92+
def.variants[i].span,
93+
"consider boxing the large fields to reduce the total size of the enum",
94+
);
95+
});
7696
}
7797
});
7898
}

tests/compile-fail/large_enum_variant.rs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,19 @@
77

88
enum LargeEnum {
99
A(i32),
10-
B([i32; 8000]), //~ ERROR large enum variant found on variant `B`
10+
B([i32; 8000]), //~ ERROR large enum variant found
11+
//~^ HELP consider boxing the large fields to reduce the total size of the enum
12+
//~| SUGGESTION Box<[i32; 8000]>
1113
}
1214

1315
enum GenericEnum<T> {
1416
A(i32),
15-
B([i32; 8000]), //~ ERROR large enum variant found on variant `B`
17+
B([i32; 8000]), //~ ERROR large enum variant found
18+
//~^ HELP consider boxing the large fields to reduce the total size of the enum
19+
//~| SUGGESTION Box<[i32; 8000]>
1620
C([T; 8000]),
17-
D(T, [i32; 8000]), //~ ERROR large enum variant found on variant `D`
21+
D(T, [i32; 8000]), //~ ERROR large enum variant found
22+
//~^ HELP consider boxing the large fields to reduce the total size of the enum
1823
}
1924

2025
trait SomeTrait {
@@ -27,11 +32,21 @@ enum LargeEnumGeneric<A: SomeTrait> {
2732

2833
enum AnotherLargeEnum {
2934
VariantOk(i32, u32),
30-
ContainingLargeEnum(LargeEnum), //~ ERROR large enum variant found on variant `ContainingLargeEnum`
31-
ContainingMoreThanOneField(i32, [i32; 8000], [i32; 9500]), //~ ERROR large enum variant found on variant `ContainingMoreThanOneField`
35+
ContainingLargeEnum(LargeEnum), //~ ERROR large enum variant found
36+
//~^ HELP consider boxing the large fields to reduce the total size of the enum
37+
//~| SUGGESTION Box<LargeEnum>
38+
ContainingMoreThanOneField(i32, [i32; 8000], [i32; 9500]), //~ ERROR large enum variant found
39+
//~^ HELP consider boxing the large fields to reduce the total size of the enum
3240
VoidVariant,
3341
StructLikeLittle { x: i32, y: i32 },
34-
StructLikeLarge { x: [i32; 8000], y: i32 }, //~ ERROR large enum variant found on variant `StructLikeLarge`
42+
StructLikeLarge { x: [i32; 8000], y: i32 }, //~ ERROR large enum variant found
43+
//~^ HELP consider boxing the large fields to reduce the total size of the enum
44+
StructLikeLarge2 {
45+
x:
46+
[i32; 8000] //~ SUGGESTION Box<[i32; 8000]>
47+
},
48+
//~^ ERROR large enum variant found
49+
//~^ HELP consider boxing the large fields to reduce the total size of the enum
3550
}
3651

3752
fn main() {

0 commit comments

Comments
 (0)