Fix large_stack_arrays triggering for static/constants

This commit is contained in:
GnomedDev 2024-10-01 17:43:38 +01:00
parent db1bda3df1
commit 4b7c19018d
No known key found for this signature in database
4 changed files with 32 additions and 24 deletions

View File

@ -30,6 +30,7 @@ declare_clippy_lint! {
pub struct LargeStackArrays { pub struct LargeStackArrays {
maximum_allowed_size: u64, maximum_allowed_size: u64,
prev_vec_macro_callsite: Option<Span>, prev_vec_macro_callsite: Option<Span>,
is_in_const_item: bool,
} }
impl LargeStackArrays { impl LargeStackArrays {
@ -37,6 +38,7 @@ impl LargeStackArrays {
Self { Self {
maximum_allowed_size: conf.array_size_threshold, maximum_allowed_size: conf.array_size_threshold,
prev_vec_macro_callsite: None, prev_vec_macro_callsite: None,
is_in_const_item: false,
} }
} }
@ -60,8 +62,21 @@ impl LargeStackArrays {
impl_lint_pass!(LargeStackArrays => [LARGE_STACK_ARRAYS]); impl_lint_pass!(LargeStackArrays => [LARGE_STACK_ARRAYS]);
impl<'tcx> LateLintPass<'tcx> for LargeStackArrays { impl<'tcx> LateLintPass<'tcx> for LargeStackArrays {
fn check_item(&mut self, _: &LateContext<'tcx>, item: &'tcx Item<'tcx>) {
if matches!(item.kind, ItemKind::Static(..) | ItemKind::Const(..)) {
self.is_in_const_item = true;
}
}
fn check_item_post(&mut self, _: &LateContext<'tcx>, item: &'tcx Item<'tcx>) {
if matches!(item.kind, ItemKind::Static(..) | ItemKind::Const(..)) {
self.is_in_const_item = false;
}
}
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &Expr<'tcx>) { fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &Expr<'tcx>) {
if let ExprKind::Repeat(_, _) | ExprKind::Array(_) = expr.kind if !self.is_in_const_item
&& let ExprKind::Repeat(_, _) | ExprKind::Array(_) = expr.kind
&& !self.is_from_vec_macro(cx, expr.span) && !self.is_from_vec_macro(cx, expr.span)
&& let ty::Array(element_type, cst) = cx.typeck_results().expr_ty(expr).kind() && let ty::Array(element_type, cst) = cx.typeck_results().expr_ty(expr).kind()
&& let ConstKind::Value(_, ty::ValTree::Leaf(element_count)) = cst.kind() && let ConstKind::Value(_, ty::ValTree::Leaf(element_count)) = cst.kind()

View File

@ -9,16 +9,6 @@ LL | const ABOVE: [u8; 11] = [0; 11];
= note: `-D clippy::large-const-arrays` implied by `-D warnings` = note: `-D clippy::large-const-arrays` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::large_const_arrays)]` = help: to override `-D warnings` add `#[allow(clippy::large_const_arrays)]`
error: allocating a local array larger than 10 bytes
--> tests/ui-toml/array_size_threshold/array_size_threshold.rs:4:25
|
LL | const ABOVE: [u8; 11] = [0; 11];
| ^^^^^^^
|
= help: consider allocating on the heap with `vec![0; 11].into_boxed_slice()`
= note: `-D clippy::large-stack-arrays` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::large_stack_arrays)]`
error: allocating a local array larger than 10 bytes error: allocating a local array larger than 10 bytes
--> tests/ui-toml/array_size_threshold/array_size_threshold.rs:8:17 --> tests/ui-toml/array_size_threshold/array_size_threshold.rs:8:17
| |
@ -26,6 +16,8 @@ LL | let above = [0u8; 11];
| ^^^^^^^^^ | ^^^^^^^^^
| |
= help: consider allocating on the heap with `vec![0u8; 11].into_boxed_slice()` = help: consider allocating on the heap with `vec![0u8; 11].into_boxed_slice()`
= note: `-D clippy::large-stack-arrays` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::large_stack_arrays)]`
error: aborting due to 3 previous errors error: aborting due to 2 previous errors

View File

@ -15,6 +15,7 @@ enum E {
T(u32), T(u32),
} }
const STATIC_PROMOTED_LARGE_ARRAY: &[u8; 512001] = &[0; 512001];
pub static DOESNOTLINT: [u8; 512_001] = [0; 512_001]; pub static DOESNOTLINT: [u8; 512_001] = [0; 512_001];
pub static DOESNOTLINT2: [u8; 512_001] = { pub static DOESNOTLINT2: [u8; 512_001] = {
let x = 0; let x = 0;

View File

@ -1,5 +1,5 @@
error: allocating a local array larger than 512000 bytes error: allocating a local array larger than 512000 bytes
--> tests/ui/large_stack_arrays.rs:32:14 --> tests/ui/large_stack_arrays.rs:33:14
| |
LL | let _x = [build(); 3]; LL | let _x = [build(); 3];
| ^^^^^^^^^^^^ | ^^^^^^^^^^^^
@ -9,7 +9,7 @@ LL | let _x = [build(); 3];
= help: to override `-D warnings` add `#[allow(clippy::large_stack_arrays)]` = help: to override `-D warnings` add `#[allow(clippy::large_stack_arrays)]`
error: allocating a local array larger than 512000 bytes error: allocating a local array larger than 512000 bytes
--> tests/ui/large_stack_arrays.rs:35:14 --> tests/ui/large_stack_arrays.rs:36:14
| |
LL | let _y = [build(), build(), build()]; LL | let _y = [build(), build(), build()];
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -17,7 +17,7 @@ LL | let _y = [build(), build(), build()];
= help: consider allocating on the heap with `vec![build(), build(), build()].into_boxed_slice()` = help: consider allocating on the heap with `vec![build(), build(), build()].into_boxed_slice()`
error: allocating a local array larger than 512000 bytes error: allocating a local array larger than 512000 bytes
--> tests/ui/large_stack_arrays.rs:41:9 --> tests/ui/large_stack_arrays.rs:42:9
| |
LL | [0u32; 20_000_000], LL | [0u32; 20_000_000],
| ^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^
@ -25,7 +25,7 @@ LL | [0u32; 20_000_000],
= help: consider allocating on the heap with `vec![0u32; 20_000_000].into_boxed_slice()` = help: consider allocating on the heap with `vec![0u32; 20_000_000].into_boxed_slice()`
error: allocating a local array larger than 512000 bytes error: allocating a local array larger than 512000 bytes
--> tests/ui/large_stack_arrays.rs:43:9 --> tests/ui/large_stack_arrays.rs:44:9
| |
LL | [S { data: [0; 32] }; 5000], LL | [S { data: [0; 32] }; 5000],
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -33,7 +33,7 @@ LL | [S { data: [0; 32] }; 5000],
= help: consider allocating on the heap with `vec![S { data: [0; 32] }; 5000].into_boxed_slice()` = help: consider allocating on the heap with `vec![S { data: [0; 32] }; 5000].into_boxed_slice()`
error: allocating a local array larger than 512000 bytes error: allocating a local array larger than 512000 bytes
--> tests/ui/large_stack_arrays.rs:45:9 --> tests/ui/large_stack_arrays.rs:46:9
| |
LL | [Some(""); 20_000_000], LL | [Some(""); 20_000_000],
| ^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^
@ -41,7 +41,7 @@ LL | [Some(""); 20_000_000],
= help: consider allocating on the heap with `vec![Some(""); 20_000_000].into_boxed_slice()` = help: consider allocating on the heap with `vec![Some(""); 20_000_000].into_boxed_slice()`
error: allocating a local array larger than 512000 bytes error: allocating a local array larger than 512000 bytes
--> tests/ui/large_stack_arrays.rs:47:9 --> tests/ui/large_stack_arrays.rs:48:9
| |
LL | [E::T(0); 5000], LL | [E::T(0); 5000],
| ^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^
@ -49,7 +49,7 @@ LL | [E::T(0); 5000],
= help: consider allocating on the heap with `vec![E::T(0); 5000].into_boxed_slice()` = help: consider allocating on the heap with `vec![E::T(0); 5000].into_boxed_slice()`
error: allocating a local array larger than 512000 bytes error: allocating a local array larger than 512000 bytes
--> tests/ui/large_stack_arrays.rs:49:9 --> tests/ui/large_stack_arrays.rs:50:9
| |
LL | [0u8; usize::MAX], LL | [0u8; usize::MAX],
| ^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^
@ -57,7 +57,7 @@ LL | [0u8; usize::MAX],
= help: consider allocating on the heap with `vec![0u8; usize::MAX].into_boxed_slice()` = help: consider allocating on the heap with `vec![0u8; usize::MAX].into_boxed_slice()`
error: allocating a local array larger than 512000 bytes error: allocating a local array larger than 512000 bytes
--> tests/ui/large_stack_arrays.rs:93:13 --> tests/ui/large_stack_arrays.rs:94:13
| |
LL | let y = [x, x, dummy!(x), x, x]; LL | let y = [x, x, dummy!(x), x, x];
| ^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^
@ -65,7 +65,7 @@ LL | let y = [x, x, dummy!(x), x, x];
= help: consider allocating on the heap with `vec![x, x, dummy!(x), x, x].into_boxed_slice()` = help: consider allocating on the heap with `vec![x, x, dummy!(x), x, x].into_boxed_slice()`
error: allocating a local array larger than 512000 bytes error: allocating a local array larger than 512000 bytes
--> tests/ui/large_stack_arrays.rs:70:13 --> tests/ui/large_stack_arrays.rs:71:13
| |
LL | [$a, $b, $a, $b] LL | [$a, $b, $a, $b]
| ^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^
@ -76,7 +76,7 @@ LL | let y = dummy![x => x];
= note: this error originates in the macro `dummy` (in Nightly builds, run with -Z macro-backtrace for more info) = note: this error originates in the macro `dummy` (in Nightly builds, run with -Z macro-backtrace for more info)
error: allocating a local array larger than 512000 bytes error: allocating a local array larger than 512000 bytes
--> tests/ui/large_stack_arrays.rs:98:20 --> tests/ui/large_stack_arrays.rs:99:20
| |
LL | let y = dummy![[x, x, x, x, x]]; LL | let y = dummy![[x, x, x, x, x]];
| ^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^
@ -84,13 +84,13 @@ LL | let y = dummy![[x, x, x, x, x]];
= help: consider allocating on the heap with `vec![x, x, x, x, x].into_boxed_slice()` = help: consider allocating on the heap with `vec![x, x, x, x, x].into_boxed_slice()`
error: allocating a local array larger than 512000 bytes error: allocating a local array larger than 512000 bytes
--> tests/ui/large_stack_arrays.rs:101:39 --> tests/ui/large_stack_arrays.rs:102:39
| |
LL | let y = proc_macros::make_it_big!([x; 1]); LL | let y = proc_macros::make_it_big!([x; 1]);
| ^^^^^^ | ^^^^^^
error: allocating a local array larger than 512000 bytes error: allocating a local array larger than 512000 bytes
--> tests/ui/large_stack_arrays.rs:82:23 --> tests/ui/large_stack_arrays.rs:83:23
| |
LL | let _x_ = [$id; $n]; LL | let _x_ = [$id; $n];
| ^^^^^^^^^ | ^^^^^^^^^