Skip to content

Commit 9a5fca7

Browse files
committed
Fixed issue #15192
Fixed issue #15192 by adding checks for no_std whild giving out Box recommendation
1 parent 49ca220 commit 9a5fca7

File tree

3 files changed

+45
-6
lines changed

3 files changed

+45
-6
lines changed

clippy_lints/src/large_enum_variant.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ use rustc_lint::{LateContext, LateLintPass};
88
use rustc_middle::ty::{self, Ty};
99
use rustc_session::impl_lint_pass;
1010
use rustc_span::Span;
11+
use clippy_utils::is_no_std_crate;
12+
13+
1114

1215
declare_clippy_lint! {
1316
/// ### What it does
@@ -117,8 +120,9 @@ impl<'tcx> LateLintPass<'tcx> for LargeEnumVariant {
117120
ident.span,
118121
"boxing a variant would require the type no longer be `Copy`",
119122
);
120-
} else {
121-
let sugg: Vec<(Span, String)> = variants_size[0]
123+
} else if !is_no_std_crate(cx) {
124+
125+
let sugg: Vec<(Span, String)> = variants_size[0]
122126
.fields_size
123127
.iter()
124128
.rev()
@@ -144,12 +148,16 @@ impl<'tcx> LateLintPass<'tcx> for LargeEnumVariant {
144148
})
145149
.collect();
146150

147-
if !sugg.is_empty() {
148-
diag.multipart_suggestion(help_text, sugg, Applicability::MaybeIncorrect);
149-
return;
151+
if !sugg.is_empty() {
152+
diag.multipart_suggestion(help_text, sugg, Applicability::MaybeIncorrect);
153+
return;
154+
}
150155
}
156+
157+
if !is_no_std_crate(cx) {
158+
diag.span_help(def.variants[variants_size[0].ind].span, help_text);
151159
}
152-
diag.span_help(def.variants[variants_size[0].ind].span, help_text);
160+
153161
},
154162
);
155163
}

tests/ui/large_enum_variant_no_std.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#![no_std]
2+
#![no_main]
3+
#![warn(clippy::large_enum_variant)]
4+
5+
use core::panic::PanicInfo;
6+
7+
#[panic_handler]
8+
fn panic(_: &PanicInfo) -> ! {
9+
loop {}
10+
}
11+
12+
enum Myenum { //~ ERROR: large size difference between variants
13+
Small(u8),
14+
Large([u8;1024]),
15+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error: large size difference between variants
2+
--> tests/ui/large_enum_variant_no_std.rs:12:1
3+
|
4+
LL | / enum Myenum {
5+
LL | | Small(u8),
6+
| | --------- the second-largest variant contains at least 1 bytes
7+
LL | | Large([u8;1024]),
8+
| | ---------------- the largest variant contains at least 1024 bytes
9+
LL | | }
10+
| |_^ the entire enum is at least 1025 bytes
11+
|
12+
= note: `-D clippy::large-enum-variant` implied by `-D warnings`
13+
= help: to override `-D warnings` add `#[allow(clippy::large_enum_variant)]`
14+
15+
error: aborting due to 1 previous error
16+

0 commit comments

Comments
 (0)