Skip to content

Commit 756caf7

Browse files
committed
account for generics
1 parent 19f94d5 commit 756caf7

File tree

3 files changed

+59
-3
lines changed

3 files changed

+59
-3
lines changed

clippy_lints/src/large_enum_variant.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use rustc_hir::{Item, ItemKind};
77
use rustc_lint::{LateContext, LateLintPass};
88
use rustc_middle::lint::in_external_macro;
99
use rustc_middle::ty::layout::LayoutOf;
10+
use rustc_middle::ty::{Adt, Ty};
1011
use rustc_session::{declare_tool_lint, impl_lint_pass};
1112
use rustc_span::source_map::Span;
1213

@@ -26,6 +27,15 @@ declare_clippy_lint! {
2627
/// the overhead is negligible and the boxing is counter-productive. Always
2728
/// measure the change this lint suggests.
2829
///
30+
/// For types that implement `Copy`, the suggestion to `Box` a variant's
31+
/// data would require removing the trait impl. The types can of course
32+
/// still be `Clone`, but that is worse ergonomically. Depending on the
33+
/// use case it may be possible to store the large data in an auxillary
34+
/// structure (e.g. Arena or ECS).
35+
///
36+
/// The lint will ignore generic types if the layout depends on the
37+
/// generics, even if the size difference will be large anyway.
38+
///
2939
/// ### Example
3040
/// ```rust
3141
/// // Bad
@@ -74,7 +84,7 @@ struct VariantInfo {
7484
impl_lint_pass!(LargeEnumVariant => [LARGE_ENUM_VARIANT]);
7585

7686
impl<'tcx> LateLintPass<'tcx> for LargeEnumVariant {
77-
fn check_item(&mut self, cx: &LateContext<'_>, item: &Item<'_>) {
87+
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &Item<'tcx>) {
7888
if in_external_macro(cx.tcx.sess, item.span) {
7989
return;
8090
}
@@ -132,7 +142,7 @@ impl<'tcx> LateLintPass<'tcx> for LargeEnumVariant {
132142
let fields = def.variants[variants_size[0].ind].data.fields();
133143
variants_size[0].fields_size.sort_by(|a, b| (a.size.cmp(&b.size)));
134144
let mut applicability = Applicability::MaybeIncorrect;
135-
if is_copy(cx, ty) {
145+
if is_copy(cx, ty) || maybe_copy(cx, ty) {
136146
diag.span_note(
137147
item.ident.span,
138148
"boxing a variant would require the type no longer be `Copy`",
@@ -176,3 +186,13 @@ impl<'tcx> LateLintPass<'tcx> for LargeEnumVariant {
176186
}
177187
}
178188
}
189+
190+
fn maybe_copy<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> bool {
191+
if let Adt(_def, substs) = ty.kind()
192+
&& substs.types().next().is_some()
193+
&& let Some(copy_trait) = cx.tcx.lang_items().copy_trait()
194+
{
195+
return cx.tcx.non_blanket_impls_for_ty(copy_trait, ty).next().is_some();
196+
}
197+
false
198+
}

tests/ui/large_enum_variant.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,22 @@ impl Clone for ManuallyCopyLargeEnum {
114114
*self
115115
}
116116
}
117+
117118
impl Copy for ManuallyCopyLargeEnum {}
118119

120+
enum SomeGenericPossiblyCopyEnum<T> {
121+
A(bool, std::marker::PhantomData<T>),
122+
B([u64; 4000]),
123+
}
124+
125+
impl<T: Copy> Clone for SomeGenericPossiblyCopyEnum<T> {
126+
fn clone(&self) -> Self {
127+
*self
128+
}
129+
}
130+
131+
impl<T: Copy> Copy for SomeGenericPossiblyCopyEnum<T> {}
132+
119133
fn main() {
120134
large_enum_variant!();
121135
}

tests/ui/large_enum_variant.stderr

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,5 +171,27 @@ help: consider boxing the large fields to reduce the total size of the enum
171171
LL | B([u128; 4000]),
172172
| ^^^^^^^^^^^^^^^
173173

174-
error: aborting due to 10 previous errors
174+
error: large size difference between variants
175+
--> $DIR/large_enum_variant.rs:122:5
176+
|
177+
LL | B([u64; 4000]),
178+
| ^^^^^^^^^^^^^^ this variant is 32000 bytes
179+
|
180+
note: and the second-largest variant is 1 bytes:
181+
--> $DIR/large_enum_variant.rs:121:5
182+
|
183+
LL | A(bool, std::marker::PhantomData<T>),
184+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
185+
note: boxing a variant would require the type no longer be `Copy`
186+
--> $DIR/large_enum_variant.rs:120:6
187+
|
188+
LL | enum SomeGenericPossiblyCopyEnum<T> {
189+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
190+
help: consider boxing the large fields to reduce the total size of the enum
191+
--> $DIR/large_enum_variant.rs:122:5
192+
|
193+
LL | B([u64; 4000]),
194+
| ^^^^^^^^^^^^^^
195+
196+
error: aborting due to 11 previous errors
175197

0 commit comments

Comments
 (0)