RS.CLIPPY.NEEDLESS_BORROWS_FOR_GENERIC_ARGS
Taking a reference that is going to be automatically dereferenced
This checker is a Clippy lint created by The Rust Project Contributors. The documentation shown here is a copy of the original documentation for: needless_borrows_for_generic_args. Copyright ©2025 The Rust Team. All rights reserved.
What it does
Checks for borrow operations (&) that are used as a generic argument to a
function when the borrowed value could be used.
Why is this bad?
Suggests that the receiver of the expression borrows the expression.
Known problems
The lint cannot tell when the implementation of a trait
for &T and T do different things. Removing a borrow
in such a case can change the semantics of the code.
Example
fn f(_: impl AsRef<str>) {}
let x = "foo";
f(&x);
Use instead:
fn f(_: impl AsRef<str>) {}
let x = "foo";
f(x);