Skip to content

needless_borrows_for_generic_args on large array #13162

Open
@fominok

Description

@fominok

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

Metadata

Metadata

Assignees

No one assigned

    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