Skip to content

Fixed issue #15192 #15241

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions clippy_lints/src/large_enum_variant.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use clippy_config::Conf;
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::is_no_std_crate;
use clippy_utils::source::snippet_with_applicability;
use clippy_utils::ty::{AdtVariantInfo, approx_ty_size, is_copy};
use rustc_errors::Applicability;
Expand Down Expand Up @@ -83,7 +84,7 @@ impl<'tcx> LateLintPass<'tcx> for LargeEnumVariant {

let mut difference = variants_size[0].size - variants_size[1].size;
if difference > self.maximum_size_difference_allowed {
let help_text = "consider boxing the large fields to reduce the total size of the enum";
let help_text = "consider boxing the large fields or introducing indirection in some other way to reduce the total size of the enum";
span_lint_and_then(
cx,
LARGE_ENUM_VARIANT,
Expand Down Expand Up @@ -117,7 +118,7 @@ impl<'tcx> LateLintPass<'tcx> for LargeEnumVariant {
ident.span,
"boxing a variant would require the type no longer be `Copy`",
);
} else {
} else if !is_no_std_crate(cx) {
let sugg: Vec<(Span, String)> = variants_size[0]
.fields_size
.iter()
Expand Down Expand Up @@ -149,7 +150,9 @@ impl<'tcx> LateLintPass<'tcx> for LargeEnumVariant {
return;
}
}
diag.span_help(def.variants[variants_size[0].ind].span, help_text);
if !is_no_std_crate(cx) {
diag.span_help(def.variants[variants_size[0].ind].span, help_text);
}
},
);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/ui-toml/enum_variant_size/enum_variant_size.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ LL | | }
|
= note: `-D clippy::large-enum-variant` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::large_enum_variant)]`
help: consider boxing the large fields to reduce the total size of the enum
help: consider boxing the large fields or introducing indirection in some other way to reduce the total size of the enum
|
LL - B([u8; 501]),
LL + B(Box<[u8; 501]>),
Expand Down
40 changes: 20 additions & 20 deletions tests/ui/large_enum_variant.64bit.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ LL | | }
|
= note: `-D clippy::large-enum-variant` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::large_enum_variant)]`
help: consider boxing the large fields to reduce the total size of the enum
help: consider boxing the large fields or introducing indirection in some other way to reduce the total size of the enum
|
LL - B([i32; 8000]),
LL + B(Box<[i32; 8000]>),
Expand All @@ -30,7 +30,7 @@ LL | | ContainingLargeEnum(LargeEnum),
LL | | }
| |_^ the entire enum is at least 32004 bytes
|
help: consider boxing the large fields to reduce the total size of the enum
help: consider boxing the large fields or introducing indirection in some other way to reduce the total size of the enum
|
LL - ContainingLargeEnum(LargeEnum),
LL + ContainingLargeEnum(Box<LargeEnum>),
Expand All @@ -49,7 +49,7 @@ LL | | StructLikeLittle { x: i32, y: i32 },
LL | | }
| |_^ the entire enum is at least 70008 bytes
|
help: consider boxing the large fields to reduce the total size of the enum
help: consider boxing the large fields or introducing indirection in some other way to reduce the total size of the enum
|
LL - ContainingMoreThanOneField(i32, [i32; 8000], [i32; 9500]),
LL + ContainingMoreThanOneField(i32, Box<[i32; 8000]>, Box<[i32; 9500]>),
Expand All @@ -67,7 +67,7 @@ LL | | StructLikeLarge { x: [i32; 8000], y: i32 },
LL | | }
| |_^ the entire enum is at least 32008 bytes
|
help: consider boxing the large fields to reduce the total size of the enum
help: consider boxing the large fields or introducing indirection in some other way to reduce the total size of the enum
|
LL - StructLikeLarge { x: [i32; 8000], y: i32 },
LL + StructLikeLarge { x: Box<[i32; 8000]>, y: i32 },
Expand All @@ -85,7 +85,7 @@ LL | | StructLikeLarge2 { x: [i32; 8000] },
LL | | }
| |_^ the entire enum is at least 32004 bytes
|
help: consider boxing the large fields to reduce the total size of the enum
help: consider boxing the large fields or introducing indirection in some other way to reduce the total size of the enum
|
LL - StructLikeLarge2 { x: [i32; 8000] },
LL + StructLikeLarge2 { x: Box<[i32; 8000]> },
Expand All @@ -104,7 +104,7 @@ LL | | C([u8; 200]),
LL | | }
| |_^ the entire enum is at least 1256 bytes
|
help: consider boxing the large fields to reduce the total size of the enum
help: consider boxing the large fields or introducing indirection in some other way to reduce the total size of the enum
|
LL - B([u8; 1255]),
LL + B(Box<[u8; 1255]>),
Expand All @@ -122,7 +122,7 @@ LL | | ContainingMoreThanOneField([i32; 8000], [i32; 2], [i32; 9500], [i32;
LL | | }
| |_^ the entire enum is at least 70132 bytes
|
help: consider boxing the large fields to reduce the total size of the enum
help: consider boxing the large fields or introducing indirection in some other way to reduce the total size of the enum
|
LL - ContainingMoreThanOneField([i32; 8000], [i32; 2], [i32; 9500], [i32; 30]),
LL + ContainingMoreThanOneField(Box<[i32; 8000]>, [i32; 2], Box<[i32; 9500]>, [i32; 30]),
Expand All @@ -140,7 +140,7 @@ LL | | B(Struct2),
LL | | }
| |_^ the entire enum is at least 32004 bytes
|
help: consider boxing the large fields to reduce the total size of the enum
help: consider boxing the large fields or introducing indirection in some other way to reduce the total size of the enum
|
LL - B(Struct2),
LL + B(Box<Struct2>),
Expand All @@ -158,7 +158,7 @@ LL | | B(Struct2),
LL | | }
| |_^ the entire enum is at least 32000 bytes
|
help: consider boxing the large fields to reduce the total size of the enum
help: consider boxing the large fields or introducing indirection in some other way to reduce the total size of the enum
|
LL - B(Struct2),
LL + B(Box<Struct2>),
Expand All @@ -176,7 +176,7 @@ LL | | B(Struct2),
LL | | }
| |_^ the entire enum is at least 32000 bytes
|
help: consider boxing the large fields to reduce the total size of the enum
help: consider boxing the large fields or introducing indirection in some other way to reduce the total size of the enum
|
LL - B(Struct2),
LL + B(Box<Struct2>),
Expand All @@ -199,7 +199,7 @@ note: boxing a variant would require the type no longer be `Copy`
|
LL | enum CopyableLargeEnum {
| ^^^^^^^^^^^^^^^^^
help: consider boxing the large fields to reduce the total size of the enum
help: consider boxing the large fields or introducing indirection in some other way to reduce the total size of the enum
--> tests/ui/large_enum_variant.rs:118:5
|
LL | B([u64; 8000]),
Expand All @@ -222,7 +222,7 @@ note: boxing a variant would require the type no longer be `Copy`
|
LL | enum ManuallyCopyLargeEnum {
| ^^^^^^^^^^^^^^^^^^^^^
help: consider boxing the large fields to reduce the total size of the enum
help: consider boxing the large fields or introducing indirection in some other way to reduce the total size of the enum
--> tests/ui/large_enum_variant.rs:124:5
|
LL | B([u64; 8000]),
Expand All @@ -245,7 +245,7 @@ note: boxing a variant would require the type no longer be `Copy`
|
LL | enum SomeGenericPossiblyCopyEnum<T> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: consider boxing the large fields to reduce the total size of the enum
help: consider boxing the large fields or introducing indirection in some other way to reduce the total size of the enum
--> tests/ui/large_enum_variant.rs:138:5
|
LL | B([u64; 4000]),
Expand All @@ -263,7 +263,7 @@ LL | | Large((T, [u8; 512])),
LL | | }
| |_^ the entire enum is at least 512 bytes
|
help: consider boxing the large fields to reduce the total size of the enum
help: consider boxing the large fields or introducing indirection in some other way to reduce the total size of the enum
|
LL - Large((T, [u8; 512])),
LL + Large(Box<(T, [u8; 512])>),
Expand All @@ -281,7 +281,7 @@ LL | | Small(u8),
LL | | }
| |_^ the entire enum is at least 520 bytes
|
help: consider boxing the large fields to reduce the total size of the enum
help: consider boxing the large fields or introducing indirection in some other way to reduce the total size of the enum
|
LL - Large([Foo<u64>; 64]),
LL + Large(Box<[Foo<u64>; 64]>),
Expand All @@ -299,7 +299,7 @@ LL | | Error(PossiblyLargeEnumWithConst<256>),
LL | | }
| |_^ the entire enum is at least 514 bytes
|
help: consider boxing the large fields to reduce the total size of the enum
help: consider boxing the large fields or introducing indirection in some other way to reduce the total size of the enum
|
LL - Error(PossiblyLargeEnumWithConst<256>),
LL + Error(Box<PossiblyLargeEnumWithConst<256>>),
Expand All @@ -317,7 +317,7 @@ LL | | Recursive(Box<WithRecursion>),
LL | | }
| |_^ the entire enum is at least 520 bytes
|
help: consider boxing the large fields to reduce the total size of the enum
help: consider boxing the large fields or introducing indirection in some other way to reduce the total size of the enum
|
LL - Large([u64; 64]),
LL + Large(Box<[u64; 64]>),
Expand All @@ -335,7 +335,7 @@ LL | | Error(WithRecursionAndGenerics<u64>),
LL | | }
| |_^ the entire enum is at least 520 bytes
|
help: consider boxing the large fields to reduce the total size of the enum
help: consider boxing the large fields or introducing indirection in some other way to reduce the total size of the enum
|
LL - Error(WithRecursionAndGenerics<u64>),
LL + Error(Box<WithRecursionAndGenerics<u64>>),
Expand All @@ -353,7 +353,7 @@ LL | | _SmallBoi(u8),
LL | | }
| |_____^ the entire enum is at least 296 bytes
|
help: consider boxing the large fields to reduce the total size of the enum
help: consider boxing the large fields or introducing indirection in some other way to reduce the total size of the enum
|
LL - BigBoi(PublishWithBytes),
LL + BigBoi(Box<PublishWithBytes>),
Expand All @@ -371,7 +371,7 @@ LL | | _SmallBoi(u8),
LL | | }
| |_____^ the entire enum is at least 224 bytes
|
help: consider boxing the large fields to reduce the total size of the enum
help: consider boxing the large fields or introducing indirection in some other way to reduce the total size of the enum
|
LL - BigBoi(PublishWithVec),
LL + BigBoi(Box<PublishWithVec>),
Expand Down
8 changes: 8 additions & 0 deletions tests/ui/large_enum_variant_no_std.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#![no_std]
#![warn(clippy::large_enum_variant)]

enum Myenum {
//~^ ERROR: large size difference between variants
Small(u8),
Large([u8; 1024]),
}
17 changes: 17 additions & 0 deletions tests/ui/large_enum_variant_no_std.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
error: large size difference between variants
--> tests/ui/large_enum_variant_no_std.rs:4:1
|
LL | / enum Myenum {
LL | |
LL | | Small(u8),
| | --------- the second-largest variant contains at least 1 bytes
LL | | Large([u8; 1024]),
| | ----------------- the largest variant contains at least 1024 bytes
LL | | }
| |_^ the entire enum is at least 1025 bytes
|
= note: `-D clippy::large-enum-variant` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::large_enum_variant)]`

error: aborting due to 1 previous error