Skip to content

Commit 528dcc3

Browse files
committed
Auto merge of #13620 - GnomedDev:large-const-array-compute-const, r=Manishearth
Fire large_const_arrays for computed array lengths changelog: [`large_const_arrays`]: Lint now fires when the length is determined by an expression
2 parents d09d85d + c5df79d commit 528dcc3

File tree

4 files changed

+21
-9
lines changed

4 files changed

+21
-9
lines changed

clippy_lints/src/large_const_arrays.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use rustc_errors::Applicability;
44
use rustc_hir::{Item, ItemKind};
55
use rustc_lint::{LateContext, LateLintPass};
66
use rustc_middle::ty::layout::LayoutOf;
7-
use rustc_middle::ty::{self, ConstKind};
7+
use rustc_middle::ty::{self, ParamEnv};
88
use rustc_session::impl_lint_pass;
99
use rustc_span::{BytePos, Pos, Span};
1010

@@ -56,7 +56,7 @@ impl<'tcx> LateLintPass<'tcx> for LargeConstArrays {
5656
&& !item.span.from_expansion()
5757
&& let ty = cx.tcx.type_of(item.owner_id).instantiate_identity()
5858
&& let ty::Array(element_type, cst) = ty.kind()
59-
&& let ConstKind::Value(_, ty::ValTree::Leaf(element_count)) = cst.kind()
59+
&& let Ok((_, ty::ValTree::Leaf(element_count))) = cst.eval(cx.tcx, ParamEnv::empty(), item.span)
6060
&& let element_count = element_count.to_target_usize(cx.tcx)
6161
&& let Ok(element_size) = cx.layout_of(*element_type).map(|l| l.size.bytes())
6262
&& u128::from(self.maximum_allowed_size) < u128::from(element_count) * u128::from(element_size)

tests/ui/large_const_arrays.fixed

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@ pub struct S {
99
// Should lint
1010
pub(crate) static FOO_PUB_CRATE: [u32; 1_000_000] = [0u32; 1_000_000];
1111
pub static FOO_PUB: [u32; 1_000_000] = [0u32; 1_000_000];
12+
static FOO_COMPUTED: [u32; 1_000 * 100] = [0u32; 1_000 * 100];
1213
static FOO: [u32; 1_000_000] = [0u32; 1_000_000];
1314

1415
// Good
1516
pub(crate) const G_FOO_PUB_CRATE: [u32; 250] = [0u32; 250];
1617
pub const G_FOO_PUB: [u32; 250] = [0u32; 250];
18+
const G_FOO_COMPUTED: [u32; 25 * 10] = [0u32; 25 * 10];
1719
const G_FOO: [u32; 250] = [0u32; 250];
1820

1921
fn main() {

tests/ui/large_const_arrays.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@ pub struct S {
99
// Should lint
1010
pub(crate) const FOO_PUB_CRATE: [u32; 1_000_000] = [0u32; 1_000_000];
1111
pub const FOO_PUB: [u32; 1_000_000] = [0u32; 1_000_000];
12+
const FOO_COMPUTED: [u32; 1_000 * 100] = [0u32; 1_000 * 100];
1213
const FOO: [u32; 1_000_000] = [0u32; 1_000_000];
1314

1415
// Good
1516
pub(crate) const G_FOO_PUB_CRATE: [u32; 250] = [0u32; 250];
1617
pub const G_FOO_PUB: [u32; 250] = [0u32; 250];
18+
const G_FOO_COMPUTED: [u32; 25 * 10] = [0u32; 25 * 10];
1719
const G_FOO: [u32; 250] = [0u32; 250];
1820

1921
fn main() {

tests/ui/large_const_arrays.stderr

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,58 +20,66 @@ LL | pub const FOO_PUB: [u32; 1_000_000] = [0u32; 1_000_000];
2020
error: large array defined as const
2121
--> tests/ui/large_const_arrays.rs:12:1
2222
|
23+
LL | const FOO_COMPUTED: [u32; 1_000 * 100] = [0u32; 1_000 * 100];
24+
| -----^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
25+
| |
26+
| help: make this a static item: `static`
27+
28+
error: large array defined as const
29+
--> tests/ui/large_const_arrays.rs:13:1
30+
|
2331
LL | const FOO: [u32; 1_000_000] = [0u32; 1_000_000];
2432
| -----^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2533
| |
2634
| help: make this a static item: `static`
2735

2836
error: large array defined as const
29-
--> tests/ui/large_const_arrays.rs:21:5
37+
--> tests/ui/large_const_arrays.rs:23:5
3038
|
3139
LL | pub const BAR_PUB: [u32; 1_000_000] = [0u32; 1_000_000];
3240
| ^^^^-----^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3341
| |
3442
| help: make this a static item: `static`
3543

3644
error: large array defined as const
37-
--> tests/ui/large_const_arrays.rs:22:5
45+
--> tests/ui/large_const_arrays.rs:24:5
3846
|
3947
LL | const BAR: [u32; 1_000_000] = [0u32; 1_000_000];
4048
| -----^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4149
| |
4250
| help: make this a static item: `static`
4351

4452
error: large array defined as const
45-
--> tests/ui/large_const_arrays.rs:23:5
53+
--> tests/ui/large_const_arrays.rs:25:5
4654
|
4755
LL | pub const BAR_STRUCT_PUB: [S; 5_000] = [S { data: [0; 32] }; 5_000];
4856
| ^^^^-----^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4957
| |
5058
| help: make this a static item: `static`
5159

5260
error: large array defined as const
53-
--> tests/ui/large_const_arrays.rs:24:5
61+
--> tests/ui/large_const_arrays.rs:26:5
5462
|
5563
LL | const BAR_STRUCT: [S; 5_000] = [S { data: [0; 32] }; 5_000];
5664
| -----^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5765
| |
5866
| help: make this a static item: `static`
5967

6068
error: large array defined as const
61-
--> tests/ui/large_const_arrays.rs:25:5
69+
--> tests/ui/large_const_arrays.rs:27:5
6270
|
6371
LL | pub const BAR_S_PUB: [Option<&str>; 200_000] = [Some("str"); 200_000];
6472
| ^^^^-----^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6573
| |
6674
| help: make this a static item: `static`
6775

6876
error: large array defined as const
69-
--> tests/ui/large_const_arrays.rs:26:5
77+
--> tests/ui/large_const_arrays.rs:28:5
7078
|
7179
LL | const BAR_S: [Option<&str>; 200_000] = [Some("str"); 200_000];
7280
| -----^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
7381
| |
7482
| help: make this a static item: `static`
7583

76-
error: aborting due to 9 previous errors
84+
error: aborting due to 10 previous errors
7785

0 commit comments

Comments
 (0)