Skip to content

needless_borrows_for_generic_args incorrectly triggers on &[] #15333

@progval

Description

@progval

Summary

Clippy warns on passing &[] to a function that expects impl IntoIterator<Item: Borrow<usize>> even though it's the shortest way to pass it an empty intoiterator, and the suggested fix does not compile

Lint Name

needless_borrows_for_generic_args

Reproducer

I tried this code:

use std::borrow::Borrow;

fn f(_: impl IntoIterator<Item: Borrow<usize>>) {}

fn main() {
    f(&[]);
}

(playground)

I saw this happen:

    Checking playground v0.0.1 (/playground)
warning: the borrowed expression implements the required traits
 --> src/main.rs:6:7
  |
6 |     f(&[]);
  |       ^^^ help: change this to: `[]`
  |
  = 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

warning: `playground` (bin "playground") generated 1 warning (run `cargo clippy --fix --bin "playground"` to apply 1 suggestion)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.69s

However, applying the suggest leads to a compilation error

   Compiling playground v0.0.1 (/playground)
error[E0283]: type annotations needed
 --> src/main.rs:6:5
  |
6 |     f([]);
  |     ^^^^^ cannot infer type
  |
  = note: cannot satisfy `_: Borrow<usize>`
note: required by a bound in `f`
 --> src/main.rs:3:33
  |
3 | fn f(_: impl IntoIterator<Item: Borrow<usize>>) {}
  |                                 ^^^^^^^^^^^^^ required by this bound in `f`

For more information about this error, try `rustc --explain E0283`.
error: could not compile `playground` (bin "playground") due to 1 previous error

Instead, Clippy should consider this code valid

Version

rustc 1.87.0 (17067e9ac 2025-05-09)
binary: rustc
commit-hash: 17067e9ac6d7ecb70e50f92c1944e545188d2359
commit-date: 2025-05-09
host: x86_64-unknown-linux-gnu
release: 1.87.0
LLVM version: 20.1.1

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