Skip to content

clippy::large_stack_arrays reports vec! literals and provides a useless suggestion #12586

Closed
@kpreid

Description

@kpreid

Summary

When the array-size-threshold configuration value is small or the array elements are large, clippy::large_stack_arrays will lint on a vec! expression. It then proposes using vec![...].into_boxed_slice(), which is unhelpful: the boxed slice in question is itself inside the vec! macro expansion, so continuing to use vec![] but with more conversions won't do anything.

Additionally, this suggestion seems like it cannot help even a normal array construction, since even if this were a literal array, switching to vec![] would not take it off the stack totally, just after construction, so the lint would continue to fire (correctly) — unless we are using [value; len] syntax. In that case only, changing to vec! would avoid putting any large array on the stack.

Lint Name

large_stack_arrays

Reproducer

I tried this code:

fn foo() {
    #![warn(clippy::large_stack_arrays)]

    let x = [0u8; 65000];
    let y = vec![x, x, x, x, x, x, x, x];
}

I saw this happen:

warning: allocating a local array larger than 512000 bytes
 --> src/lib.rs:5:13
  |
5 |     let y = vec![x, x, x, x, x, x, x, x];
  |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = help: consider allocating on the heap with `vec![$($x),+].into_boxed_slice()`
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#large_stack_arrays
note: the lint level is defined here
 --> src/lib.rs:2:13
  |
2 |     #![warn(clippy::large_stack_arrays)]
  |             ^^^^^^^^^^^^^^^^^^^^^^^^^^
  = note: this warning originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info)

I expected to see this happen: Either no recommendation, or a recommendation that actually avoids the array going on the stack.

Version

rustc 1.79.0-nightly (c9f8f3438 2024-03-27)
binary: rustc
commit-hash: c9f8f3438a8134a413aa5d4903e0196e44e37bbc
commit-date: 2024-03-27
host: x86_64-apple-darwin
release: 1.79.0-nightly
LLVM version: 18.1.2

Additional Labels

No response

Metadata

Metadata

Assignees

Labels

C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't have

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions