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

FP: needless_lifetimes #12085

Closed
beyarkay opened this issue Jan 3, 2024 · 5 comments
Closed

FP: needless_lifetimes #12085

beyarkay opened this issue Jan 3, 2024 · 5 comments
Assignees
Labels
C-bug Category: Clippy is not doing the correct thing I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@beyarkay
Copy link

beyarkay commented Jan 3, 2024

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

@beyarkay beyarkay added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Jan 3, 2024
@m-rph
Copy link
Contributor

m-rph commented Jan 4, 2024

@rustbot claim

@m-rph
Copy link
Contributor

m-rph commented Jan 5, 2024

Given that the same lifetime is given, to all refs, these could be elided completely, which suggests that the code doesn't consider all lifetimes, perhaps this is related to nesting?

Edit: I had asked Samuel whether they are familiar with the codebase and wanted to tackle this, but deleted the comment.

@samueltardieu
Copy link
Contributor

Note that this is not a false positive: the lint should be triggered indeed, but the suggestion is wrong because it forgets to replace the latest &'a. Also, I'm surprised it suggests &'_ str in the output instead of the simpler &str.

@rustbot label +I-suggestion-causes-error -I-false-positive

@rustbot rustbot added I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied and removed I-false-positive Issue: The lint was triggered on code it shouldn't have labels Jan 7, 2024
@samueltardieu
Copy link
Contributor

Please go ahead, I don't specially know this part of the codebase.

@m-rph
Copy link
Contributor

m-rph commented Mar 29, 2024

Closing as duplicate. This appears to be the same issue as #12291.

@m-rph m-rph closed this as completed Mar 29, 2024
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 I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants