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 wrongly detect elidable lifetime with Iterator #11291

Open
mgautierfr opened this issue Aug 3, 2023 · 4 comments
Open

needless_lifetimes wrongly detect elidable lifetime with Iterator #11291

mgautierfr opened this issue Aug 3, 2023 · 4 comments
Assignees
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

@mgautierfr
Copy link

mgautierfr commented Aug 3, 2023

Summary

Clippy wrongly detect a lifetime which could be elided.

Lint Name

needless_lifetimes

Reproducer

I tried this code:

use std::collections::HashMap;

pub struct MyContainer(HashMap<u8, u32>);

impl MyContainer {
    pub fn iter<'a>(&'a self) -> impl Iterator<Item = (&'a u8, &'a u32)> + 'a {
        self.0.iter()
    }
}

I saw this happen:

warning: the following explicit lifetimes could be elided: 'a
 --> test_clippy.rs:6:5
  |
6 |     pub fn iter<'a>(&'a self) -> impl Iterator<Item = (&'a u8, &'a u32)> + '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
  |
6 -     pub fn iter<'a>(&'a self) -> impl Iterator<Item = (&'a u8, &'a u32)> + 'a {
6 +     pub fn iter(&self) -> impl Iterator<Item = (&'_ u8, &'a u32)> + 'a {
  |

warning: 1 warning emitted

I expected to see this happen:

Lifetime 'a cannot be elided. Clippy should not emit a warning.

Version

`clippy-driver -V` : `clippy 0.1.73 (8131b97 2023-08-02)`

`rustc -Vv` : 

rustc 1.73.0-nightly (8131b9774 2023-08-02)
binary: rustc
commit-hash: 8131b9774ebcb6c162fcac71545a13543ec369e7
commit-date: 2023-08-02
host: x86_64-unknown-linux-gnu
release: 1.73.0-nightly
LLVM version: 16.0.5


### Additional Labels

_No response_

<!-- TRIAGEBOT_START -->

<!-- TRIAGEBOT_ASSIGN_START -->

<!-- TRIAGEBOT_ASSIGN_DATA_START$${"user":"PartiallyTyped"}$$TRIAGEBOT_ASSIGN_DATA_END -->

<!-- TRIAGEBOT_ASSIGN_END -->
<!-- TRIAGEBOT_END -->
@mgautierfr mgautierfr 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 Aug 3, 2023
@mgautierfr mgautierfr changed the title needless_lifetimes needless_lifetimes wrongly detect elidable lifetime with Iterator Aug 3, 2023
@Centri3
Copy link
Member

Centri3 commented Aug 3, 2023

No, they can be: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=5f4cb0c7410a0cd73f34c5dba5c2e37a

Looks like the suggestion is just missing a few of them (the + 'a and &'a u32)

@mgautierfr
Copy link
Author

Ho indeed.

I have made this by try/error iterations following rustc and clippy suggestions to end in this situation.
As clippy suggestion is wrong I was supposing that lifetime was necessary. Thanks.

So it seems that the bug is on the faulty suggestion, not the detection.

@Alexendoo Alexendoo added I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied and removed I-false-positive Issue: The lint was triggered on code it shouldn't have labels Aug 3, 2023
@m-rph
Copy link
Contributor

m-rph commented Feb 12, 2024

@rustbot claim

@m-rph
Copy link
Contributor

m-rph commented Mar 29, 2024

The problem with this lint is with the collection of lifetimes within the "inner" types.

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.

4 participants