needless_borrowedreference triggers when a lambda is passed to construct a struct, where the type-restriction is only enforced for an implementation. When the type-restriction is eager (like directly to a function), it doesn't appear to trigger. The lint is a false positive because when applying the lint's suggestion, the compiler does not backward-infer that the closure should be a tuple-reference rather than a tuple. The lifetime is specific to the struct and return-type, which can't be (easily) expressed in lambda typing, thus adding `: &(,)` wont work. There are workarounds to properly type the lambda, but are inappropriate fixes.
Checking playground v0.0.1 (/playground)
error: dereferencing a tuple pattern where every element takes a reference
--> src/lib.rs:20:23
|
20 | Wrap(&("blah",), |&(ref v,)| v).call();
| ^^^^^^^^^
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrowed_reference
note: the lint level is defined here
--> src/lib.rs:1:9
|
1 | #![deny(clippy::needless_borrowed_reference)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: try removing the `&` and `ref` parts
|
20 - Wrap(&("blah",), |&(ref v,)| v).call();
20 + Wrap(&("blah",), |(v,)| v).call();
|
Getting this same issue - notably, the suggestion fails to compile (I think there's an issue tag for that?), since without the & rustc assumes it's not a reference at all.
Summary
needless_borrowedreference triggers when a lambda is passed to construct a struct, where the type-restriction is only enforced for an implementation. When the type-restriction is eager (like directly to a function), it doesn't appear to trigger. The lint is a false positive because when applying the lint's suggestion, the compiler does not backward-infer that the closure should be a tuple-reference rather than a tuple. The lifetime is specific to the struct and return-type, which can't be (easily) expressed in lambda typing, thus adding `: &(,)` wont work. There are workarounds to properly type the lambda, but are inappropriate fixes.
Lint Name
needless_borrowed_reference
Reproducer
I tried this code:
(playground)
I saw this happen:
I expected to see this happen:
Version
Additional Labels
No response