Skip to content

Commit 2d2e8cc

Browse files
committed
Auto merge of rust-lang#13485 - GnomedDev:reduce-large-threshold, r=xFrednet
Reduce default 'large array' threshold As-is this threshold is `512kb`, but as rust-lang#9449 points out this is way too high for most people to consider sensible (why would you want to copy `256kb` of data around on the stack or duplicate it via `const`) and didn't get any discussion when originally added. This PR reduces it the threshold to `1kb`, which is higher than the issue says ("a few cpu words") but helps out for actual codebases. While reducing this, I found that `large_stack_arrays` was triggering for statically promoted arrays in constants/statics, so I also fixed that up as seen in the difference to [array_size_threshold.stderr](https://github.com/rust-lang/rust-clippy/compare/master...GnomedDev:rust-clippy:reduce-large-threshold?expand=1#diff-4c2a2a855d9ff7777f1d385be0c1bede2a3fc8aaab94837cde27a35235233fc7). Closes rust-lang#9449. changelog: [`large_stack_arrays`]: No longer triggers in `static`/`const` context changelog: [`large_const_arrays`]: Changed the default of [`array-size-threshold`] from `512kb` to `16kb`
2 parents 753629b + 995eb95 commit 2d2e8cc

File tree

9 files changed

+84
-76
lines changed

9 files changed

+84
-76
lines changed

book/src/lint_configuration.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ arithmetic-side-effects-allowed-unary = ["SomeType", "AnotherType"]
329329
## `array-size-threshold`
330330
The maximum allowed size for arrays on the stack
331331

332-
**Default Value:** `512000`
332+
**Default Value:** `16384`
333333

334334
---
335335
**Affected lints:**

