Skip to content

Commit 51947df

Browse files
committed
Auto merge of #6485 - phansch:macro-check-large-enum-variant, r=llogiq
Don't trigger large_enum_variant in external macros Closes #1776 (the potential JSON output issue is not something we can fix in Clippy and I can't reproduce it anymore) changelog: Don't trigger [`large_enum_variant`] in external macros
2 parents 400d40b + 12bd244 commit 51947df

File tree

4 files changed

+31
-10
lines changed

4 files changed

+31
-10
lines changed

clippy_lints/src/large_enum_variant.rs

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::utils::{snippet_opt, span_lint_and_then};
44
use rustc_errors::Applicability;
55
use rustc_hir::{Item, ItemKind, VariantData};
66
use rustc_lint::{LateContext, LateLintPass};
7+
use rustc_middle::lint::in_external_macro;
78
use rustc_session::{declare_tool_lint, impl_lint_pass};
89
use rustc_target::abi::LayoutOf;
910

@@ -58,6 +59,9 @@ impl_lint_pass!(LargeEnumVariant => [LARGE_ENUM_VARIANT]);
5859

5960
impl<'tcx> LateLintPass<'tcx> for LargeEnumVariant {
6061
fn check_item(&mut self, cx: &LateContext<'_>, item: &Item<'_>) {
62+
if in_external_macro(cx.tcx.sess, item.span) {
63+
return;
64+
}
6165
let did = cx.tcx.hir().local_def_id(item.hir_id);
6266
if let ItemKind::Enum(ref def, _) = item.kind {
6367
let ty = cx.tcx.type_of(did);

tests/ui/auxiliary/macro_rules.rs

+10
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,13 @@ macro_rules! as_conv {
8484
0u32 as u64
8585
};
8686
}
87+
88+
#[macro_export]
89+
macro_rules! large_enum_variant {
90+
() => {
91+
enum LargeEnumInMacro {
92+
A(i32),
93+
B([i32; 8000]),
94+
}
95+
};
96+
}

tests/ui/large_enum_variant.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1+
// aux-build:macro_rules.rs
2+
13
#![allow(dead_code)]
24
#![allow(unused_variables)]
35
#![warn(clippy::large_enum_variant)]
46

7+
#[macro_use]
8+
extern crate macro_rules;
9+
510
enum LargeEnum {
611
A(i32),
712
B([i32; 8000]),
@@ -51,4 +56,6 @@ enum LargeEnumOk {
5156
LargeB([i32; 8001]),
5257
}
5358

54-
fn main() {}
59+
fn main() {
60+
large_enum_variant!();
61+
}

tests/ui/large_enum_variant.stderr

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
error: large size difference between variants
2-
--> $DIR/large_enum_variant.rs:7:5
2+
--> $DIR/large_enum_variant.rs:12:5
33
|
44
LL | B([i32; 8000]),
55
| ^^^^^^^^^^^^^^ this variant is 32000 bytes
66
|
77
= note: `-D clippy::large-enum-variant` implied by `-D warnings`
88
note: and the second-largest variant is 4 bytes:
9-
--> $DIR/large_enum_variant.rs:6:5
9+
--> $DIR/large_enum_variant.rs:11:5
1010
|
1111
LL | A(i32),
1212
| ^^^^^^
@@ -16,13 +16,13 @@ LL | B(Box<[i32; 8000]>),
1616
| ^^^^^^^^^^^^^^^^
1717

1818
error: large size difference between variants
19-
--> $DIR/large_enum_variant.rs:31:5
19+
--> $DIR/large_enum_variant.rs:36:5
2020
|
2121
LL | ContainingLargeEnum(LargeEnum),
2222
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this variant is 32004 bytes
2323
|
2424
note: and the second-largest variant is 8 bytes:
25-
--> $DIR/large_enum_variant.rs:30:5
25+
--> $DIR/large_enum_variant.rs:35:5
2626
|
2727
LL | VariantOk(i32, u32),
2828
| ^^^^^^^^^^^^^^^^^^^
@@ -32,30 +32,30 @@ LL | ContainingLargeEnum(Box<LargeEnum>),
3232
| ^^^^^^^^^^^^^^
3333

3434
error: large size difference between variants
35-
--> $DIR/large_enum_variant.rs:41:5
35+
--> $DIR/large_enum_variant.rs:46:5
3636
|
3737
LL | StructLikeLarge { x: [i32; 8000], y: i32 },
3838
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this variant is 32004 bytes
3939
|
4040
note: and the second-largest variant is 8 bytes:
41-
--> $DIR/large_enum_variant.rs:40:5
41+
--> $DIR/large_enum_variant.rs:45:5
4242
|
4343
LL | VariantOk(i32, u32),
4444
| ^^^^^^^^^^^^^^^^^^^
4545
help: consider boxing the large fields to reduce the total size of the enum
46-
--> $DIR/large_enum_variant.rs:41:5
46+
--> $DIR/large_enum_variant.rs:46:5
4747
|
4848
LL | StructLikeLarge { x: [i32; 8000], y: i32 },
4949
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5050

5151
error: large size difference between variants
52-
--> $DIR/large_enum_variant.rs:46:5
52+
--> $DIR/large_enum_variant.rs:51:5
5353
|
5454
LL | StructLikeLarge2 { x: [i32; 8000] },
5555
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this variant is 32000 bytes
5656
|
5757
note: and the second-largest variant is 8 bytes:
58-
--> $DIR/large_enum_variant.rs:45:5
58+
--> $DIR/large_enum_variant.rs:50:5
5959
|
6060
LL | VariantOk(i32, u32),
6161
| ^^^^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)