2017-02-04 21:12:55 -06:00
|
|
|
//! lint when there is a large size difference between variants on an enum
|
2017-01-30 06:17:56 -06:00
|
|
|
|
2021-03-25 13:29:11 -05:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_then;
|
2021-09-16 03:42:36 -05:00
|
|
|
use clippy_utils::source::snippet_with_applicability;
|
2018-12-29 09:04:45 -06:00
|
|
|
use rustc_errors::Applicability;
|
2021-09-16 03:42:36 -05:00
|
|
|
use rustc_hir::{Item, ItemKind};
|
2020-01-12 00:08:41 -06:00
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
2021-01-02 09:29:43 -06:00
|
|
|
use rustc_middle::lint::in_external_macro;
|
2021-08-30 09:38:27 -05:00
|
|
|
use rustc_middle::ty::layout::LayoutOf;
|
2020-01-11 05:37:08 -06:00
|
|
|
use rustc_session::{declare_tool_lint, impl_lint_pass};
|
2021-09-16 03:42:36 -05:00
|
|
|
use rustc_span::source_map::Span;
|
2017-01-30 06:17:56 -06:00
|
|
|
|
2018-03-28 08:24:26 -05:00
|
|
|
declare_clippy_lint! {
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### What it does
|
|
|
|
/// Checks for large size differences between variants on
|
2019-03-05 10:50:33 -06:00
|
|
|
/// `enum`s.
|
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// Enum size is bounded by the largest variant. Having a
|
2020-04-08 17:05:20 -05:00
|
|
|
/// large variant can penalize the memory layout of that enum.
|
2019-03-05 10:50:33 -06:00
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Known problems
|
|
|
|
/// This lint obviously cannot take the distribution of
|
2020-04-08 17:05:20 -05:00
|
|
|
/// variants in your running program into account. It is possible that the
|
|
|
|
/// smaller variants make up less than 1% of all instances, in which case
|
|
|
|
/// the overhead is negligible and the boxing is counter-productive. Always
|
|
|
|
/// measure the change this lint suggests.
|
2019-03-05 10:50:33 -06:00
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Example
|
2019-03-05 10:50:33 -06:00
|
|
|
/// ```rust
|
2020-04-15 02:55:02 -05:00
|
|
|
/// // Bad
|
2019-03-05 10:50:33 -06:00
|
|
|
/// enum Test {
|
|
|
|
/// A(i32),
|
|
|
|
/// B([i32; 8000]),
|
|
|
|
/// }
|
2020-04-15 02:55:02 -05:00
|
|
|
///
|
|
|
|
/// // Possibly better
|
|
|
|
/// enum Test2 {
|
|
|
|
/// A(i32),
|
|
|
|
/// B(Box<[i32; 8000]>),
|
|
|
|
/// }
|
2019-03-05 10:50:33 -06:00
|
|
|
/// ```
|
Added `clippy::version` attribute to all normal lints
So, some context for this, well, more a story. I'm not used to scripting, I've never really scripted anything, even if it's a valuable skill. I just never really needed it. Now, `@flip1995` correctly suggested using a script for this in `rust-clippy#7813`...
And I decided to write a script using nushell because why not? This was a mistake... I spend way more time on this than I would like to admit. It has definitely been more than 4 hours. It shouldn't take that long, but me being new to scripting and nushell just wasn't a good mixture... Anyway, here is the script that creates another script which adds the versions. Fun...
Just execute this on the `gh-pages` branch and the resulting `replacer.sh` in `clippy_lints` and it should all work.
```nu
mv v0.0.212 rust-1.00.0;
mv beta rust-1.57.0;
mv master rust-1.58.0;
let paths = (open ./rust-1.58.0/lints.json | select id id_span | flatten | select id path);
let versions = (
ls | where name =~ "rust-" | select name | format {name}/lints.json |
each { open $it | select id | insert version $it | str substring "5,11" version} |
group-by id | rotate counter-clockwise id version |
update version {get version | first 1} | flatten | select id version);
$paths | each { |row|
let version = ($versions | where id == ($row.id) | format {version})
let idu = ($row.id | str upcase)
$"sed -i '0,/($idu),/{s/pub ($idu),/#[clippy::version = "($version)"]\n pub ($idu),/}' ($row.path)"
} | str collect ";" | str find-replace --all '1.00.0' 'pre 1.29.0' | save "replacer.sh";
```
And this still has some problems, but at this point I just want to be done -.-
2021-10-21 14:06:26 -05:00
|
|
|
#[clippy::version = "pre 1.29.0"]
|
2017-01-30 06:17:56 -06:00
|
|
|
pub LARGE_ENUM_VARIANT,
|
2018-03-28 08:24:26 -05:00
|
|
|
perf,
|
2017-02-04 21:12:55 -06:00
|
|
|
"large size difference between variants on an enum"
|
2017-01-30 06:17:56 -06:00
|
|
|
}
|
|
|
|
|
2017-08-09 02:30:56 -05:00
|
|
|
#[derive(Copy, Clone)]
|
2017-01-30 06:17:56 -06:00
|
|
|
pub struct LargeEnumVariant {
|
2017-02-04 21:12:55 -06:00
|
|
|
maximum_size_difference_allowed: u64,
|
2017-01-30 06:17:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl LargeEnumVariant {
|
2019-09-18 01:37:41 -05:00
|
|
|
#[must_use]
|
2017-02-04 21:12:55 -06:00
|
|
|
pub fn new(maximum_size_difference_allowed: u64) -> Self {
|
2017-09-05 04:33:04 -05:00
|
|
|
Self {
|
2018-03-15 10:07:15 -05:00
|
|
|
maximum_size_difference_allowed,
|
2017-09-05 04:33:04 -05:00
|
|
|
}
|
2017-01-30 06:17:56 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-16 03:42:36 -05:00
|
|
|
struct FieldInfo {
|
|
|
|
ind: usize,
|
|
|
|
size: u64,
|
|
|
|
}
|
|
|
|
|
|
|
|
struct VariantInfo {
|
|
|
|
ind: usize,
|
|
|
|
size: u64,
|
|
|
|
fields_size: Vec<FieldInfo>,
|
|
|
|
}
|
|
|
|
|
2019-04-08 15:43:55 -05:00
|
|
|
impl_lint_pass!(LargeEnumVariant => [LARGE_ENUM_VARIANT]);
|
2017-01-30 06:17:56 -06:00
|
|
|
|
2020-06-25 15:41:36 -05:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for LargeEnumVariant {
|
|
|
|
fn check_item(&mut self, cx: &LateContext<'_>, item: &Item<'_>) {
|
2021-01-02 09:29:43 -06:00
|
|
|
if in_external_macro(cx.tcx.sess, item.span) {
|
|
|
|
return;
|
|
|
|
}
|
2019-09-27 10:16:06 -05:00
|
|
|
if let ItemKind::Enum(ref def, _) = item.kind {
|
2021-01-30 10:47:51 -06:00
|
|
|
let ty = cx.tcx.type_of(item.def_id);
|
2018-11-27 14:14:15 -06:00
|
|
|
let adt = ty.ty_adt_def().expect("already checked whether this is an enum");
|
2021-09-16 03:42:36 -05:00
|
|
|
if adt.variants.len() <= 1 {
|
|
|
|
return;
|
2017-01-30 06:17:56 -06:00
|
|
|
}
|
2021-09-16 03:42:36 -05:00
|
|
|
let mut variants_size: Vec<VariantInfo> = adt
|
|
|
|
.variants
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.map(|(i, variant)| {
|
|
|
|
let mut fields_size = Vec::new();
|
|
|
|
let size: u64 = variant
|
|
|
|
.fields
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.filter_map(|(i, f)| {
|
|
|
|
let ty = cx.tcx.type_of(f.did);
|
|
|
|
// don't count generics by filtering out everything
|
|
|
|
// that does not have a layout
|
|
|
|
cx.layout_of(ty).ok().map(|l| {
|
|
|
|
let size = l.size.bytes();
|
|
|
|
fields_size.push(FieldInfo { ind: i, size });
|
|
|
|
size
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.sum();
|
|
|
|
VariantInfo {
|
|
|
|
ind: i,
|
|
|
|
size,
|
|
|
|
fields_size,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect();
|
2017-02-04 21:12:55 -06:00
|
|
|
|
2021-09-16 03:42:36 -05:00
|
|
|
variants_size.sort_by(|a, b| (b.size.cmp(&a.size)));
|
2017-02-04 21:12:55 -06:00
|
|
|
|
2021-09-16 03:42:36 -05:00
|
|
|
let mut difference = variants_size[0].size - variants_size[1].size;
|
|
|
|
if difference > self.maximum_size_difference_allowed {
|
|
|
|
let help_text = "consider boxing the large fields to reduce the total size of the enum";
|
|
|
|
span_lint_and_then(
|
|
|
|
cx,
|
|
|
|
LARGE_ENUM_VARIANT,
|
|
|
|
def.variants[variants_size[0].ind].span,
|
|
|
|
"large size difference between variants",
|
|
|
|
|diag| {
|
|
|
|
diag.span_label(
|
|
|
|
def.variants[variants_size[0].ind].span,
|
|
|
|
&format!("this variant is {} bytes", variants_size[0].size),
|
|
|
|
);
|
|
|
|
diag.span_note(
|
|
|
|
def.variants[variants_size[1].ind].span,
|
|
|
|
&format!("and the second-largest variant is {} bytes:", variants_size[1].size),
|
|
|
|
);
|
2017-02-04 21:12:55 -06:00
|
|
|
|
2021-09-16 03:42:36 -05:00
|
|
|
let fields = def.variants[variants_size[0].ind].data.fields();
|
|
|
|
variants_size[0].fields_size.sort_by(|a, b| (a.size.cmp(&b.size)));
|
|
|
|
let mut applicability = Applicability::MaybeIncorrect;
|
|
|
|
let sugg: Vec<(Span, String)> = variants_size[0]
|
|
|
|
.fields_size
|
|
|
|
.iter()
|
|
|
|
.rev()
|
|
|
|
.map_while(|val| {
|
|
|
|
if difference > self.maximum_size_difference_allowed {
|
|
|
|
difference = difference.saturating_sub(val.size);
|
|
|
|
Some((
|
|
|
|
fields[val.ind].ty.span,
|
|
|
|
format!(
|
|
|
|
"Box<{}>",
|
|
|
|
snippet_with_applicability(
|
|
|
|
cx,
|
|
|
|
fields[val.ind].ty.span,
|
|
|
|
"..",
|
|
|
|
&mut applicability
|
|
|
|
)
|
|
|
|
.into_owned()
|
|
|
|
),
|
|
|
|
))
|
|
|
|
} else {
|
|
|
|
None
|
2017-08-09 02:30:56 -05:00
|
|
|
}
|
2021-09-16 03:42:36 -05:00
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
if !sugg.is_empty() {
|
|
|
|
diag.multipart_suggestion(help_text, sugg, Applicability::MaybeIncorrect);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
diag.span_help(def.variants[variants_size[0].ind].span, help_text);
|
|
|
|
},
|
|
|
|
);
|
2017-02-04 21:12:55 -06:00
|
|
|
}
|
2017-01-30 06:17:56 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|