Skip to content

Commit 05f3793

Browse files
committed
Auto merge of #13534 - GnomedDev:fix-large-stack-arrays-nested-item, r=flip1995
Fix large_stack_arrays triggering when nesting const items Fixes #13529. r? `@flip1995` changelog: [`large_stack_arrays`]: No longer triggers in static/const context when using nested items
2 parents 14c05c9 + 87b4c1f commit 05f3793

File tree

3 files changed

+24
-17
lines changed

3 files changed

+24
-17
lines changed

clippy_lints/src/large_stack_arrays.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::num::Saturating;
2+
13
use clippy_config::Conf;
24
use clippy_utils::diagnostics::span_lint_and_then;
35
use clippy_utils::is_from_proc_macro;
@@ -30,15 +32,15 @@ declare_clippy_lint! {
3032
pub struct LargeStackArrays {
3133
maximum_allowed_size: u64,
3234
prev_vec_macro_callsite: Option<Span>,
33-
is_in_const_item: bool,
35+
const_item_counter: Saturating<u16>,
3436
}
3537

3638
impl LargeStackArrays {
3739
pub fn new(conf: &'static Conf) -> Self {
3840
Self {
3941
maximum_allowed_size: conf.array_size_threshold,
4042
prev_vec_macro_callsite: None,
41-
is_in_const_item: false,
43+
const_item_counter: Saturating(0),
4244
}
4345
}
4446

@@ -64,18 +66,18 @@ impl_lint_pass!(LargeStackArrays => [LARGE_STACK_ARRAYS]);
6466
impl<'tcx> LateLintPass<'tcx> for LargeStackArrays {
6567
fn check_item(&mut self, _: &LateContext<'tcx>, item: &'tcx Item<'tcx>) {
6668
if matches!(item.kind, ItemKind::Static(..) | ItemKind::Const(..)) {
67-
self.is_in_const_item = true;
69+
self.const_item_counter += 1;
6870
}
6971
}
7072

7173
fn check_item_post(&mut self, _: &LateContext<'tcx>, item: &'tcx Item<'tcx>) {
7274
if matches!(item.kind, ItemKind::Static(..) | ItemKind::Const(..)) {
73-
self.is_in_const_item = false;
75+
self.const_item_counter -= 1;
7476
}
7577
}
7678

7779
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &Expr<'tcx>) {
78-
if !self.is_in_const_item
80+
if self.const_item_counter.0 == 0
7981
&& let ExprKind::Repeat(_, _) | ExprKind::Array(_) = expr.kind
8082
&& !self.is_from_vec_macro(cx, expr.span)
8183
&& let ty::Array(element_type, cst) = cx.typeck_results().expr_ty(expr).kind()

tests/ui/large_stack_arrays.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ enum E {
1616
}
1717

1818
const STATIC_PROMOTED_LARGE_ARRAY: &[u8; 512001] = &[0; 512001];
19+
const STATIC_PROMOTED_LARGE_ARRAY_WITH_NESTED: &[u8; 512001] = {
20+
const NESTED: () = ();
21+
&[0; 512001]
22+
};
23+
1924
pub static DOESNOTLINT: [u8; 512_001] = [0; 512_001];
2025
pub static DOESNOTLINT2: [u8; 512_001] = {
2126
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 16384 bytes
2-
--> tests/ui/large_stack_arrays.rs:33:14
2+
--> tests/ui/large_stack_arrays.rs:38: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 16384 bytes
12-
--> tests/ui/large_stack_arrays.rs:36:14
12+
--> tests/ui/large_stack_arrays.rs:41: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 16384 bytes
20-
--> tests/ui/large_stack_arrays.rs:42:9
20+
--> tests/ui/large_stack_arrays.rs:47: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 16384 bytes
28-
--> tests/ui/large_stack_arrays.rs:44:9
28+
--> tests/ui/large_stack_arrays.rs:49: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 16384 bytes
36-
--> tests/ui/large_stack_arrays.rs:46:9
36+
--> tests/ui/large_stack_arrays.rs:51: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 16384 bytes
44-
--> tests/ui/large_stack_arrays.rs:48:9
44+
--> tests/ui/large_stack_arrays.rs:53: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 16384 bytes
52-
--> tests/ui/large_stack_arrays.rs:50:9
52+
--> tests/ui/large_stack_arrays.rs:55: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 16384 bytes
60-
--> tests/ui/large_stack_arrays.rs:94:13
60+
--> tests/ui/large_stack_arrays.rs:99: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 16384 bytes
68-
--> tests/ui/large_stack_arrays.rs:71:13
68+
--> tests/ui/large_stack_arrays.rs:76: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 16384 bytes
79-
--> tests/ui/large_stack_arrays.rs:99:20
79+
--> tests/ui/large_stack_arrays.rs:104: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 16384 bytes
87-
--> tests/ui/large_stack_arrays.rs:102:39
87+
--> tests/ui/large_stack_arrays.rs:107:39
8888
|
8989
LL | let y = proc_macros::make_it_big!([x; 1]);
9090
| ^^^^^^
9191

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

0 commit comments

Comments
 (0)