Skip to content

needless_borrowed_reference for lambdas not immediately typed #11566

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Wolvereness opened this issue Sep 25, 2023 · 1 comment
Open

needless_borrowed_reference for lambdas not immediately typed #11566

Wolvereness opened this issue Sep 25, 2023 · 1 comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@Wolvereness
Copy link

Summary

needless_borrowed_reference 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)

#![deny(clippy::needless_borrowed_reference)]
#![allow(dead_code)]

struct Wrap<'a, L, R>(&'a (L,), R);

trait Callable {
    fn call(&self);
}

impl<
    'a,
    T,
    F: Fn(&'a (T,)) -> V,
    V,
> Callable for Wrap<'a, T, F> {
    fn call(&self) {}
}

fn test() {
    Wrap(&("blah",), |&(ref v,)| v).call();
}

I saw this happen:

    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();
   |

I expected to see this happen:

Version

0.1.74 (2023-09-24 37390d6)

Additional Labels

No response

@Wolvereness Wolvereness added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Sep 25, 2023
@coolreader18
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have
Projects
None yet
Development

No branches or pull requests

2 participants