Skip to content

False positive on clippy::unnecessary_to_owned causes ... does not live long enough #15252

Open
@kjarosh

Description

@kjarosh

Summary

MWE:

fn main() {
    fn f(a: impl AsRef<[u8]> + 'static) -> Box<dyn AsRef<[u8]>> {
        Box::new(a)
    }

    let _b = {
        let v = vec![1u8];
        let bytes = &v;
        f(bytes.to_vec())
    };
}

Clippy suggests:

warning: unnecessary use of `to_vec`
 --> main.rs:9:11
  |
9 |         f(bytes.to_vec())
  |           ^^^^^^^^^^^^^^ help: use: `bytes`
  |
  = 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

After fixing, I get the error:

error[E0597]: `v` does not live long enough
  --> main.rs:8:21
   |
7  |         let v = vec![1u8];
   |             - binding `v` declared here
8  |         let bytes = &v;
   |                     ^^ borrowed value does not live long enough
9  |         f(bytes)
   |         -------- argument requires that `v` is borrowed for `'static`
10 |     };
   |     - `v` dropped here while still borrowed

Lint Name

clippy::unnecessary_to_owned

Reproducer

No response

Version

rustc 1.88.0 (6b00bc388 2025-06-23) (Fedora 1.88.0-1.fc41)
binary: rustc
commit-hash: 6b00bc3880198600130e1cf62b8f8a93494488cc
commit-date: 2025-06-23
host: x86_64-unknown-linux-gnu
release: 1.88.0
LLVM version: 19.1.7

Additional Labels

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't have

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions