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 on a function returning an impl Fn #4452

Closed
pkoch opened this issue Aug 25, 2019 · 1 comment
Closed

needless_lifetimes false positive on a function returning an impl Fn #4452

pkoch opened this issue Aug 25, 2019 · 1 comment

Comments

@pkoch
Copy link

pkoch commented Aug 25, 2019

I was doing some fizz buzz, and I had this code

  pub fn sounder<'a>(div: u32, msg: &'a str) -> impl Fn(u32) -> Option<&'a str>                                                                                                                    
  {                                                                               
      move |n: u32|{                                                              
          if n % div != 0 {                                                       
              return None;                                                        
          };                                                                      
                                                                                  
          Some(msg)                                                               
      }                                                                           
  }

Clippy is telling me that

warning: explicit lifetimes given in parameter types where they could be elided (or replaced with `'_` if needed by type declaration)
  --> src/lib.rs:1:1
   |
1  | / pub fn sounder<'a>(div: u32, msg: &'a str) -> impl Fn(u32) -> Option<&'a str>
2  | | {
3  | |     move |n: u32|{
4  | |         if n % div != 0 {
...  |
9  | |     }
10 | | }
   | |_^
   |
   = 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

However, I don't think I can use &'_:

error[E0106]: missing lifetime specifier
 --> src/lib.rs:1:67
  |
1 | pub fn sounder(div: u32, msg: &'_ str) -> impl Fn(u32) -> Option<&'_ str>
  |                                                                   ^^ help: consider giving it an explicit bounded or 'static lifetime: `'static`
  |
  = help: this function's return type contains a borrowed value with an elided lifetime, but the lifetime cannot be derived from the arguments

And I certainly can't just elide them:

error[E0106]: missing lifetime specifier
 --> src/lib.rs:1:63
  |
1 | pub fn sounder(div: u32, msg: &str) -> impl Fn(u32) -> Option<&str>
  |                                                               ^ help: consider giving it an explicit bounded or 'static lifetime: `&'static`
  |
  = help: this function's return type contains a borrowed value with an elided lifetime, but the lifetime cannot be derived from the arguments

Is this a false positive? Using clippy 0.0.212 (e3cb40e 2019-06-25).

@flip1995
Copy link
Member

Duplicate of #2944

@flip1995 flip1995 marked this as a duplicate of #2944 Aug 26, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants