Skip to content

FP: needless_lifetimes #12085

@beyarkay

Description

@beyarkay

Summary

I'm getting an error about unnecessary lifetimes, but when I action the suggestion, rustc throws an error.

Lint Name

needless_lifetimes

Reproducer

I tried this code:

// #[allow(clippy::needless_lifetimes)]
fn iterate_hashtags<'a>(txt: &'a str) -> impl Iterator<Item = (&'a str, &'a str)> {
    txt.split('#')
        .filter(|item| !item.is_empty())
        .filter_map(move |item| {
            item.find(' ')
                .map(|byte_of_first_space| item.split_at(byte_of_first_space))
        })
}

fn main() {
    let str_with_hashtags = "#thing this is a description of the thing\n#otherthing another thingy\nthat spans\nmultiple lines\n#hashtag hash taggery inception";
        for (hashtag, line) in iterate_hashtags(str_with_hashtags) {
            println!("{}: `{}`", hashtag, line);
        }
}

I saw this happen:

╰→ cargo clippy
warning: the following explicit lifetimes could be elided: 'a
  --> src/main.rs:14:21
   |
14 | fn iterate_hashtags<'a>(txt: &'a str) -> impl Iterator<Item = (&'a str, &'a str)> {
   |                     ^^        ^^                                ^^
   |
   = 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
   |
14 - fn iterate_hashtags<'a>(txt: &'a str) -> impl Iterator<Item = (&'a str, &'a str)> {
14 + fn iterate_hashtags(txt: &str) -> impl Iterator<Item = (&'_ str, &'a str)> {
   |

Which implies the following code should work:

// #[allow(clippy::needless_lifetimes)]
fn iterate_hashtags(txt: &str) -> impl Iterator<Item = (&'_ str, &'a str)> {
    txt.split('#')
        .filter(|item| !item.is_empty())
        .filter_map(move |item| {
            item.find(' ')
                .map(|byte_of_first_space| item.split_at(byte_of_first_space))
            //.map(|split| (split.0.trim(), split.1.trim()))
            //.filter(|split| !split.1.is_empty())
        })
}

But the above function throws this error:

╰→ cargo c
    Checking boyd-bot v0.1.0 (/Users/brk/projects/boyd-bot)
error[E0261]: use of undeclared lifetime name `'a`
  --> src/main.rs:14:67
   |
14 | fn iterate_hashtags(txt: &str) -> impl Iterator<Item = (&'_ str, &'a str)> {
   |                                                                   ^^ undeclared lifetime
   |
   = note: for more information on higher-ranked polymorphism, visit https://doc.rust-lang.org/nomicon/hrtb.html
help: consider making the bound lifetime-generic with a new `'a` lifetime
   |
14 | fn iterate_hashtags(txt: &str) -> impl for<'a> Iterator<Item = (&'_ str, &'a str)> {
   |                                        +++++++
help: consider introducing lifetime `'a` here
   |
14 | fn iterate_hashtags<'a>(txt: &str) -> impl Iterator<Item = (&'_ str, &'a str)> {
   |                    ++++

error: lifetime may not live long enough
  --> src/main.rs:15:5
   |
14 |   fn iterate_hashtags(txt: &str) -> impl Iterator<Item = (&'_ str, &'a str)> {
   |                            - let's call the lifetime of this reference `'1`
15 | /     txt.split('#')
16 | |         .filter(|item| !item.is_empty())
17 | |         .filter_map(move |item| {
18 | |             item.find(' ')
...  |
21 | |             //.filter(|split| !split.1.is_empty())
22 | |         })
   | |__________^ returning this value requires that `'1` must outlive `'static`
   |
help: to declare that `impl Iterator<Item = (&str, &str)>` captures data from argument `txt`, you can add an explicit `'_` lifetime bound
   |
14 | fn iterate_hashtags(txt: &str) -> impl Iterator<Item = (&'_ str, &'a str)> + '_ {
   |                                                                            ++++

Version

rustc 1.75.0 (82e1608df 2023-12-21)
binary: rustc
commit-hash: 82e1608dfa6e0b5569232559e3d385fea5a93112
commit-date: 2023-12-21
host: aarch64-apple-darwin
release: 1.75.0
LLVM version: 17.0.6

I updated clippy in case that was the issue, but I'm on clippy 0.1.75 (82e1608d 2023-12-21) and it's still a problem.

Additional Labels

@rustbot label +l-suggestion-causes-error

Metadata

Metadata

Labels

C-bugCategory: Clippy is not doing the correct thingI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions