Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
kpreid opened this issue Mar 29, 2024 · 1 comment · Fixed by #12624
Closed

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

kpreid opened this issue Mar 29, 2024 · 1 comment · Fixed by #12624
Assignees
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@kpreid
Copy link
Contributor

kpreid commented Mar 29, 2024

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

@kpreid kpreid added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Mar 29, 2024
@J-ZhengLi
Copy link
Member

@rustbot claim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants