rust/clippy_lints/src/large_const_arrays.rs

90 lines
3.2 KiB
Rust
Raw Normal View History

use clippy_utils::diagnostics::span_lint_and_then;
2020-02-29 18:41:18 +01:00
use if_chain::if_chain;
use rustc_errors::Applicability;
use rustc_hir::{Item, ItemKind};
use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::ty::layout::LayoutOf;
2020-03-23 22:07:46 +01:00
use rustc_middle::ty::{self, ConstKind};
2020-02-29 18:41:18 +01:00
use rustc_session::{declare_tool_lint, impl_lint_pass};
use rustc_span::{BytePos, Pos, Span};
declare_clippy_lint! {
/// ### What it does
/// Checks for large `const` arrays that should
2020-02-29 18:41:18 +01:00
/// be defined as `static` instead.
///
/// ### Why is this bad?
/// Performance: const variables are inlined upon use.
2020-02-29 18:41:18 +01:00
/// Static items result in only one instance and has a fixed location in memory.
///
/// ### Example
2020-02-29 18:41:18 +01:00
/// ```rust,ignore
/// pub const a = [0u32; 1_000_000];
/// ```
2020-02-29 18:41:18 +01:00
///
/// Use instead:
/// ```rust,ignore
2020-02-29 18:41:18 +01:00
/// pub static a = [0u32; 1_000_000];
/// ```
#[clippy::version = "1.44.0"]
2020-02-29 18:41:18 +01:00
pub LARGE_CONST_ARRAYS,
perf,
"large non-scalar const array may cause performance overhead"
}
pub struct LargeConstArrays {
maximum_allowed_size: u128,
2020-02-29 18:41:18 +01:00
}
impl LargeConstArrays {
#[must_use]
pub fn new(maximum_allowed_size: u128) -> Self {
2020-02-29 18:41:18 +01:00
Self { maximum_allowed_size }
}
}
impl_lint_pass!(LargeConstArrays => [LARGE_CONST_ARRAYS]);
impl<'tcx> LateLintPass<'tcx> for LargeConstArrays {
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'_>) {
2020-02-29 18:41:18 +01:00
if_chain! {
if !item.span.from_expansion();
2023-09-20 13:41:20 +00:00
if let ItemKind::Const(_, generics, _) = &item.kind;
// Since static items may not have generics, skip generic const items.
// FIXME(generic_const_items): I don't think checking `generics.hwcp` suffices as it
// doesn't account for empty where-clauses that only consist of keyword `where` IINM.
if generics.params.is_empty() && !generics.has_where_clause_predicates;
2023-09-20 13:41:20 +00:00
let ty = cx.tcx.type_of(item.owner_id).instantiate_identity();
if let ty::Array(element_type, cst) = ty.kind();
if let ConstKind::Value(ty::ValTree::Leaf(element_count)) = cst.kind();
if let Ok(element_count) = element_count.try_to_target_usize(cx.tcx);
2022-01-25 14:13:38 +11:00
if let Ok(element_size) = cx.layout_of(*element_type).map(|l| l.size.bytes());
if self.maximum_allowed_size < u128::from(element_count) * u128::from(element_size);
2020-02-29 18:41:18 +01:00
then {
let hi_pos = item.ident.span.lo() - BytePos::from_usize(1);
let sugg_span = Span::new(
hi_pos - BytePos::from_usize("const".len()),
hi_pos,
item.span.ctxt(),
2021-04-18 14:27:04 +02:00
item.span.parent(),
2020-02-29 18:41:18 +01:00
);
span_lint_and_then(
cx,
LARGE_CONST_ARRAYS,
item.span,
"large array defined as const",
|diag| {
diag.span_suggestion(
2020-02-29 18:41:18 +01:00
sugg_span,
"make this a static item",
"static",
2020-02-29 18:41:18 +01:00
Applicability::MachineApplicable,
);
}
);
}
}
}
}