Skip to content

Commit 430bf33

Browse files
committed
edit
1 parent f2ab9bb commit 430bf33

File tree

1 file changed

+60
-60
lines changed

1 file changed

+60
-60
lines changed

clippy_lints/src/large_enum_variant.rs

Lines changed: 60 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,9 @@ impl<'tcx> LateLintPass<'tcx> for LargeEnumVariant {
8080
if let ItemKind::Enum(ref def, _) = item.kind {
8181
let ty = cx.tcx.type_of(item.def_id);
8282
let adt = ty.ty_adt_def().expect("already checked whether this is an enum");
83-
83+
if adt.variants.len() <= 1 {
84+
return;
85+
}
8486
let mut variants_size: Vec<VariantInfo> = adt
8587
.variants
8688
.iter()
@@ -110,69 +112,67 @@ impl<'tcx> LateLintPass<'tcx> for LargeEnumVariant {
110112
})
111113
.collect();
112114

113-
if variants_size.len() >= 2 {
114-
variants_size.sort_by(|a, b| (b.size.cmp(&a.size)));
115+
variants_size.sort_by(|a, b| (b.size.cmp(&a.size)));
115116

116-
let mut difference = variants_size[0].size - variants_size[1].size;
117-
if difference > self.maximum_size_difference_allowed {
118-
let help_text = "consider boxing the large fields to reduce the total size of the enum";
119-
span_lint_and_then(
120-
cx,
121-
LARGE_ENUM_VARIANT,
122-
def.variants[variants_size[0].ind].span,
123-
"large size difference between variants",
124-
|diag| {
125-
diag.span_label(
126-
def.variants[variants_size[0].ind].span,
127-
&format!("this variant is {} bytes", variants_size[0].size),
128-
);
129-
diag.span_note(
130-
def.variants[variants_size[1].ind].span,
131-
&format!("and the second-largest variant is {} bytes:", variants_size[1].size),
132-
);
117+
let mut difference = variants_size[0].size - variants_size[1].size;
118+
if difference > self.maximum_size_difference_allowed {
119+
let help_text = "consider boxing the large fields to reduce the total size of the enum";
120+
span_lint_and_then(
121+
cx,
122+
LARGE_ENUM_VARIANT,
123+
def.variants[variants_size[0].ind].span,
124+
"large size difference between variants",
125+
|diag| {
126+
diag.span_label(
127+
def.variants[variants_size[0].ind].span,
128+
&format!("this variant is {} bytes", variants_size[0].size),
129+
);
130+
diag.span_note(
131+
def.variants[variants_size[1].ind].span,
132+
&format!("and the second-largest variant is {} bytes:", variants_size[1].size),
133+
);
133134

134-
let fields = def.variants[variants_size[0].ind].data.fields();
135-
variants_size[0].fields_size.sort_by(|a, b| (a.size.cmp(&b.size)));
136-
let mut applicability = Applicability::MaybeIncorrect;
137-
let sugg: Vec<(Span, String)> = variants_size[0]
138-
.fields_size
139-
.iter()
140-
.rev()
141-
.take_while(|val| {
142-
let judge = difference > self.maximum_size_difference_allowed;
143-
difference = if difference > val.size {
144-
difference - val.size
145-
} else {
146-
0
147-
};
148-
judge
149-
})
150-
.map(|val| {
151-
(
152-
fields[val.ind].ty.span,
153-
format!(
154-
"Box<{}>",
155-
snippet_with_applicability(
156-
cx,
157-
fields[val.ind].ty.span,
158-
"..",
159-
&mut applicability
160-
)
161-
.into_owned()
162-
),
163-
)
164-
})
165-
.collect();
135+
let fields = def.variants[variants_size[0].ind].data.fields();
136+
variants_size[0].fields_size.sort_by(|a, b| (a.size.cmp(&b.size)));
137+
let mut applicability = Applicability::MaybeIncorrect;
138+
let sugg: Vec<(Span, String)> = variants_size[0]
139+
.fields_size
140+
.iter()
141+
.rev()
142+
.take_while(|val| {
143+
let judge = difference > self.maximum_size_difference_allowed;
144+
difference = if difference > val.size {
145+
difference - val.size
146+
} else {
147+
0
148+
};
149+
judge
150+
})
151+
.map(|val| {
152+
(
153+
fields[val.ind].ty.span,
154+
format!(
155+
"Box<{}>",
156+
snippet_with_applicability(
157+
cx,
158+
fields[val.ind].ty.span,
159+
"..",
160+
&mut applicability
161+
)
162+
.into_owned()
163+
),
164+
)
165+
})
166+
.collect();
166167

167-
if !sugg.is_empty() {
168-
diag.multipart_suggestion(help_text, sugg, Applicability::MaybeIncorrect);
169-
return;
170-
}
168+
if !sugg.is_empty() {
169+
diag.multipart_suggestion(help_text, sugg, Applicability::MaybeIncorrect);
170+
return;
171+
}
171172

172-
diag.span_help(def.variants[variants_size[0].ind].span, help_text);
173-
},
174-
);
175-
}
173+
diag.span_help(def.variants[variants_size[0].ind].span, help_text);
174+
},
175+
);
176176
}
177177
}
178178
}

0 commit comments

Comments
 (0)