Skip to content

Commit 4b7c190

Browse files
committed
Fix large_stack_arrays triggering for static/constants
1 parent db1bda3 commit 4b7c190

File tree

4 files changed

+32
-24
lines changed

4 files changed

+32
-24
lines changed

clippy_lints/src/large_stack_arrays.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,15 @@ declare_clippy_lint! {
3030
pub struct LargeStackArrays {
3131
maximum_allowed_size: u64,
3232
prev_vec_macro_callsite: Option<Span>,
33+
is_in_const_item: bool,
3334
}
3435

3536
impl LargeStackArrays {
3637
pub fn new(conf: &'static Conf) -> Self {
3738
Self {
3839
maximum_allowed_size: conf.array_size_threshold,
3940
prev_vec_macro_callsite: None,
41+
is_in_const_item: false,
4042
}
4143
}
4244

@@ -60,8 +62,21 @@ impl LargeStackArrays {
6062
impl_lint_pass!(LargeStackArrays => [LARGE_STACK_ARRAYS]);
6163

6264
impl<'tcx> LateLintPass<'tcx> for LargeStackArrays {
65+
fn check_item(&mut self, _: &LateContext<'tcx>, item: &'tcx Item<'tcx>) {
66+
if matches!(item.kind, ItemKind::Static(..) | ItemKind::Const(..)) {
67+
self.is_in_const_item = true;
68+
}
69+
}
70+
71+
fn check_item_post(&mut self, _: &LateContext<'tcx>, item: &'tcx Item<'tcx>) {
72+
if matches!(item.kind, ItemKind::Static(..) | ItemKind::Const(..)) {
73+
self.is_in_const_item = false;
74+
}
75+
}
76+
6377
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &Expr<'tcx>) {
64-
if let ExprKind::Repeat(_, _) | ExprKind::Array(_) = expr.kind
78+
if !self.is_in_const_item
79+
&& let ExprKind::Repeat(_, _) | ExprKind::Array(_) = expr.kind
6580
&& !self.is_from_vec_macro(cx, expr.span)
6681
&& let ty::Array(element_type, cst) = cx.typeck_results().expr_ty(expr).kind()
6782
&& let ConstKind::Value(_, ty::ValTree::Leaf(element_count)) = cst.kind()

tests/ui-toml/array_size_threshold/array_size_threshold.stderr

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,15 @@ LL | const ABOVE: [u8; 11] = [0; 11];
99
= note: `-D clippy::large-const-arrays` implied by `-D warnings`
1010
= help: to override `-D warnings` add `#[allow(clippy::large_const_arrays)]`
1111

12-
error: allocating a local array larger than 10 bytes
13-
--> tests/ui-toml/array_size_threshold/array_size_threshold.rs:4:25
14-
|
15-
LL | const ABOVE: [u8; 11] = [0; 11];
16-
| ^^^^^^^
17-
|
18-
= help: consider allocating on the heap with `vec![0; 11].into_boxed_slice()`
19-
= note: `-D clippy::large-stack-arrays` implied by `-D warnings`
20-
= help: to override `-D warnings` add `#[allow(clippy::large_stack_arrays)]`
21-
2212
error: allocating a local array larger than 10 bytes
2313
--> tests/ui-toml/array_size_threshold/array_size_threshold.rs:8:17
2414
|
2515
LL | let above = [0u8; 11];
2616
| ^^^^^^^^^
2717
|
2818
= help: consider allocating on the heap with `vec![0u8; 11].into_boxed_slice()`
19+
= note: `-D clippy::large-stack-arrays` implied by `-D warnings`
20+
= help: to override `-D warnings` add `#[allow(clippy::large_stack_arrays)]`
2921

30-
error: aborting due to 3 previous errors
22+
error: aborting due to 2 previous errors
3123

tests/ui/large_stack_arrays.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ enum E {
1515
T(u32),
1616
}
1717

18+
const STATIC_PROMOTED_LARGE_ARRAY: &[u8; 512001] = &[0; 512001];
1819
pub static DOESNOTLINT: [u8; 512_001] = [0; 512_001];
1920
pub static DOESNOTLINT2: [u8; 512_001] = {
2021
let x = 0;

tests/ui/large_stack_arrays.stderr

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: allocating a local array larger than 512000 bytes
2-
--> tests/ui/large_stack_arrays.rs:32:14
2+
--> tests/ui/large_stack_arrays.rs:33:14
33
|
44
LL | let _x = [build(); 3];
55
| ^^^^^^^^^^^^
@@ -9,63 +9,63 @@ LL | let _x = [build(); 3];
99
= help: to override `-D warnings` add `#[allow(clippy::large_stack_arrays)]`
1010

1111
error: allocating a local array larger than 512000 bytes
12-
--> tests/ui/large_stack_arrays.rs:35:14
12+
--> tests/ui/large_stack_arrays.rs:36:14
1313
|
1414
LL | let _y = [build(), build(), build()];
1515
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
1616
|
1717
= help: consider allocating on the heap with `vec![build(), build(), build()].into_boxed_slice()`
1818

1919
error: allocating a local array larger than 512000 bytes
20-
--> tests/ui/large_stack_arrays.rs:41:9
20+
--> tests/ui/large_stack_arrays.rs:42:9
2121
|
2222
LL | [0u32; 20_000_000],
2323
| ^^^^^^^^^^^^^^^^^^
2424
|
2525
= help: consider allocating on the heap with `vec![0u32; 20_000_000].into_boxed_slice()`
2626

2727
error: allocating a local array larger than 512000 bytes
28-
--> tests/ui/large_stack_arrays.rs:43:9
28+
--> tests/ui/large_stack_arrays.rs:44:9
2929
|
3030
LL | [S { data: [0; 32] }; 5000],
3131
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
3232
|
3333
= help: consider allocating on the heap with `vec![S { data: [0; 32] }; 5000].into_boxed_slice()`
3434

3535
error: allocating a local array larger than 512000 bytes
36-
--> tests/ui/large_stack_arrays.rs:45:9
36+
--> tests/ui/large_stack_arrays.rs:46:9
3737
|
3838
LL | [Some(""); 20_000_000],
3939
| ^^^^^^^^^^^^^^^^^^^^^^
4040
|
4141
= help: consider allocating on the heap with `vec![Some(""); 20_000_000].into_boxed_slice()`
4242

4343
error: allocating a local array larger than 512000 bytes
44-
--> tests/ui/large_stack_arrays.rs:47:9
44+
--> tests/ui/large_stack_arrays.rs:48:9
4545
|
4646
LL | [E::T(0); 5000],
4747
| ^^^^^^^^^^^^^^^
4848
|
4949
= help: consider allocating on the heap with `vec![E::T(0); 5000].into_boxed_slice()`
5050

5151
error: allocating a local array larger than 512000 bytes
52-
--> tests/ui/large_stack_arrays.rs:49:9
52+
--> tests/ui/large_stack_arrays.rs:50:9
5353
|
5454
LL | [0u8; usize::MAX],
5555
| ^^^^^^^^^^^^^^^^^
5656
|
5757
= help: consider allocating on the heap with `vec![0u8; usize::MAX].into_boxed_slice()`
5858

5959
error: allocating a local array larger than 512000 bytes
60-
--> tests/ui/large_stack_arrays.rs:93:13
60+
--> tests/ui/large_stack_arrays.rs:94:13
6161
|
6262
LL | let y = [x, x, dummy!(x), x, x];
6363
| ^^^^^^^^^^^^^^^^^^^^^^^
6464
|
6565
= help: consider allocating on the heap with `vec![x, x, dummy!(x), x, x].into_boxed_slice()`
6666

6767
error: allocating a local array larger than 512000 bytes
68-
--> tests/ui/large_stack_arrays.rs:70:13
68+
--> tests/ui/large_stack_arrays.rs:71:13
6969
|
7070
LL | [$a, $b, $a, $b]
7171
| ^^^^^^^^^^^^^^^^
@@ -76,21 +76,21 @@ LL | let y = dummy![x => x];
7676
= note: this error originates in the macro `dummy` (in Nightly builds, run with -Z macro-backtrace for more info)
7777

7878
error: allocating a local array larger than 512000 bytes
79-
--> tests/ui/large_stack_arrays.rs:98:20
79+
--> tests/ui/large_stack_arrays.rs:99:20
8080
|
8181
LL | let y = dummy![[x, x, x, x, x]];
8282
| ^^^^^^^^^^^^^^^
8383
|
8484
= help: consider allocating on the heap with `vec![x, x, x, x, x].into_boxed_slice()`
8585

8686
error: allocating a local array larger than 512000 bytes
87-
--> tests/ui/large_stack_arrays.rs:101:39
87+
--> tests/ui/large_stack_arrays.rs:102:39
8888
|
8989
LL | let y = proc_macros::make_it_big!([x; 1]);
9090
| ^^^^^^
9191

9292
error: allocating a local array larger than 512000 bytes
93-
--> tests/ui/large_stack_arrays.rs:82:23
93+
--> tests/ui/large_stack_arrays.rs:83:23
9494
|
9595
LL | let _x_ = [$id; $n];
9696
| ^^^^^^^^^

0 commit comments

Comments
 (0)