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

needless_lifetimes suggestion forgets to remove some lifetimes #10495

Open
matthiaskrgr opened this issue Mar 12, 2023 · 1 comment
Open

needless_lifetimes suggestion forgets to remove some lifetimes #10495

matthiaskrgr opened this issue Mar 12, 2023 · 1 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

Comments

@matthiaskrgr
Copy link
Member

Summary

clippy leaves in one of lifetimes and thus the code fails to compile

9 - fn f<'a>(PhantomData::<&'a u8>: PhantomData<&'a u8>) {}
9 + fn f(PhantomData::<&'a u8>: PhantomData<&u8>) {}

Reproducer

I tried this code:

#![allow(dead_code)]
use std::marker::PhantomData;

fn f<'a>(PhantomData::<&'a u8>: PhantomData<&'a u8>) {}

fn main() {}

I expected to see this happen:

warning: the following explicit lifetimes could be elided: 'a
 --> src/main.rs:9:1
  |
9 | fn f<'a>(PhantomData::<&'a u8>: PhantomData<&'a u8>) {}
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_lifetimes
  = note: `#[warn(clippy::needless_lifetimes)]` on by default
help: elide the lifetimes
  |
9 - fn f<'a>(PhantomData::<&'a u8>: PhantomData<&'a u8>) {}
9 + fn f(PhantomData::<&u8>: PhantomData<&u8>) {}

Instead, this happened:

warning: the following explicit lifetimes could be elided: 'a
 --> src/main.rs:9:1
  |
9 | fn f<'a>(PhantomData::<&'a u8>: PhantomData<&'a u8>) {}
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_lifetimes
  = note: `#[warn(clippy::needless_lifetimes)]` on by default
help: elide the lifetimes
  |
9 - fn f<'a>(PhantomData::<&'a u8>: PhantomData<&'a u8>) {}
9 + fn f(PhantomData::<&'a u8>: PhantomData<&u8>) {}

Version

rustc 1.70.0-nightly (8a73f50d8 2023-03-11)
binary: rustc
commit-hash: 8a73f50d875840b8077b8ec080fa41881d7ce40d
commit-date: 2023-03-11
host: x86_64-unknown-linux-gnu
release: 1.70.0-nightly
LLVM version: 15.0.7

Additional Labels

No response

@matthiaskrgr matthiaskrgr added 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 labels Mar 12, 2023
@DaniPopes
Copy link
Contributor

DaniPopes commented Apr 19, 2023

Encountered the same with an impl in return position:

pub fn f<'a>(slice: &'a [u8]) -> impl Iterator<Item = &'a u8> + 'a {
    slice.iter()
}

needless_lifetimes suggestion:

warning: the following explicit lifetimes could be elided: 'a
 --> src/main.rs:1:1
  |
1 | pub fn f<'a>(slice: &'a [u8]) -> impl Iterator<Item = &'a u8> + 'a {
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_lifetimes
  = note: `#[warn(clippy::needless_lifetimes)]` on by default
help: elide the lifetimes
  |
1 - pub fn f<'a>(slice: &'a [u8]) -> impl Iterator<Item = &'a u8> + 'a {
1 + pub fn f(slice: &[u8]) -> impl Iterator<Item = &'_ u8> + 'a {
  |

Reported errors:

The following errors were reported:
error[E0261]: use of undeclared lifetime name `'a`
 --> src/main.rs:1:58
  |
1 | pub fn f(slice: &[u8]) -> impl Iterator<Item = &'_ u8> + 'a {
  |         -                                                ^^ undeclared lifetime
  |         |
  |         help: consider introducing lifetime `'a` here: `<'a>`

error: lifetime may not live long enough
 --> src/main.rs:2:5
  |
1 | pub fn f(slice: &[u8]) -> impl Iterator<Item = &'_ u8> + 'a {
  |                 - let's call the lifetime of this reference `'1`
2 |     slice.iter()
  |     ^^^^^^^^^^^^ returning this value requires that `'1` must outlive `'static`
  |
help: to declare that `impl std::iter::Iterator<Item = &u8> + '_` captures data from argument `slice`, you can add an explicit `'_` lifetime bound
  |
1 | pub fn f(slice: &[u8]) -> impl Iterator<Item = &'_ u8> + 'a + '_ {
  |                                                             ++++

Version

rustc 1.71.0-nightly (7908a1d65 2023-04-17)
binary: rustc
commit-hash: 7908a1d65496b88626e4b7c193c81d777005d6f3
commit-date: 2023-04-17
host: x86_64-unknown-linux-gnu
release: 1.71.0-nightly
LLVM version: 16.0.2

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

No branches or pull requests

2 participants