mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-12-29 22:43:41 +00:00
64ae3ae006
Specifically, rename the `Const` struct as `ConstS` and re-introduce `Const` as this: ``` pub struct Const<'tcx>(&'tcx Interned<ConstS>); ``` This now matches `Ty` and `Predicate` more closely, including using pointer-based `eq` and `hash`. Notable changes: - `mk_const` now takes a `ConstS`. - `Const` was copy, despite being 48 bytes. Now `ConstS` is not, so need a we need separate arena for it, because we can't use the `Dropless` one any more. - Many `&'tcx Const<'tcx>`/`&Const<'tcx>` to `Const<'tcx>` changes - Many `ct.ty` to `ct.ty()` and `ct.val` to `ct.val()` changes. - Lots of tedious sigil fiddling.
68 lines
2.2 KiB
Rust
68 lines
2.2 KiB
Rust
use clippy_utils::diagnostics::span_lint_and_help;
|
|
use clippy_utils::source::snippet;
|
|
use if_chain::if_chain;
|
|
use rustc_hir::{Expr, ExprKind};
|
|
use rustc_lint::{LateContext, LateLintPass};
|
|
use rustc_middle::mir::interpret::ConstValue;
|
|
use rustc_middle::ty::layout::LayoutOf;
|
|
use rustc_middle::ty::{self, ConstKind};
|
|
use rustc_session::{declare_tool_lint, impl_lint_pass};
|
|
|
|
declare_clippy_lint! {
|
|
/// ### What it does
|
|
/// Checks for local arrays that may be too large.
|
|
///
|
|
/// ### Why is this bad?
|
|
/// Large local arrays may cause stack overflow.
|
|
///
|
|
/// ### Example
|
|
/// ```rust,ignore
|
|
/// let a = [0u32; 1_000_000];
|
|
/// ```
|
|
#[clippy::version = "1.41.0"]
|
|
pub LARGE_STACK_ARRAYS,
|
|
pedantic,
|
|
"allocating large arrays on stack may cause stack overflow"
|
|
}
|
|
|
|
pub struct LargeStackArrays {
|
|
maximum_allowed_size: u64,
|
|
}
|
|
|
|
impl LargeStackArrays {
|
|
#[must_use]
|
|
pub fn new(maximum_allowed_size: u64) -> Self {
|
|
Self { maximum_allowed_size }
|
|
}
|
|
}
|
|
|
|
impl_lint_pass!(LargeStackArrays => [LARGE_STACK_ARRAYS]);
|
|
|
|
impl<'tcx> LateLintPass<'tcx> for LargeStackArrays {
|
|
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
|
|
if_chain! {
|
|
if let ExprKind::Repeat(_, _) = expr.kind;
|
|
if let ty::Array(element_type, cst) = cx.typeck_results().expr_ty(expr).kind();
|
|
if let ConstKind::Value(ConstValue::Scalar(element_count)) = cst.val();
|
|
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 {
|
|
span_lint_and_help(
|
|
cx,
|
|
LARGE_STACK_ARRAYS,
|
|
expr.span,
|
|
&format!(
|
|
"allocating a local array larger than {} bytes",
|
|
self.maximum_allowed_size
|
|
),
|
|
None,
|
|
&format!(
|
|
"consider allocating on the heap with `vec!{}.into_boxed_slice()`",
|
|
snippet(cx, expr.span, "[...]")
|
|
),
|
|
);
|
|
}
|
|
}
|
|
}
|
|
}
|