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: false positive with impl Trait #1306

Closed
japaric opened this issue Oct 30, 2016 · 0 comments · Fixed by #1400
Closed

needless_lifetimes: false positive with impl Trait #1306

japaric opened this issue Oct 30, 2016 · 0 comments · Fixed by #1400
Labels
C-bug Category: Clippy is not doing the correct thing

Comments

@japaric
Copy link
Member

japaric commented Oct 30, 2016

This code compiles:

#![feature(conservative_impl_trait)]

trait Foo {}

struct Bar {}

struct Baz<'a> {
    bar: &'a Bar,
}

impl<'a> Foo for Baz<'a> {}

impl Bar {
    fn baz<'a>(&'a self) -> impl Foo + 'a {
        Baz { bar: self }
    }
}

fn main() {}

But clippy gives this warning:

$ cargo clippy
warning: explicit lifetimes given in parameter types where they could be elided, #[warn(needless_lifetimes)] on by default
  --> src/main.rs:14:5
   |
14 |     fn baz<'a>(&'a self) -> impl Foo + 'a {
   |     ^
   |
   = help: for further information visit https://github.com/Manishearth/rust-clippy/wiki#needless_lifetimes

If you apply the suggestion:

 impl<'a> Foo for Baz<'a> {}

 impl Bar {
-    fn baz<'a>(&'a self) -> impl Foo + 'a {
+    fn baz(&self) -> impl Foo {
         Baz { bar: self }
     }
 }

you end with code that doesn't compile:

$ cargo clippy
error[E0564]: only named lifetimes are allowed in `impl Trait`, but `` was found in the type `Baz<'_>`
  --> src/main.rs:14:22
   |
14 |     fn baz(&self) -> impl Foo {
   |                      ^^^^^^^^

error: aborting due to previous error

I don't know if these lifetimes will become elidable in the future but, in this case, clippy should probably not raise a warning for now.

@mcarton mcarton added the C-bug Category: Clippy is not doing the correct thing label Oct 30, 2016
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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants