Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions clippy_lints/src/large_const_arrays.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use rustc_errors::Applicability;
use rustc_hir::{Item, ItemKind};
use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::ty::layout::LayoutOf;
use rustc_middle::ty::{self, ConstKind};
use rustc_middle::ty::{self, ParamEnv};
use rustc_session::impl_lint_pass;
use rustc_span::{BytePos, Pos, Span};

Expand Down Expand Up @@ -56,7 +56,7 @@ impl<'tcx> LateLintPass<'tcx> for LargeConstArrays {
&& !item.span.from_expansion()
&& let ty = cx.tcx.type_of(item.owner_id).instantiate_identity()
&& let ty::Array(element_type, cst) = ty.kind()
&& let ConstKind::Value(_, ty::ValTree::Leaf(element_count)) = cst.kind()
&& let Ok((_, ty::ValTree::Leaf(element_count))) = cst.eval(cx.tcx, ParamEnv::empty(), item.span)
&& let element_count = element_count.to_target_usize(cx.tcx)
&& let Ok(element_size) = cx.layout_of(*element_type).map(|l| l.size.bytes())
&& u128::from(self.maximum_allowed_size) < u128::from(element_count) * u128::from(element_size)
Expand Down
2 changes: 2 additions & 0 deletions tests/ui/large_const_arrays.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ pub struct S {
// Should lint
pub(crate) static FOO_PUB_CRATE: [u32; 1_000_000] = [0u32; 1_000_000];
pub static FOO_PUB: [u32; 1_000_000] = [0u32; 1_000_000];
static FOO_COMPUTED: [u32; 1_000 * 100] = [0u32; 1_000 * 100];
static FOO: [u32; 1_000_000] = [0u32; 1_000_000];

// Good
pub(crate) const G_FOO_PUB_CRATE: [u32; 250] = [0u32; 250];
pub const G_FOO_PUB: [u32; 250] = [0u32; 250];
const G_FOO_COMPUTED: [u32; 25 * 10] = [0u32; 25 * 10];
const G_FOO: [u32; 250] = [0u32; 250];

fn main() {
Expand Down
2 changes: 2 additions & 0 deletions tests/ui/large_const_arrays.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ pub struct S {
// Should lint
pub(crate) const FOO_PUB_CRATE: [u32; 1_000_000] = [0u32; 1_000_000];
pub const FOO_PUB: [u32; 1_000_000] = [0u32; 1_000_000];
const FOO_COMPUTED: [u32; 1_000 * 100] = [0u32; 1_000 * 100];
const FOO: [u32; 1_000_000] = [0u32; 1_000_000];

// Good
pub(crate) const G_FOO_PUB_CRATE: [u32; 250] = [0u32; 250];
pub const G_FOO_PUB: [u32; 250] = [0u32; 250];
const G_FOO_COMPUTED: [u32; 25 * 10] = [0u32; 25 * 10];
const G_FOO: [u32; 250] = [0u32; 250];

fn main() {
Expand Down
22 changes: 15 additions & 7 deletions tests/ui/large_const_arrays.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -20,58 +20,66 @@ LL | pub const FOO_PUB: [u32; 1_000_000] = [0u32; 1_000_000];
error: large array defined as const
--> tests/ui/large_const_arrays.rs:12:1
|
LL | const FOO_COMPUTED: [u32; 1_000 * 100] = [0u32; 1_000 * 100];
| -----^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| help: make this a static item: `static`

error: large array defined as const
--> tests/ui/large_const_arrays.rs:13:1
|
LL | const FOO: [u32; 1_000_000] = [0u32; 1_000_000];
| -----^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| help: make this a static item: `static`

error: large array defined as const
--> tests/ui/large_const_arrays.rs:21:5
--> tests/ui/large_const_arrays.rs:23:5
|
LL | pub const BAR_PUB: [u32; 1_000_000] = [0u32; 1_000_000];
| ^^^^-----^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| help: make this a static item: `static`

error: large array defined as const
--> tests/ui/large_const_arrays.rs:22:5
--> tests/ui/large_const_arrays.rs:24:5
|
LL | const BAR: [u32; 1_000_000] = [0u32; 1_000_000];
| -----^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| help: make this a static item: `static`

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

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

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

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

error: aborting due to 9 previous errors
error: aborting due to 10 previous errors