Skip to content
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

False positive with clippy::needless-lifetimes when lifetime is used in where clause with assoc type #6159

Closed
WaffleLapkin opened this issue Oct 11, 2020 · 3 comments · Fixed by #6198
Labels
C-bug Category: Clippy is not doing the correct thing I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@WaffleLapkin
Copy link
Member

I tried this code:

use core::ops::Deref;

pub fn apply_deref<'a, T, F, R>(x: &'a T, f: F) -> R
where
    T: Deref,
    F: FnOnce(&'a T::Target) -> R,
{
    f(x.deref())
}

I expected to see this happen: no warnings

Instead, this happened: needless lifetimes warning

warning: explicit lifetimes given in parameter types where they could be elided (or replaced with `'_` if needed by type declaration)
 --> src/lib.rs:3:1
  |
3 | pub fn apply_deref<'a, T, F, R>(x: &'a T, f: F) -> R
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: `#[warn(clippy::needless_lifetimes)]` on by default
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_lifetimes

Meta

  • cargo clippy -V: clippy 0.0.212 (38d911d 2020-10-09)
  • rustc -Vv:
    rustc 1.49.0-nightly (38d911dfc 2020-10-09)
    binary: rustc
    commit-hash: 38d911dfc55a7a1eea1c80139113ed2ff0151087
    commit-date: 2020-10-09
    host: x86_64-unknown-linux-gnu
    release: 1.49.0-nightly
    LLVM version: 11.0
    
@WaffleLapkin WaffleLapkin added the C-bug Category: Clippy is not doing the correct thing label Oct 11, 2020
@ebroto
Copy link
Member

ebroto commented Oct 11, 2020

Removing the lifetime will cause a compilation error if the returned value borrows from the closure parameter or is the closure parameter itself:

use core::ops::Deref;

pub fn apply_deref<T, F, R>(x: &T, f: F) -> R
where
    T: Deref,
    F: FnOnce(&T::Target) -> R,
{
    f(x.deref())
}

fn main() {
    let x: Vec<i32> = vec![];
    apply_deref(&x, |v| v);
}

(playground)

cc @montrivo as you may have an idea of what the problem could be

@ebroto ebroto added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Oct 11, 2020
@tnielens
Copy link
Contributor

This is a regression of #5978. I've prepared a fix.

@tnielens
Copy link
Contributor

The lint bails out on named lifetimes found in the where clause predicates. The regression caused the lint to shortcircuit after the first predicate (T: Deref, in the example) and not walk the following ones (F: FnOnce(&'a T::Target) -> R,).

@bors bors closed this as completed in 38be214 Oct 25, 2020
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-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants