1 //! lint when there is a large size difference between variants on an enum
5 use crate::utils::{snippet_opt, span_lint_and_then};
6 use rustc::ty::layout::LayoutOf;
8 /// **What it does:** Checks for large size differences between variants on
11 /// **Why is this bad?** Enum size is bounded by the largest variant. Having a
13 /// can penalize the memory layout of that enum.
15 /// **Known problems:** None.
24 declare_clippy_lint! {
25 pub LARGE_ENUM_VARIANT,
27 "large size difference between variants on an enum"
30 #[derive(Copy, Clone)]
31 pub struct LargeEnumVariant {
32 maximum_size_difference_allowed: u64,
35 impl LargeEnumVariant {
36 pub fn new(maximum_size_difference_allowed: u64) -> Self {
38 maximum_size_difference_allowed,
43 impl LintPass for LargeEnumVariant {
44 fn get_lints(&self) -> LintArray {
45 lint_array!(LARGE_ENUM_VARIANT)
49 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for LargeEnumVariant {
50 fn check_item(&mut self, cx: &LateContext, item: &Item) {
51 let did = cx.tcx.hir.local_def_id(item.id);
52 if let ItemEnum(ref def, _) = item.node {
53 let ty = cx.tcx.type_of(did);
54 let adt = ty.ty_adt_def()
55 .expect("already checked whether this is an enum");
57 let mut smallest_variant: Option<(_, _)> = None;
58 let mut largest_variant: Option<(_, _)> = None;
60 for (i, variant) in adt.variants.iter().enumerate() {
61 let size: u64 = variant
65 let ty = cx.tcx.type_of(f.did);
66 // don't count generics by filtering out everything
67 // that does not have a layout
68 cx.layout_of(ty).ok().map(|l| l.size.bytes())
72 let grouped = (size, (i, variant));
74 update_if(&mut smallest_variant, grouped, |a, b| b.0 <= a.0);
75 update_if(&mut largest_variant, grouped, |a, b| b.0 >= a.0);
78 if let (Some(smallest), Some(largest)) = (smallest_variant, largest_variant) {
79 let difference = largest.0 - smallest.0;
81 if difference > self.maximum_size_difference_allowed {
82 let (i, variant) = largest.1;
88 "large size difference between variants",
90 if variant.fields.len() == 1 {
91 let span = match def.variants[i].node.data {
92 VariantData::Struct(ref fields, _) | VariantData::Tuple(ref fields, _) => {
95 VariantData::Unit(_) => unreachable!(),
97 if let Some(snip) = snippet_opt(cx, span) {
100 "consider boxing the large fields to reduce the total size of the \
102 format!("Box<{}>", snip),
108 def.variants[i].span,
109 "consider boxing the large fields to reduce the total size of the enum",
119 fn update_if<T, F>(old: &mut Option<T>, new: T, f: F)
121 F: Fn(&T, &T) -> bool,
123 if let Some(ref mut val) = *old {