2020-02-29 11:41:18 -06:00
|
|
|
use crate::rustc_target::abi::LayoutOf;
|
2021-03-25 13:29:11 -05:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_then;
|
2020-02-29 11:41:18 -06:00
|
|
|
use if_chain::if_chain;
|
|
|
|
use rustc_errors::Applicability;
|
|
|
|
use rustc_hir::{Item, ItemKind};
|
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
2020-03-23 16:07:46 -05:00
|
|
|
use rustc_middle::mir::interpret::ConstValue;
|
|
|
|
use rustc_middle::ty::{self, ConstKind};
|
2020-02-29 11:41:18 -06:00
|
|
|
use rustc_session::{declare_tool_lint, impl_lint_pass};
|
|
|
|
use rustc_span::{BytePos, Pos, Span};
|
|
|
|
use rustc_typeck::hir_ty_to_ty;
|
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### What it does
|
|
|
|
/// Checks for large `const` arrays that should
|
2020-02-29 11:41:18 -06:00
|
|
|
/// be defined as `static` instead.
|
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// Performance: const variables are inlined upon use.
|
2020-02-29 11:41:18 -06:00
|
|
|
/// Static items result in only one instance and has a fixed location in memory.
|
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Example
|
2020-02-29 11:41:18 -06:00
|
|
|
/// ```rust,ignore
|
|
|
|
/// // Bad
|
|
|
|
/// pub const a = [0u32; 1_000_000];
|
|
|
|
///
|
|
|
|
/// // Good
|
|
|
|
/// pub static a = [0u32; 1_000_000];
|
|
|
|
/// ```
|
|
|
|
pub LARGE_CONST_ARRAYS,
|
|
|
|
perf,
|
|
|
|
"large non-scalar const array may cause performance overhead"
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct LargeConstArrays {
|
|
|
|
maximum_allowed_size: u64,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LargeConstArrays {
|
|
|
|
#[must_use]
|
|
|
|
pub fn new(maximum_allowed_size: u64) -> Self {
|
|
|
|
Self { maximum_allowed_size }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl_lint_pass!(LargeConstArrays => [LARGE_CONST_ARRAYS]);
|
|
|
|
|
2020-06-25 15:41:36 -05:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for LargeConstArrays {
|
|
|
|
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'_>) {
|
2020-02-29 11:41:18 -06:00
|
|
|
if_chain! {
|
|
|
|
if !item.span.from_expansion();
|
|
|
|
if let ItemKind::Const(hir_ty, _) = &item.kind;
|
|
|
|
let ty = hir_ty_to_ty(cx.tcx, hir_ty);
|
2020-08-03 17:18:29 -05:00
|
|
|
if let ty::Array(element_type, cst) = ty.kind();
|
2020-12-06 08:01:03 -06:00
|
|
|
if let ConstKind::Value(ConstValue::Scalar(element_count)) = cst.val;
|
2020-02-29 11:41:18 -06:00
|
|
|
if let Ok(element_count) = element_count.to_machine_usize(&cx.tcx);
|
|
|
|
if let Ok(element_size) = cx.layout_of(element_type).map(|l| l.size.bytes());
|
|
|
|
if self.maximum_allowed_size < element_count * element_size;
|
|
|
|
|
|
|
|
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(),
|
|
|
|
);
|
|
|
|
span_lint_and_then(
|
|
|
|
cx,
|
|
|
|
LARGE_CONST_ARRAYS,
|
|
|
|
item.span,
|
|
|
|
"large array defined as const",
|
2020-04-17 01:08:00 -05:00
|
|
|
|diag| {
|
|
|
|
diag.span_suggestion(
|
2020-02-29 11:41:18 -06:00
|
|
|
sugg_span,
|
|
|
|
"make this a static item",
|
|
|
|
"static".to_string(),
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|