+impl<'tcx> LateLintPass<'tcx> for LargeEnumVariant {
+ fn check_item(&mut self, cx: &LateContext<'tcx>, item: &Item<'tcx>) {
+ if in_external_macro(cx.tcx.sess, item.span) {
+ return;
+ }
+ if let ItemKind::Enum(ref def, _) = item.kind {
+ let ty = cx.tcx.type_of(item.def_id);
+ let adt = ty.ty_adt_def().expect("already checked whether this is an enum");
+ if adt.variants().len() <= 1 {
+ return;
+ }
+ let mut variants_size: Vec<VariantInfo> = Vec::new();
+ for (i, variant) in adt.variants().iter().enumerate() {
+ let mut fields_size = Vec::new();
+ for (i, f) in variant.fields.iter().enumerate() {
+ let ty = cx.tcx.type_of(f.did);
+ // don't lint variants which have a field of generic type.
+ match cx.layout_of(ty) {
+ Ok(l) => {
+ let fsize = l.size.bytes();
+ fields_size.push(FieldInfo { ind: i, size: fsize });
+ },
+ Err(_) => {
+ return;
+ },
+ }
+ }
+ let size: u64 = fields_size.iter().map(|info| info.size).sum();