Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Lifetime error following unnecessary_to_owned going from .to_vec().into_iter() to .iter().copied() #14711

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

Open
ginnyTheCat opened this issue Apr 30, 2025 · 2 comments
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@ginnyTheCat
Copy link

Summary

As closures don't allow to specify that the returntype has the same lifetime as the input, it's in this case required to clone the input and return a owned value. In this case the iterator would borrow b and therefore clippy's suggestion to replace it with .iter().copied() results in a lifetime error.

error: lifetime may not live long enough
 --> src/main.rs:8:13
  |
8 |     run(|b| b.iter().copied().map(|c| c.to_ascii_uppercase()));
  |          -- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ returning this value requires that `'1` must outlive `'2`
  |          ||
  |          |return type of closure is Map<Copied<std::slice::Iter<'2, u8>>, {closure@src/main.rs:8:35: 8:38}>
  |          has type `&'1 [u8]`
  |
help: consider adding 'move' keyword before the nested closure
  |
8 |     run(|b| b.iter().copied().map(move |c| c.to_ascii_uppercase()));
  |

Lint Name

unnecessary_to_owned

Reproducer

I tried this code:

fn run<F: Fn(&[u8]) -> I, I: Iterator<Item = u8>>(f: F) {
    for x in f(b"abc") {
        println!("{x}");
    }
}

fn main() {
    run(|b| b.to_vec().into_iter().map(|c| c.to_ascii_uppercase()));
}

I saw this happen:

warning: unnecessary use of `to_vec`
 --> src/main.rs:8:13
  |
8 |     run(|b| b.to_vec().into_iter().map(|c| c.to_ascii_uppercase()));
  |             ^^^^^^^^^^^^^^^^^^^^^^ help: use: `b.iter().copied()`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_to_owned
  = note: `#[warn(clippy::unnecessary_to_owned)]` on by default

warning: `tmp` (bin "tmp") generated 1 warning

I expected to see this happen:
Nothing at all

Version

rustc 1.88.0-nightly (74509131e 2025-04-29)
binary: rustc
commit-hash: 74509131e85a97353c67c503ea32e148a56cf4bd
commit-date: 2025-04-29
host: x86_64-unknown-linux-gnu
release: 1.88.0-nightly
LLVM version: 20.1.2

Additional Labels

No response

@ginnyTheCat ginnyTheCat 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 Apr 30, 2025
@profetia
Copy link
Contributor

Not sure if clippy can really handle this. To be honest you can just relax the trait bound to:

fn run<'a, F: Fn(&'a [u8]) -> I, I: 'a + Iterator<Item = u8>>(f: F) {
    for x in f(b"abc") {
        println!("{x}");
    }
}

@ginnyTheCat
Copy link
Author

To be honest you can just relax the trait bound to

I guess my minimal example is a bit to minimal. In my real code I'm iterating over a list of &[u8] slices that i can only hold for the duration of that iteration. Therefore I would need to do F: for<'a> Fn(&'a [u8]) -> impl 'a + Iterator<Item = u8> which is currently unsupported. I think I've run into problems with this before and found out this can only be done using GATs atm. This piece of code is not perf relevant so it doesn't matter here really. I just thought it would make sense to report this clippy issue.

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-false-positive Issue: The lint was triggered on code it shouldn't have
Projects
None yet
Development

No branches or pull requests

2 participants