rust-lang / rust-lang/rust-clippy
needless_borrows_for_generic_args on large array
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
Summary
Hello, I've noticed a Clippy warning that triggers a runtime error if I decide to follow its advice, because the borrow is actually needed to avoid putting the whole data on stack.
Lint Name
needless_borrows_for_generic_args
Reproducer
I tried this code:
static BIG: [u8; 1337420228] = [1; 1337420228];
fn main() {
test(&BIG);
}
fn test<T: AsRef<[u8]>>(_t: T) {}
I saw this happen:
warning: the borrowed expression implements the required traits
--> src/main.rs:4:10
|
4 | test(&BIG);
| ^^^^ help: change this to: `BIG`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrows_for_generic_args
= note: `#[warn(clippy::needless_borrows_for_generic_args)]` on by default
If I follow this advice by removing the borrow:
thread 'main' has overflowed its stack
fatal runtime error: stack overflow
fish: Job 1, 'cargo run' terminated by signal SIGABRT (Abort)
Which is expected
Version
rustc 1.80.0 (051478957 2024-07-21)
binary: rustc
commit-hash: 051478957371ee0084a7c0913941d2a8c4757bb9
commit-date: 2024-07-21
host: x86_64-unknown-linux-gnu
release: 1.80.0
LLVM version: 18.1.7
Additional Labels
No response
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the needless_borrows_for_generic_args warning with the large static array example in the issue. Then inspect the lint's implementation and existing tests to determine how the suggestion handles large arrays; done should prevent a stack-overflowing suggestion while preserving valid diagnostics.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 35/100