about summary refs log tree commit diff
diff options
context:
space:
mode:
authorlakshay bhatia <bhatia00lakshay@gmail.com>2025-07-10 19:31:15 +0530
committerlakshay bhatia <bhatia00lakshay@gmail.com>2025-07-20 13:51:45 +0530
commitb738d96325cc3214362e476bb1e38716c7c5e8d5 (patch)
treee5e4877bfe92e7d3b5010738aae096c3f850e4bd
parentba947c572886cfeca8109b980828b6c676cef717 (diff)
downloadrust-b738d96325cc3214362e476bb1e38716c7c5e8d5.tar.gz
rust-b738d96325cc3214362e476bb1e38716c7c5e8d5.zip
Improve Help Message in `large_enum_variant to satisfy` `#[no_std]`
- Updated the `help:` text to include a more general suggestion
- Ensures compatibility with `#[no_std]` environments where boxing is
  still possible via `alloc`.
-rw-r--r--clippy_lints/src/large_enum_variant.rs5
-rw-r--r--tests/ui-toml/enum_variant_size/enum_variant_size.stderr2
-rw-r--r--tests/ui/large_enum_variant.32bit.stderr36
-rw-r--r--tests/ui/large_enum_variant.64bit.stderr40
-rw-r--r--tests/ui/large_enum_variant_no_std.rs8
-rw-r--r--tests/ui/large_enum_variant_no_std.stderr22
6 files changed, 72 insertions, 41 deletions
diff --git a/clippy_lints/src/large_enum_variant.rs b/clippy_lints/src/large_enum_variant.rs
index e85d779b488..c2b73943106 100644
--- a/clippy_lints/src/large_enum_variant.rs
+++ b/clippy_lints/src/large_enum_variant.rs
@@ -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;
@@ -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,
@@ -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()
diff --git a/tests/ui-toml/enum_variant_size/enum_variant_size.stderr b/tests/ui-toml/enum_variant_size/enum_variant_size.stderr
index 020b3cc7878..a5dfd7015a3 100644
--- a/tests/ui-toml/enum_variant_size/enum_variant_size.stderr
+++ b/tests/ui-toml/enum_variant_size/enum_variant_size.stderr
@@ -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]>),
diff --git a/tests/ui/large_enum_variant.32bit.stderr b/tests/ui/large_enum_variant.32bit.stderr
index 80ca5daa1d5..ac1ed27a6b3 100644
--- a/tests/ui/large_enum_variant.32bit.stderr
+++ b/tests/ui/large_enum_variant.32bit.stderr
@@ -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]>),
@@ -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>),
@@ -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]>),
@@ -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 },
@@ -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]> },
@@ -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]>),
@@ -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]),
@@ -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>),
@@ -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>),
@@ -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>),
@@ -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]),
@@ -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]),
@@ -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]),
@@ -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])>),
@@ -281,7 +281,7 @@ LL | |     Small(u8),
 LL | | }
    | |_^ the entire enum is at least 516 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]>),
@@ -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>>),
@@ -317,7 +317,7 @@ LL | |     Recursive(Box<WithRecursion>),
 LL | | }
    | |_^ the entire enum is at least 516 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]>),
@@ -335,7 +335,7 @@ LL | |     Error(WithRecursionAndGenerics<u64>),
 LL | | }
    | |_^ the entire enum is at least 516 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>>),
diff --git a/tests/ui/large_enum_variant.64bit.stderr b/tests/ui/large_enum_variant.64bit.stderr
index 559bdf2a2f5..d8199f9090f 100644
--- a/tests/ui/large_enum_variant.64bit.stderr
+++ b/tests/ui/large_enum_variant.64bit.stderr
@@ -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]>),
@@ -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>),
@@ -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]>),
@@ -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 },
@@ -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]> },
@@ -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]>),
@@ -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]),
@@ -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>),
@@ -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>),
@@ -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>),
@@ -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]),
@@ -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]),
@@ -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]),
@@ -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])>),
@@ -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]>),
@@ -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>>),
@@ -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]>),
@@ -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>>),
@@ -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>),
@@ -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>),
diff --git a/tests/ui/large_enum_variant_no_std.rs b/tests/ui/large_enum_variant_no_std.rs
new file mode 100644
index 00000000000..ff0213155b6
--- /dev/null
+++ b/tests/ui/large_enum_variant_no_std.rs
@@ -0,0 +1,8 @@
+#![no_std]
+#![warn(clippy::large_enum_variant)]
+
+enum Myenum {
+    //~^ ERROR: large size difference between variants
+    Small(u8),
+    Large([u8; 1024]),
+}
diff --git a/tests/ui/large_enum_variant_no_std.stderr b/tests/ui/large_enum_variant_no_std.stderr
new file mode 100644
index 00000000000..4f32e3e4835
--- /dev/null
+++ b/tests/ui/large_enum_variant_no_std.stderr
@@ -0,0 +1,22 @@
+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
+   |
+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_no_std.rs:7:5
+   |
+LL |     Large([u8; 1024]),
+   |     ^^^^^^^^^^^^^^^^^
+   = 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
+