Clippy needless_lifetimes and a function returning an `impl Fn`

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

So, is this a false positive?

Yeah it's a false positive. You should report it to clippy.

1 Like

Reported on needless_lifetimes false positive on a function returning an `impl Fn` · Issue #4452 · rust-lang/rust-clippy · GitHub

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.