Skip to content

Commit dd0505c

Browse files
Calculate the 2 largest variants using iterators
No need to store all sizes in a vector
1 parent 66fb62b commit dd0505c

File tree

1 file changed

+22
-21
lines changed

1 file changed

+22
-21
lines changed

src/librustc_lint/types.rs

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -697,7 +697,6 @@ impl LateLintPass for VariantSizeDifferences {
697697
fn check_item(&mut self, cx: &LateContext, it: &hir::Item) {
698698
if let hir::ItemEnum(ref enum_definition, ref gens) = it.node {
699699
if gens.ty_params.is_empty() { // sizes only make sense for non-generic types
700-
let mut sizes = vec![];
701700
let t = cx.tcx.node_id_to_type(it.id);
702701
let layout = cx.tcx.normalizing_infer_ctxt(ProjectionMode::Any).enter(|infcx| {
703702
t.layout(&infcx).unwrap_or_else(|e| {
@@ -710,26 +709,28 @@ impl LateLintPass for VariantSizeDifferences {
710709

711710
debug!("enum `{}` is {} bytes large", t, size.bytes());
712711

713-
for (variant, variant_layout) in enum_definition.variants.iter().zip(variants) {
714-
// Subtract the size of the enum discriminant
715-
let bytes = variant_layout.min_size().bytes().saturating_sub(discr_size);
716-
sizes.push(bytes);
717-
718-
debug!("- variant `{}` is {} bytes large", variant.node.name, bytes);
719-
}
720-
721-
let (largest, slargest, largest_index) = sizes.iter()
722-
.enumerate()
723-
.fold((0, 0, 0),
724-
|(l, s, li), (idx, &size)|
725-
if size > l {
726-
(size, l, idx)
727-
} else if size > s {
728-
(l, size, li)
729-
} else {
730-
(l, s, li)
731-
}
732-
);
712+
let (largest, slargest, largest_index) = enum_definition.variants
713+
.iter()
714+
.zip(variants)
715+
.map(|(variant, variant_layout)| {
716+
// Subtract the size of the enum discriminant
717+
let bytes = variant_layout.min_size().bytes()
718+
.saturating_sub(discr_size);
719+
720+
debug!("- variant `{}` is {} bytes large", variant.node.name, bytes);
721+
bytes
722+
})
723+
.enumerate()
724+
.fold((0, 0, 0),
725+
|(l, s, li), (idx, size)|
726+
if size > l {
727+
(size, l, idx)
728+
} else if size > s {
729+
(l, size, li)
730+
} else {
731+
(l, s, li)
732+
}
733+
);
733734

734735
// we only warn if the largest variant is at least thrice as large as
735736
// the second-largest.

0 commit comments

Comments
 (0)