Skip to content

Commit ec2f2d5

Browse files
committed
Auto merge of #10806 - y21:issue10741, r=giraffate
[`large_stack_arrays`]: check array initializer expressions Fixes #10741. Prior to this PR, the lint only checked array repeat expressions (ie. `[T; n]`). Now it also checks array initializer expressions. changelog: [`large_stack_arrays`]: check array initializer expressions
2 parents 7319864 + e9a98d9 commit ec2f2d5

File tree

3 files changed

+37
-8
lines changed

3 files changed

+37
-8
lines changed

clippy_lints/src/large_stack_arrays.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ impl_lint_pass!(LargeStackArrays => [LARGE_STACK_ARRAYS]);
3838

3939
impl<'tcx> LateLintPass<'tcx> for LargeStackArrays {
4040
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
41-
if let ExprKind::Repeat(_, _) = expr.kind
41+
if let ExprKind::Repeat(_, _) | ExprKind::Array(_) = expr.kind
4242
&& let ty::Array(element_type, cst) = cx.typeck_results().expr_ty(expr).kind()
4343
&& let ConstKind::Value(ty::ValTree::Leaf(element_count)) = cst.kind()
4444
&& let Ok(element_count) = element_count.try_to_target_usize(cx.tcx)

tests/ui/large_stack_arrays.rs

+13
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,19 @@ pub static DOESNOTLINT2: [u8; 512_001] = {
1818
[x; 512_001]
1919
};
2020

21+
fn issue_10741() {
22+
#[derive(Copy, Clone)]
23+
struct Large([u32; 100_000]);
24+
25+
fn build() -> Large {
26+
Large([0; 100_000])
27+
}
28+
29+
let _x = [build(); 3];
30+
31+
let _y = [build(), build(), build()];
32+
}
33+
2134
fn main() {
2235
let bad = (
2336
[0u32; 20_000_000],

tests/ui/large_stack_arrays.stderr

+23-7
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,59 @@
11
error: allocating a local array larger than 512000 bytes
2-
--> $DIR/large_stack_arrays.rs:23:9
2+
--> $DIR/large_stack_arrays.rs:29:14
3+
|
4+
LL | let _x = [build(); 3];
5+
| ^^^^^^^^^^^^
6+
|
7+
= help: consider allocating on the heap with `vec![build(); 3].into_boxed_slice()`
8+
= note: `-D clippy::large-stack-arrays` implied by `-D warnings`
9+
10+
error: allocating a local array larger than 512000 bytes
11+
--> $DIR/large_stack_arrays.rs:31:14
12+
|
13+
LL | let _y = [build(), build(), build()];
14+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
15+
|
16+
= help: consider allocating on the heap with `vec![build(), build(), build()].into_boxed_slice()`
17+
18+
error: allocating a local array larger than 512000 bytes
19+
--> $DIR/large_stack_arrays.rs:36:9
320
|
421
LL | [0u32; 20_000_000],
522
| ^^^^^^^^^^^^^^^^^^
623
|
724
= help: consider allocating on the heap with `vec![0u32; 20_000_000].into_boxed_slice()`
8-
= note: `-D clippy::large-stack-arrays` implied by `-D warnings`
925

1026
error: allocating a local array larger than 512000 bytes
11-
--> $DIR/large_stack_arrays.rs:24:9
27+
--> $DIR/large_stack_arrays.rs:37:9
1228
|
1329
LL | [S { data: [0; 32] }; 5000],
1430
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
1531
|
1632
= help: consider allocating on the heap with `vec![S { data: [0; 32] }; 5000].into_boxed_slice()`
1733

1834
error: allocating a local array larger than 512000 bytes
19-
--> $DIR/large_stack_arrays.rs:25:9
35+
--> $DIR/large_stack_arrays.rs:38:9
2036
|
2137
LL | [Some(""); 20_000_000],
2238
| ^^^^^^^^^^^^^^^^^^^^^^
2339
|
2440
= help: consider allocating on the heap with `vec![Some(""); 20_000_000].into_boxed_slice()`
2541

2642
error: allocating a local array larger than 512000 bytes
27-
--> $DIR/large_stack_arrays.rs:26:9
43+
--> $DIR/large_stack_arrays.rs:39:9
2844
|
2945
LL | [E::T(0); 5000],
3046
| ^^^^^^^^^^^^^^^
3147
|
3248
= help: consider allocating on the heap with `vec![E::T(0); 5000].into_boxed_slice()`
3349

3450
error: allocating a local array larger than 512000 bytes
35-
--> $DIR/large_stack_arrays.rs:27:9
51+
--> $DIR/large_stack_arrays.rs:40:9
3652
|
3753
LL | [0u8; usize::MAX],
3854
| ^^^^^^^^^^^^^^^^^
3955
|
4056
= help: consider allocating on the heap with `vec![0u8; usize::MAX].into_boxed_slice()`
4157

42-
error: aborting due to 5 previous errors
58+
error: aborting due to 7 previous errors
4359

0 commit comments

Comments
 (0)