clippy_config/src/conf.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ define_Conf! {
366366
arithmetic_side_effects_allowed_unary: Vec<String> = <_>::default(),
367367
/// The maximum allowed size for arrays on the stack
368368
#[lints(large_const_arrays, large_stack_arrays)]
369-
array_size_threshold: u64 = 512_000,
369+
array_size_threshold: u64 = 16 * 1024,
370370
/// Suppress lints whenever the suggested change would cause breakage for other crates.
371371
#[lints(
372372
box_collection,

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()

clippy_lints/src/methods/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5182,7 +5182,7 @@ impl ShouldImplTraitCase {
51825182
}
51835183

51845184
#[rustfmt::skip]
5185-
const TRAIT_METHODS: [ShouldImplTraitCase; 30] = [
5185+
static TRAIT_METHODS: [ShouldImplTraitCase; 30] = [
51865186
ShouldImplTraitCase::new("std::ops::Add", "add", 2, FN_HEADER, SelfKind::Value, OutType::Any, true),
51875187
ShouldImplTraitCase::new("std::convert::AsMut", "as_mut", 1, FN_HEADER, SelfKind::RefMut, OutType::Ref, true),
51885188
ShouldImplTraitCase::new("std::convert::AsRef", "as_ref", 1, FN_HEADER, SelfKind::Ref, OutType::Ref, true),

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_const_arrays.fixed

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ pub static FOO_PUB: [u32; 1_000_000] = [0u32; 1_000_000];
1212
static FOO: [u32; 1_000_000] = [0u32; 1_000_000];
1313

1414
// Good
15-
pub(crate) const G_FOO_PUB_CRATE: [u32; 1_000] = [0u32; 1_000];
16-
pub const G_FOO_PUB: [u32; 1_000] = [0u32; 1_000];
17-
const G_FOO: [u32; 1_000] = [0u32; 1_000];
15+
pub(crate) const G_FOO_PUB_CRATE: [u32; 250] = [0u32; 250];
16+
pub const G_FOO_PUB: [u32; 250] = [0u32; 250];
17+
const G_FOO: [u32; 250] = [0u32; 250];
1818

1919
fn main() {
2020
// Should lint
@@ -26,10 +26,10 @@ fn main() {
2626
static BAR_S: [Option<&str>; 200_000] = [Some("str"); 200_000];
2727

2828
// Good
29-
pub const G_BAR_PUB: [u32; 1_000] = [0u32; 1_000];
30-
const G_BAR: [u32; 1_000] = [0u32; 1_000];
31-
pub const G_BAR_STRUCT_PUB: [S; 500] = [S { data: [0; 32] }; 500];
32-
const G_BAR_STRUCT: [S; 500] = [S { data: [0; 32] }; 500];
33-
pub const G_BAR_S_PUB: [Option<&str>; 200] = [Some("str"); 200];
34-
const G_BAR_S: [Option<&str>; 200] = [Some("str"); 200];
29+
pub const G_BAR_PUB: [u32; 250] = [0u32; 250];
30+
const G_BAR: [u32; 250] = [0u32; 250];
31+
pub const G_BAR_STRUCT_PUB: [S; 4] = [S { data: [0; 32] }; 4];
32+
const G_BAR_STRUCT: [S; 4] = [S { data: [0; 32] }; 4];
33+
pub const G_BAR_S_PUB: [Option<&str>; 50] = [Some("str"); 50];
34+
const G_BAR_S: [Option<&str>; 50] = [Some("str"); 50];
3535
}

tests/ui/large_const_arrays.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ pub const FOO_PUB: [u32; 1_000_000] = [0u32; 1_000_000];
1212
const FOO: [u32; 1_000_000] = [0u32; 1_000_000];
1313

1414
// Good
15-
pub(crate) const G_FOO_PUB_CRATE: [u32; 1_000] = [0u32; 1_000];
16-
pub const G_FOO_PUB: [u32; 1_000] = [0u32; 1_000];
17-
const G_FOO: [u32; 1_000] = [0u32; 1_000];
15+
pub(crate) const G_FOO_PUB_CRATE: [u32; 250] = [0u32; 250];
16+
pub const G_FOO_PUB: [u32; 250] = [0u32; 250];
17+
const G_FOO: [u32; 250] = [0u32; 250];
1818

1919
fn main() {
2020
// Should lint
@@ -26,10 +26,10 @@ fn main() {
2626
const BAR_S: [Option<&str>; 200_000] = [Some("str"); 200_000];
2727

2828
// Good
29-
pub const G_BAR_PUB: [u32; 1_000] = [0u32; 1_000];
30-
const G_BAR: [u32; 1_000] = [0u32; 1_000];
31-
pub const G_BAR_STRUCT_PUB: [S; 500] = [S { data: [0; 32] }; 500];
32-
const G_BAR_STRUCT: [S; 500] = [S { data: [0; 32] }; 500];
33-
pub const G_BAR_S_PUB: [Option<&str>; 200] = [Some("str"); 200];
34-
const G_BAR_S: [Option<&str>; 200] = [Some("str"); 200];
29+
pub const G_BAR_PUB: [u32; 250] = [0u32; 250];
30+
const G_BAR: [u32; 250] = [0u32; 250];
31+
pub const G_BAR_STRUCT_PUB: [S; 4] = [S { data: [0; 32] }; 4];
32+
const G_BAR_STRUCT: [S; 4] = [S { data: [0; 32] }; 4];
33+
pub const G_BAR_S_PUB: [Option<&str>; 50] = [Some("str"); 50];
34+
const G_BAR_S: [Option<&str>; 50] = [Some("str"); 50];
3535
}

tests/ui/large_stack_arrays.rs

Lines changed: 20 additions & 19 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;
@@ -23,38 +24,38 @@ pub static DOESNOTLINT2: [u8; 512_001] = {
2324

2425
fn issue_10741() {
2526
#[derive(Copy, Clone)]
26-
struct Large([u32; 100_000]);
27+
struct Large([u32; 2048]);
2728

2829
fn build() -> Large {
29-
Large([0; 100_000])
30+
Large([0; 2048])
3031
}
3132

3233
let _x = [build(); 3];
33-
//~^ ERROR: allocating a local array larger than 512000 bytes
34+
//~^ ERROR: allocating a local array larger than 16384 bytes
3435

3536
let _y = [build(), build(), build()];
36-
//~^ ERROR: allocating a local array larger than 512000 bytes
37+
//~^ ERROR: allocating a local array larger than 16384 bytes
3738
}
3839

3940
fn main() {
4041
let bad = (
4142
[0u32; 20_000_000],
42-
//~^ ERROR: allocating a local array larger than 512000 bytes
43+
//~^ ERROR: allocating a local array larger than 16384 bytes
4344
[S { data: [0; 32] }; 5000],
44-
//~^ ERROR: allocating a local array larger than 512000 bytes
45+
//~^ ERROR: allocating a local array larger than 16384 bytes
4546
[Some(""); 20_000_000],
46-
//~^ ERROR: allocating a local array larger than 512000 bytes
47+
//~^ ERROR: allocating a local array larger than 16384 bytes
4748
[E::T(0); 5000],
48-
//~^ ERROR: allocating a local array larger than 512000 bytes
49+
//~^ ERROR: allocating a local array larger than 16384 bytes
4950
[0u8; usize::MAX],
50-
//~^ ERROR: allocating a local array larger than 512000 bytes
51+
//~^ ERROR: allocating a local array larger than 16384 bytes
5152
);
5253

5354
let good = (
54-
[0u32; 1000],
55-
[S { data: [0; 32] }; 1000],
56-
[Some(""); 1000],
57-
[E::T(0); 1000],
55+
[0u32; 50],
56+
[S { data: [0; 32] }; 4],
57+
[Some(""); 50],
58+
[E::T(0); 2],
5859
[(); 20_000_000],
5960
);
6061
}
@@ -68,7 +69,7 @@ fn issue_12586() {
6869
// Weird rule to test help messages.
6970
($a:expr => $b:expr) => {
7071
[$a, $b, $a, $b]
71-
//~^ ERROR: allocating a local array larger than 512000 bytes
72+
//~^ ERROR: allocating a local array larger than 16384 bytes
7273
};
7374
($id:ident; $n:literal) => {
7475
dummy!(::std::vec![$id;$n])
@@ -80,26 +81,26 @@ fn issue_12586() {
8081
macro_rules! create_then_move {
8182
($id:ident; $n:literal) => {{
8283
let _x_ = [$id; $n];
83-
//~^ ERROR: allocating a local array larger than 512000 bytes
84+
//~^ ERROR: allocating a local array larger than 16384 bytes
8485
_x_
8586
}};
8687
}
8788

88-
let x = [0u32; 50_000];
89+
let x = [0u32; 4096];
8990
let y = vec![x, x, x, x, x];
9091
let y = vec![dummy![x, x, x, x, x]];
9192
let y = vec![dummy![[x, x, x, x, x]]];
9293
let y = dummy![x, x, x, x, x];
9394
let y = [x, x, dummy!(x), x, x];
94-
//~^ ERROR: allocating a local array larger than 512000 bytes
95+
//~^ ERROR: allocating a local array larger than 16384 bytes
9596
let y = dummy![x => x];
9697
let y = dummy![x;5];
9798
let y = dummy!(vec![dummy![x, x, x, x, x]]);
9899
let y = dummy![[x, x, x, x, x]];
99-
//~^ ERROR: allocating a local array larger than 512000 bytes
100+
//~^ ERROR: allocating a local array larger than 16384 bytes
100101

101102
let y = proc_macros::make_it_big!([x; 1]);
102-
//~^ ERROR: allocating a local array larger than 512000 bytes
103+
//~^ ERROR: allocating a local array larger than 16384 bytes
103104
let y = vec![proc_macros::make_it_big!([x; 10])];
104105
let y = vec![create_then_move![x; 5]; 5];
105106
}

tests/ui/large_stack_arrays.stderr

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

11-
error: allocating a local array larger than 512000 bytes
12-
--> tests/ui/large_stack_arrays.rs:35:14
11+
error: allocating a local array larger than 16384 bytes
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

19-
error: allocating a local array larger than 512000 bytes
20-
--> tests/ui/large_stack_arrays.rs:41:9
19+
error: allocating a local array larger than 16384 bytes
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

27-
error: allocating a local array larger than 512000 bytes
28-
--> tests/ui/large_stack_arrays.rs:43:9
27+
error: allocating a local array larger than 16384 bytes
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

35-
error: allocating a local array larger than 512000 bytes
36-
--> tests/ui/large_stack_arrays.rs:45:9
35+
error: allocating a local array larger than 16384 bytes
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

43-
error: allocating a local array larger than 512000 bytes
44-
--> tests/ui/large_stack_arrays.rs:47:9
43+
error: allocating a local array larger than 16384 bytes
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

51-
error: allocating a local array larger than 512000 bytes
52-
--> tests/ui/large_stack_arrays.rs:49:9
51+
error: allocating a local array larger than 16384 bytes
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

59-
error: allocating a local array larger than 512000 bytes
60-
--> tests/ui/large_stack_arrays.rs:93:13
59+
error: allocating a local array larger than 16384 bytes
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

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

78-
error: allocating a local array larger than 512000 bytes
79-
--> tests/ui/large_stack_arrays.rs:98:20
78+
error: allocating a local array larger than 16384 bytes
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

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

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

0 commit comments

Comments
 (0)