rust-clippy icon indicating copy to clipboard operation
rust-clippy copied to clipboard

Fix output lifetime collection in needless_lifetimes

Open PartiallyUntyped opened this issue 1 year ago • 5 comments

This is currently draft as I am unsure about how to go with fixing this. I don't completely understand the code D:, but I've identified the issue more or less.

The problem

In the following snippet

mod issue11291 {
    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 {
            //~^ ERROR: the following explicit lifetimes could be elided: 'a
            self.0.iter()
        }
    }
}

Clippy will suggest:

 pub fn iter<'a>(&'a self) -> impl Iterator<Item = (&'_ u8, &'a u32)> + 'a

Which misses the 2 latter lifetimes.

I probed a bit into the implementation. The lifetimes of the output are collected via a walker. The implementation of the walker is identical for both input and output declaration.

The problem seems related to the truncation in TyKind::OpaqueDef

https://github.com/rust-lang/rust-clippy/blob/86717f2f0c9630e51d8ab8fefb997db89d38ad8f/clippy_lints/src/lifetimes.rs#L532-L542

Without the truncation, all lifetimes are collected, but going by hir::DefId, we have 4 different lifetimes (LTs) instead of 3. Going by span, we have 3 LTs.

Removing the truncation and keeping only unique LT by their span causes something to break in the linting logic.

Update 1:

It seems that the issue is related to the defids of the LTs, as they can't be found in the allowed LTs.

Update 2:

Yup, using localdefid causes issues with opaque types. Using identifiers for any checks works better and the types are correlated selected. need to fix how the suggestions work to account for that, but otherwise it seems correct-ish.

changelog: [`needless_lifetimes`]: Suggested fix will now elide all output lifetimes.

fixes: #12085
fixes: #11291

r? @blyxyas

PartiallyUntyped avatar Mar 10 '24 19:03 PartiallyUntyped

Hey @m-rph, this is a ping from triage, since there hasn't been any activity in some time. Are you still planning to continue this implementation?

@rustbot author

xFrednet avatar Jun 20 '24 18:06 xFrednet

@xFrednet yup. Need to find time. There's 5 issues that need to be addressed and will do all of them in 1 ~~rust~~ go.

PartiallyUntyped avatar Jun 20 '24 18:06 PartiallyUntyped

Okay, just here to poke you a bit ^^ Let me know if you need any help.

error: found time
  --> https://github.com/rust-lang/rust-clippy/pull/12456/comment-5/1:29
   |
   | @xFrednet yup. Need to find time. 
   |                             ^^^^
   = note: This `time` might be a different type then the
           one you where refering to

Sorry, couldn't resist after your comment :P

xFrednet avatar Jun 20 '24 18:06 xFrednet

The assignment was a test. I'm unassigning myself again. Thank you for the help!

xFrednet avatar Jun 25 '24 17:06 xFrednet

With Alejandra we discussed whether I could rewrite the lint given how old it was, both options are on the table. Seeing that I have accumulated a large collection of issues around needless_lifetimes, I think it's best to just go ahead with the rewrite.

PartiallyUntyped avatar Jun 27 '24 07:06 PartiallyUntyped

Hey, @m-rph this is a ping from triage, since there hasn't been any activity in some time. Are you still planning to continue this implementation?

Also, is this PR maybe outdated, since you crated https://github.com/rust-lang/rust-clippy/pull/13141?

@rustbot author

xFrednet avatar Jul 23 '24 06:07 xFrednet

Forgot about this. I will close.

PartiallyUntyped avatar Jul 23 '24 13:07 PartiallyUntyped