1 //! lint when there is a large size difference between variants on an enum
3 use clippy_utils::diagnostics::span_lint_and_then;
4 use clippy_utils::source::snippet_with_applicability;
5 use rustc_errors::Applicability;
6 use rustc_hir::{Item, ItemKind};
7 use rustc_lint::{LateContext, LateLintPass};
8 use rustc_middle::lint::in_external_macro;
9 use rustc_middle::ty::layout::LayoutOf;
10 use rustc_session::{declare_tool_lint, impl_lint_pass};
11 use rustc_span::source_map::Span;
13 declare_clippy_lint! {
15 /// Checks for large size differences between variants on
18 /// ### Why is this bad?
19 /// Enum size is bounded by the largest variant. Having a
20 /// large variant can penalize the memory layout of that enum.
22 /// ### Known problems
23 /// This lint obviously cannot take the distribution of
24 /// variants in your running program into account. It is possible that the
25 /// smaller variants make up less than 1% of all instances, in which case
26 /// the overhead is negligible and the boxing is counter-productive. Always
27 /// measure the change this lint suggests.
37 /// // Possibly better
40 /// B(Box<[i32; 8000]>),
43 #[clippy::version = "pre 1.29.0"]
44 pub LARGE_ENUM_VARIANT,
46 "large size difference between variants on an enum"
49 #[derive(Copy, Clone)]
50 pub struct LargeEnumVariant {
51 maximum_size_difference_allowed: u64,
54 impl LargeEnumVariant {
56 pub fn new(maximum_size_difference_allowed: u64) -> Self {
58 maximum_size_difference_allowed,
71 fields_size: Vec<FieldInfo>,
74 impl_lint_pass!(LargeEnumVariant => [LARGE_ENUM_VARIANT]);
76 impl<'tcx> LateLintPass<'tcx> for LargeEnumVariant {
77 fn check_item(&mut self, cx: &LateContext<'_>, item: &Item<'_>) {
78 if in_external_macro(cx.tcx.sess, item.span) {
81 if let ItemKind::Enum(ref def, _) = item.kind {
82 let ty = cx.tcx.type_of(item.def_id);
83 let adt = ty.ty_adt_def().expect("already checked whether this is an enum");
84 if adt.variants.len() <= 1 {
87 let mut variants_size: Vec<VariantInfo> = adt
92 let mut fields_size = Vec::new();
93 let size: u64 = variant
97 .filter_map(|(i, f)| {
98 let ty = cx.tcx.type_of(f.did);
99 // don't count generics by filtering out everything
100 // that does not have a layout
101 cx.layout_of(ty).ok().map(|l| {
102 let size = l.size.bytes();
103 fields_size.push(FieldInfo { ind: i, size });
116 variants_size.sort_by(|a, b| (b.size.cmp(&a.size)));
118 let mut difference = variants_size[0].size - variants_size[1].size;
119 if difference > self.maximum_size_difference_allowed {
120 let help_text = "consider boxing the large fields to reduce the total size of the enum";
124 def.variants[variants_size[0].ind].span,
125 "large size difference between variants",
128 def.variants[variants_size[0].ind].span,
129 &format!("this variant is {} bytes", variants_size[0].size),
132 def.variants[variants_size[1].ind].span,
133 &format!("and the second-largest variant is {} bytes:", variants_size[1].size),
136 let fields = def.variants[variants_size[0].ind].data.fields();
137 variants_size[0].fields_size.sort_by(|a, b| (a.size.cmp(&b.size)));
138 let mut applicability = Applicability::MaybeIncorrect;
139 let sugg: Vec<(Span, String)> = variants_size[0]
144 if difference > self.maximum_size_difference_allowed {
145 difference = difference.saturating_sub(val.size);
147 fields[val.ind].ty.span,
150 snippet_with_applicability(
152 fields[val.ind].ty.span,
165 if !sugg.is_empty() {
166 diag.multipart_suggestion(help_text, sugg, Applicability::MaybeIncorrect);
170 diag.span_help(def.variants[variants_size[0].ind].span, help_text);