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
This commit is contained in:
bors 2020-12-21 23:49:51 +00:00
commit 51947df56f
4 changed files with 31 additions and 10 deletions

View File

@ -4,6 +4,7 @@
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;
@ -58,6 +59,9 @@ pub fn new(maximum_size_difference_allowed: u64) -> Self {
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);

View File

@ -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]),
}
};
}

View File

@ -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]),
@ -51,4 +56,6 @@ enum LargeEnumOk {
LargeB([i32; 8001]),
}
fn main() {}
fn main() {
large_enum_variant!();
}

View File

@ -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),
| ^^^^^^
@ -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),
| ^^^^^^^^^^^^^^^^^^^
@ -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),
| ^^^^^^^^^^^^^^^^^^^