improve messages and add suggestions

This commit is contained in:
Oliver Schneider 2017-01-31 08:36:39 +01:00
parent 82dd50d0e2
commit 9bda699c80
2 changed files with 46 additions and 11 deletions

View file

@ -2,12 +2,12 @@
use rustc::lint::*;
use rustc::hir::*;
use utils::span_help_and_lint;
use utils::{span_lint_and_then, snippet_opt};
use rustc::ty::layout::TargetDataLayout;
use rustc::ty::TypeFoldable;
use rustc::traits::Reveal;
/// **What it does:** Checks for large variants on enums.
/// **What it does:** Checks for large variants on `enum`s.
///
/// **Why is this bad?** Enum size is bounded by the largest variant. Having a large variant
/// can penalize the memory layout of that enum.
@ -68,11 +68,31 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for LargeEnumVariant {
})
.sum();
if size > self.maximum_variant_size_allowed {
span_help_and_lint(cx,
span_lint_and_then(cx,
LARGE_ENUM_VARIANT,
def.variants[i].span,
&format!("large enum variant found on variant `{}`", variant.name),
"consider boxing the large branches to reduce the total size of the enum");
"large enum variant found",
|db| {
if variant.fields.len() == 1 {
let span = match def.variants[i].node.data {
VariantData::Struct(ref fields, _) |
VariantData::Tuple(ref fields, _) => fields[0].ty.span,
VariantData::Unit(_) => unreachable!(),
};
if let Some(snip) = snippet_opt(cx, span) {
db.span_suggestion(
span,
"consider boxing the large fields to reduce the total size of the enum",
format!("Box<{}>", snip),
);
return;
}
}
db.span_help(
def.variants[i].span,
"consider boxing the large fields to reduce the total size of the enum",
);
});
}
});
}

View file

@ -7,14 +7,19 @@
enum LargeEnum {
A(i32),
B([i32; 8000]), //~ ERROR large enum variant found on variant `B`
B([i32; 8000]), //~ ERROR large enum variant found
//~^ HELP consider boxing the large fields to reduce the total size of the enum
//~| SUGGESTION Box<[i32; 8000]>
}
enum GenericEnum<T> {
A(i32),
B([i32; 8000]), //~ ERROR large enum variant found on variant `B`
B([i32; 8000]), //~ ERROR large enum variant found
//~^ HELP consider boxing the large fields to reduce the total size of the enum
//~| SUGGESTION Box<[i32; 8000]>
C([T; 8000]),
D(T, [i32; 8000]), //~ ERROR large enum variant found on variant `D`
D(T, [i32; 8000]), //~ ERROR large enum variant found
//~^ HELP consider boxing the large fields to reduce the total size of the enum
}
trait SomeTrait {
@ -27,11 +32,21 @@ enum LargeEnumGeneric<A: SomeTrait> {
enum AnotherLargeEnum {
VariantOk(i32, u32),
ContainingLargeEnum(LargeEnum), //~ ERROR large enum variant found on variant `ContainingLargeEnum`
ContainingMoreThanOneField(i32, [i32; 8000], [i32; 9500]), //~ ERROR large enum variant found on variant `ContainingMoreThanOneField`
ContainingLargeEnum(LargeEnum), //~ ERROR large enum variant found
//~^ HELP consider boxing the large fields to reduce the total size of the enum
//~| SUGGESTION Box<LargeEnum>
ContainingMoreThanOneField(i32, [i32; 8000], [i32; 9500]), //~ ERROR large enum variant found
//~^ HELP consider boxing the large fields to reduce the total size of the enum
VoidVariant,
StructLikeLittle { x: i32, y: i32 },
StructLikeLarge { x: [i32; 8000], y: i32 }, //~ ERROR large enum variant found on variant `StructLikeLarge`
StructLikeLarge { x: [i32; 8000], y: i32 }, //~ ERROR large enum variant found
//~^ HELP consider boxing the large fields to reduce the total size of the enum
StructLikeLarge2 {
x:
[i32; 8000] //~ SUGGESTION Box<[i32; 8000]>
},
//~^ ERROR large enum variant found
//~^ HELP consider boxing the large fields to reduce the total size of the enum
}
fn main() {