Skip to content

Commit

Permalink
Don't trigger large_enum_variant in external macros
Browse files Browse the repository at this point in the history
  • Loading branch information
phansch committed Dec 21, 2020
1 parent 4911ab1 commit 12bd244
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 10 deletions.
4 changes: 4 additions & 0 deletions clippy_lints/src/large_enum_variant.rs
Expand Up @@ -4,6 +4,7 @@ use crate::utils::{snippet_opt, span_lint_and_then};
use rustc_errors::Applicability;
use rustc_hir::{Item, ItemKind, VariantData};
use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::lint::in_external_macro;
use rustc_session::{declare_tool_lint, impl_lint_pass};
use rustc_target::abi::LayoutOf;

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

impl<'tcx> LateLintPass<'tcx> for LargeEnumVariant {
fn check_item(&mut self, cx: &LateContext<'_>, item: &Item<'_>) {
if in_external_macro(cx.tcx.sess, item.span) {
return;
}
let did = cx.tcx.hir().local_def_id(item.hir_id);
if let ItemKind::Enum(ref def, _) = item.kind {
let ty = cx.tcx.type_of(did);
Expand Down
10 changes: 10 additions & 0 deletions tests/ui/auxiliary/macro_rules.rs
Expand Up @@ -84,3 +84,13 @@ macro_rules! as_conv {
0u32 as u64
};
}

#[macro_export]
macro_rules! large_enum_variant {
() => {
enum LargeEnumInMacro {
A(i32),
B([i32; 8000]),
}
};
}
9 changes: 8 additions & 1 deletion tests/ui/large_enum_variant.rs
@@ -1,7 +1,12 @@
// aux-build:macro_rules.rs

#![allow(dead_code)]
#![allow(unused_variables)]
#![warn(clippy::large_enum_variant)]

#[macro_use]
extern crate macro_rules;

enum LargeEnum {
A(i32),
B([i32; 8000]),
Expand Down Expand Up @@ -51,4 +56,6 @@ enum LargeEnumOk {
LargeB([i32; 8001]),
}

fn main() {}
fn main() {
large_enum_variant!();
}
18 changes: 9 additions & 9 deletions tests/ui/large_enum_variant.stderr
@@ -1,12 +1,12 @@
error: large size difference between variants
--> $DIR/large_enum_variant.rs:7:5
--> $DIR/large_enum_variant.rs:12:5
|
LL | B([i32; 8000]),
| ^^^^^^^^^^^^^^ this variant is 32000 bytes
|
= note: `-D clippy::large-enum-variant` implied by `-D warnings`
note: and the second-largest variant is 4 bytes:
--> $DIR/large_enum_variant.rs:6:5
--> $DIR/large_enum_variant.rs:11:5
|
LL | A(i32),
| ^^^^^^
Expand All @@ -16,13 +16,13 @@ LL | B(Box<[i32; 8000]>),
| ^^^^^^^^^^^^^^^^

error: large size difference between variants
--> $DIR/large_enum_variant.rs:31:5
--> $DIR/large_enum_variant.rs:36:5
|
LL | ContainingLargeEnum(LargeEnum),
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this variant is 32004 bytes
|
note: and the second-largest variant is 8 bytes:
--> $DIR/large_enum_variant.rs:30:5
--> $DIR/large_enum_variant.rs:35:5
|
LL | VariantOk(i32, u32),
| ^^^^^^^^^^^^^^^^^^^
Expand All @@ -32,30 +32,30 @@ LL | ContainingLargeEnum(Box<LargeEnum>),
| ^^^^^^^^^^^^^^

error: large size difference between variants
--> $DIR/large_enum_variant.rs:41:5
--> $DIR/large_enum_variant.rs:46:5
|
LL | StructLikeLarge { x: [i32; 8000], y: i32 },
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this variant is 32004 bytes
|
note: and the second-largest variant is 8 bytes:
--> $DIR/large_enum_variant.rs:40:5
--> $DIR/large_enum_variant.rs:45:5
|
LL | VariantOk(i32, u32),
| ^^^^^^^^^^^^^^^^^^^
help: consider boxing the large fields to reduce the total size of the enum
--> $DIR/large_enum_variant.rs:41:5
--> $DIR/large_enum_variant.rs:46:5
|
LL | StructLikeLarge { x: [i32; 8000], y: i32 },
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: large size difference between variants
--> $DIR/large_enum_variant.rs:46:5
--> $DIR/large_enum_variant.rs:51:5
|
LL | StructLikeLarge2 { x: [i32; 8000] },
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this variant is 32000 bytes
|
note: and the second-largest variant is 8 bytes:
--> $DIR/large_enum_variant.rs:45:5
--> $DIR/large_enum_variant.rs:50:5
|
LL | VariantOk(i32, u32),
| ^^^^^^^^^^^^^^^^^^^
Expand Down

0 comments on commit 12bd244

Please sign in to comment.