rust-lang / rust-lang/rust-clippy

needless_borrows_for_generic_args on large array

Open
#13,162 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug I-false-positive
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.