1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
11 //! lint when there is a large size difference between variants on an enum
13 use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
14 use crate::rustc::{declare_tool_lint, lint_array};
15 use crate::rustc::hir::*;
16 use crate::utils::{snippet_opt, span_lint_and_then};
17 use crate::rustc::ty::layout::LayoutOf;
18 use crate::rustc_errors::Applicability;
20 /// **What it does:** Checks for large size differences between variants on
23 /// **Why is this bad?** Enum size is bounded by the largest variant. Having a
25 /// can penalize the memory layout of that enum.
27 /// **Known problems:** None.
36 declare_clippy_lint! {
37 pub LARGE_ENUM_VARIANT,
39 "large size difference between variants on an enum"
42 #[derive(Copy, Clone)]
43 pub struct LargeEnumVariant {
44 maximum_size_difference_allowed: u64,
47 impl LargeEnumVariant {
48 pub fn new(maximum_size_difference_allowed: u64) -> Self {
50 maximum_size_difference_allowed,
55 impl LintPass for LargeEnumVariant {
56 fn get_lints(&self) -> LintArray {
57 lint_array!(LARGE_ENUM_VARIANT)
61 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for LargeEnumVariant {
62 fn check_item(&mut self, cx: &LateContext<'_, '_>, item: &Item) {
63 let did = cx.tcx.hir.local_def_id(item.id);
64 if let ItemKind::Enum(ref def, _) = item.node {
65 let ty = cx.tcx.type_of(did);
66 let adt = ty.ty_adt_def()
67 .expect("already checked whether this is an enum");
69 let mut smallest_variant: Option<(_, _)> = None;
70 let mut largest_variant: Option<(_, _)> = None;
72 for (i, variant) in adt.variants.iter().enumerate() {
73 let size: u64 = variant
77 let ty = cx.tcx.type_of(f.did);
78 // don't count generics by filtering out everything
79 // that does not have a layout
80 cx.layout_of(ty).ok().map(|l| l.size.bytes())
84 let grouped = (size, (i, variant));
86 update_if(&mut smallest_variant, grouped, |a, b| b.0 <= a.0);
87 update_if(&mut largest_variant, grouped, |a, b| b.0 >= a.0);
90 if let (Some(smallest), Some(largest)) = (smallest_variant, largest_variant) {
91 let difference = largest.0 - smallest.0;
93 if difference > self.maximum_size_difference_allowed {
94 let (i, variant) = largest.1;
100 "large size difference between variants",
102 if variant.fields.len() == 1 {
103 let span = match def.variants[i].node.data {
104 VariantData::Struct(ref fields, _) | VariantData::Tuple(ref fields, _) => {
107 VariantData::Unit(_) => unreachable!(),
109 if let Some(snip) = snippet_opt(cx, span) {
110 db.span_suggestion_with_applicability(
112 "consider boxing the large fields to reduce the total size of the \
114 format!("Box<{}>", snip),
115 Applicability::MaybeIncorrect,
121 def.variants[i].span,
122 "consider boxing the large fields to reduce the total size of the enum",
132 fn update_if<T, F>(old: &mut Option<T>, new: T, f: F)
134 F: Fn(&T, &T) -> bool,
136 if let Some(ref mut val) = *old {