rust-lang / rust-clippy

A bunch of lints to catch common mistakes and improve your Rust code. Book: https://doc.rust-lang.org/clippy/
https://rust-lang.github.io/rust-clippy/
Other
11.47k stars 1.55k forks source link

Incorrect collect suggestion - borrow doesn't live long enough #7335

Open sophiajt opened 3 years ago

sophiajt commented 3 years ago

I tried this code:

    let string_clone: Vec<_> = concat_string.item.lines().map(|x| x.to_string()).collect();

    if objects {
        Ok(string_clone
            .into_iter()...

Clippy suggests turning this into:

Ok(concat_string.item.lines().map(|x| x.to_string())...

I expected to see this happen: clippy to suggest a correct replacement

Instead, this happened: clippy suggests the above, which will not compile as concat_string.item will not live long enough.

Meta

Backtrace

``` ```

sophiajt commented 3 years ago

Another example of the same issue:

    let rows: Vec<Value> = ret_str
        .lines()
        .map(|v| v.to_str_value_create_tag())
        .collect();
    Ok((rows.into_iter().map(ReturnSuccess::value)).to_action_stream())