|
| 1 | +use crate::rustc_target::abi::LayoutOf; |
| 2 | +use crate::utils::span_lint_and_then; |
| 3 | +use if_chain::if_chain; |
| 4 | +use rustc::mir::interpret::ConstValue; |
| 5 | +use rustc::ty::{self, ConstKind}; |
| 6 | +use rustc_errors::Applicability; |
| 7 | +use rustc_hir::{Item, ItemKind}; |
| 8 | +use rustc_lint::{LateContext, LateLintPass}; |
| 9 | +use rustc_session::{declare_tool_lint, impl_lint_pass}; |
| 10 | +use rustc_span::{BytePos, Pos, Span}; |
| 11 | +use rustc_typeck::hir_ty_to_ty; |
| 12 | + |
| 13 | +declare_clippy_lint! { |
| 14 | + /// **What it does:** Checks for large `const` arrays that should |
| 15 | + /// be defined as `static` instead. |
| 16 | + /// |
| 17 | + /// **Why is this bad?** Performance: const variables are inlined upon use. |
| 18 | + /// Static items result in only one instance and has a fixed location in memory. |
| 19 | + /// |
| 20 | + /// **Known problems:** None. |
| 21 | + /// |
| 22 | + /// **Example:** |
| 23 | + /// ```rust,ignore |
| 24 | + /// // Bad |
| 25 | + /// pub const a = [0u32; 1_000_000]; |
| 26 | + /// |
| 27 | + /// // Good |
| 28 | + /// pub static a = [0u32; 1_000_000]; |
| 29 | + /// ``` |
| 30 | + pub LARGE_CONST_ARRAYS, |
| 31 | + perf, |
| 32 | + "large non-scalar const array may cause performance overhead" |
| 33 | +} |
| 34 | + |
| 35 | +pub struct LargeConstArrays { |
| 36 | + maximum_allowed_size: u64, |
| 37 | +} |
| 38 | + |
| 39 | +impl LargeConstArrays { |
| 40 | + #[must_use] |
| 41 | + pub fn new(maximum_allowed_size: u64) -> Self { |
| 42 | + Self { maximum_allowed_size } |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +impl_lint_pass!(LargeConstArrays => [LARGE_CONST_ARRAYS]); |
| 47 | + |
| 48 | +impl<'a, 'tcx> LateLintPass<'a, 'tcx> for LargeConstArrays { |
| 49 | + fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item<'_>) { |
| 50 | + if_chain! { |
| 51 | + if !item.span.from_expansion(); |
| 52 | + if let ItemKind::Const(hir_ty, _) = &item.kind; |
| 53 | + let ty = hir_ty_to_ty(cx.tcx, hir_ty); |
| 54 | + if let ty::Array(element_type, cst) = ty.kind; |
| 55 | + if let ConstKind::Value(val) = cst.val; |
| 56 | + if let ConstValue::Scalar(element_count) = val; |
| 57 | + if let Ok(element_count) = element_count.to_machine_usize(&cx.tcx); |
| 58 | + if let Ok(element_size) = cx.layout_of(element_type).map(|l| l.size.bytes()); |
| 59 | + if self.maximum_allowed_size < element_count * element_size; |
| 60 | + |
| 61 | + then { |
| 62 | + let hi_pos = item.ident.span.lo() - BytePos::from_usize(1); |
| 63 | + let sugg_span = Span::new( |
| 64 | + hi_pos - BytePos::from_usize("const".len()), |
| 65 | + hi_pos, |
| 66 | + item.span.ctxt(), |
| 67 | + ); |
| 68 | + span_lint_and_then( |
| 69 | + cx, |
| 70 | + LARGE_CONST_ARRAYS, |
| 71 | + item.span, |
| 72 | + "large array defined as const", |
| 73 | + |db| { |
| 74 | + db.span_suggestion( |
| 75 | + sugg_span, |
| 76 | + "make this a static item", |
| 77 | + "static".to_string(), |
| 78 | + Applicability::MachineApplicable, |
| 79 | + ); |
| 80 | + } |
| 81 | + ); |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | +} |
0 commit comments