2017-02-05 03:12:55 +00:00
|
|
|
//! lint when there is a large size difference between variants on an enum
|
2017-01-30 12:17:56 +00:00
|
|
|
|
2021-10-07 09:21:30 +00:00
|
|
|
use clippy_utils::source::snippet_with_applicability;
|
2022-10-16 18:37:26 +00:00
|
|
|
use clippy_utils::{
|
|
|
|
diagnostics::span_lint_and_then,
|
|
|
|
ty::{approx_ty_size, is_copy, AdtVariantInfo},
|
|
|
|
};
|
2018-12-29 15:04:45 +00:00
|
|
|
use rustc_errors::Applicability;
|
2021-10-07 09:21:30 +00:00
|
|
|
use rustc_hir::{Item, ItemKind};
|
2020-01-12 06:08:41 +00:00
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
2021-01-02 15:29:43 +00:00
|
|
|
use rustc_middle::lint::in_external_macro;
|
2022-10-16 18:37:26 +00:00
|
|
|
use rustc_middle::ty::{Adt, Ty};
|
2020-01-11 11:37:08 +00:00
|
|
|
use rustc_session::{declare_tool_lint, impl_lint_pass};
|
2021-10-07 09:21:30 +00:00
|
|
|
use rustc_span::source_map::Span;
|
2017-01-30 12:17:56 +00:00
|
|
|
|
2018-03-28 13:24:26 +00:00
|
|
|
declare_clippy_lint! {
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### What it does
|
|
|
|
/// Checks for large size differences between variants on
|
2019-03-05 16:50:33 +00:00
|
|
|
/// `enum`s.
|
|
|
|
///
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### Why is this bad?
|
2022-09-09 11:36:26 +00:00
|
|
|
/// Enum size is bounded by the largest variant. Having one
|
2020-04-08 22:05:20 +00:00
|
|
|
/// large variant can penalize the memory layout of that enum.
|
2019-03-05 16:50:33 +00:00
|
|
|
///
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### Known problems
|
|
|
|
/// This lint obviously cannot take the distribution of
|
2020-04-08 22:05:20 +00: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 16:50:33 +00:00
|
|
|
///
|
2022-06-04 11:34:07 +00:00
|
|
|
/// For types that implement `Copy`, the suggestion to `Box` a variant's
|
|
|
|
/// data would require removing the trait impl. The types can of course
|
|
|
|
/// still be `Clone`, but that is worse ergonomically. Depending on the
|
2022-07-18 07:39:37 +00:00
|
|
|
/// use case it may be possible to store the large data in an auxiliary
|
2022-06-04 11:34:07 +00:00
|
|
|
/// structure (e.g. Arena or ECS).
|
|
|
|
///
|
2022-09-09 11:36:26 +00:00
|
|
|
/// The lint will ignore the impact of generic types to the type layout by
|
|
|
|
/// assuming every type parameter is zero-sized. Depending on your use case,
|
|
|
|
/// this may lead to a false positive.
|
2022-06-04 11:34:07 +00:00
|
|
|
///
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### Example
|
2019-03-05 16:50:33 +00:00
|
|
|
/// ```rust
|
|
|
|
/// enum Test {
|
|
|
|
/// A(i32),
|
|
|
|
/// B([i32; 8000]),
|
|
|
|
/// }
|
2022-06-16 15:39:06 +00:00
|
|
|
/// ```
|
2020-04-15 07:55:02 +00:00
|
|
|
///
|
2022-06-16 15:39:06 +00:00
|
|
|
/// Use instead:
|
|
|
|
/// ```rust
|
2020-04-15 07:55:02 +00:00
|
|
|
/// // Possibly better
|
|
|
|
/// enum Test2 {
|
|
|
|
/// A(i32),
|
|
|
|
/// B(Box<[i32; 8000]>),
|
|
|
|
/// }
|
2019-03-05 16:50:33 +00:00
|
|
|
/// ```
|
2021-12-06 11:33:31 +00:00
|
|
|
#[clippy::version = "pre 1.29.0"]
|
2017-01-30 12:17:56 +00:00
|
|
|
pub LARGE_ENUM_VARIANT,
|
2018-03-28 13:24:26 +00:00
|
|
|
perf,
|
2017-02-05 03:12:55 +00:00
|
|
|
"large size difference between variants on an enum"
|
2017-01-30 12:17:56 +00:00
|
|
|
}
|
|
|
|
|
2017-08-09 07:30:56 +00:00
|
|
|
#[derive(Copy, Clone)]
|
2017-01-30 12:17:56 +00:00
|
|
|
pub struct LargeEnumVariant {
|
2017-02-05 03:12:55 +00:00
|
|
|
maximum_size_difference_allowed: u64,
|
2017-01-30 12:17:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl LargeEnumVariant {
|
2019-09-18 06:37:41 +00:00
|
|
|
#[must_use]
|
2017-02-05 03:12:55 +00:00
|
|
|
pub fn new(maximum_size_difference_allowed: u64) -> Self {
|
2017-09-05 09:33:04 +00:00
|
|
|
Self {
|
2018-03-15 15:07:15 +00:00
|
|
|
maximum_size_difference_allowed,
|
2017-09-05 09:33:04 +00:00
|
|
|
}
|
2017-01-30 12:17:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-08 20:43:55 +00:00
|
|
|
impl_lint_pass!(LargeEnumVariant => [LARGE_ENUM_VARIANT]);
|
2017-01-30 12:17:56 +00:00
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for LargeEnumVariant {
|
2022-06-04 11:34:07 +00:00
|
|
|
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &Item<'tcx>) {
|
2021-01-02 15:29:43 +00:00
|
|
|
if in_external_macro(cx.tcx.sess, item.span) {
|
|
|
|
return;
|
|
|
|
}
|
2019-09-27 15:16:06 +00:00
|
|
|
if let ItemKind::Enum(ref def, _) = item.kind {
|
2022-10-27 03:02:18 +00:00
|
|
|
let ty = cx.tcx.type_of(item.owner_id);
|
2022-10-23 13:18:45 +00:00
|
|
|
let Adt(adt, subst) = ty.kind() else {
|
|
|
|
panic!("already checked whether this is an enum")
|
2022-09-09 11:36:26 +00:00
|
|
|
};
|
2022-03-04 20:28:41 +00:00
|
|
|
if adt.variants().len() <= 1 {
|
2021-10-07 09:21:30 +00:00
|
|
|
return;
|
2017-01-30 12:17:56 +00:00
|
|
|
}
|
2022-10-16 18:37:26 +00:00
|
|
|
let variants_size = AdtVariantInfo::new(cx, *adt, subst);
|
2017-02-05 03:12:55 +00:00
|
|
|
|
2021-10-07 09:21:30 +00: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,
|
2022-09-09 11:36:26 +00:00
|
|
|
item.span,
|
2021-10-07 09:21:30 +00:00
|
|
|
"large size difference between variants",
|
|
|
|
|diag| {
|
2022-09-09 11:36:26 +00:00
|
|
|
diag.span_label(
|
|
|
|
item.span,
|
|
|
|
format!("the entire enum is at least {} bytes", approx_ty_size(cx, ty)),
|
|
|
|
);
|
2021-10-07 09:21:30 +00:00
|
|
|
diag.span_label(
|
|
|
|
def.variants[variants_size[0].ind].span,
|
2022-09-09 11:36:26 +00:00
|
|
|
format!("the largest variant contains at least {} bytes", variants_size[0].size),
|
2021-10-07 09:21:30 +00:00
|
|
|
);
|
2022-09-09 11:36:26 +00:00
|
|
|
diag.span_label(
|
2021-10-07 09:21:30 +00:00
|
|
|
def.variants[variants_size[1].ind].span,
|
2022-09-09 11:36:26 +00:00
|
|
|
&if variants_size[1].fields_size.is_empty() {
|
|
|
|
"the second-largest variant carries no data at all".to_owned()
|
|
|
|
} else {
|
|
|
|
format!(
|
|
|
|
"the second-largest variant contains at least {} bytes",
|
|
|
|
variants_size[1].size
|
|
|
|
)
|
|
|
|
},
|
2021-10-07 09:21:30 +00:00
|
|
|
);
|
2017-02-05 03:12:55 +00:00
|
|
|
|
2021-10-07 09:21:30 +00:00
|
|
|
let fields = def.variants[variants_size[0].ind].data.fields();
|
|
|
|
let mut applicability = Applicability::MaybeIncorrect;
|
2022-06-04 11:34:07 +00:00
|
|
|
if is_copy(cx, ty) || maybe_copy(cx, ty) {
|
|
|
|
diag.span_note(
|
|
|
|
item.ident.span,
|
|
|
|
"boxing a variant would require the type no longer be `Copy`",
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
let sugg: Vec<(Span, String)> = variants_size[0]
|
|
|
|
.fields_size
|
|
|
|
.iter()
|
|
|
|
.rev()
|
2022-10-16 18:37:26 +00:00
|
|
|
.map_while(|&(ind, size)| {
|
2022-06-04 11:34:07 +00:00
|
|
|
if difference > self.maximum_size_difference_allowed {
|
2022-10-16 18:37:26 +00:00
|
|
|
difference = difference.saturating_sub(size);
|
2022-06-04 11:34:07 +00:00
|
|
|
Some((
|
2022-10-16 18:37:26 +00:00
|
|
|
fields[ind].ty.span,
|
2022-06-04 11:34:07 +00:00
|
|
|
format!(
|
|
|
|
"Box<{}>",
|
|
|
|
snippet_with_applicability(
|
|
|
|
cx,
|
2022-10-16 18:37:26 +00:00
|
|
|
fields[ind].ty.span,
|
2022-06-04 11:34:07 +00:00
|
|
|
"..",
|
|
|
|
&mut applicability
|
|
|
|
)
|
|
|
|
.into_owned()
|
|
|
|
),
|
|
|
|
))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect();
|
2021-10-07 09:21:30 +00:00
|
|
|
|
2022-06-04 11:34:07 +00:00
|
|
|
if !sugg.is_empty() {
|
|
|
|
diag.multipart_suggestion(help_text, sugg, Applicability::MaybeIncorrect);
|
|
|
|
return;
|
|
|
|
}
|
2021-10-07 09:21:30 +00:00
|
|
|
}
|
|
|
|
diag.span_help(def.variants[variants_size[0].ind].span, help_text);
|
|
|
|
},
|
|
|
|
);
|
2017-02-05 03:12:55 +00:00
|
|
|
}
|
2017-01-30 12:17:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-06-04 11:34:07 +00:00
|
|
|
|
|
|
|
fn maybe_copy<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> bool {
|
|
|
|
if let Adt(_def, substs) = ty.kind()
|
|
|
|
&& substs.types().next().is_some()
|
|
|
|
&& let Some(copy_trait) = cx.tcx.lang_items().copy_trait()
|
|
|
|
{
|
|
|
|
return cx.tcx.non_blanket_impls_for_ty(copy_trait, ty).next().is_some();
|
|
|
|
}
|
|
|
|
false
|
|
|
|
}
|