Single-use-lifetimes false positive?

⚓ Rust    📅 2025-06-23    👤 surdeus    👁️ 6      

surdeus

Warning

This post was published 51 days ago. The information described in this article may have changed.

The following code triggers the single_use_lifetimes lint:

struct Foo<'a>(&'a str);
impl<'a> Foo<'a> {
    // The below lint causes a compilation failure.
    #[deny(single_use_lifetimes)]
    fn new<'b: 'a>(x: &'b str) -> Self {
        Self(x)
    }
}

however the below does not:

impl<'a: 'b, 'b> From<&'a str> for Foo<'b> {
    // The below lint does not cause a compilation failure.
    #[deny(single_use_lifetimes)]
    fn from(value: &'a str) -> Self {
        Self(value)
    }
}

There are benefits to the From impl, so I'm glad the lint does not trigger for it. How is Foo::new different, or am I correct in thinking this is a false positive?

7 posts - 3 participants

Read full topic

🏷️ rust_feed