Skip to content

False positive: single_use_lifetimes #77175

Closed
@Nugine

Description

@Nugine

I tried this code:

#[deny(single_use_lifetimes)]

// error: lifetime parameter `'a` only used once
async fn bar<'a>(s1: String, s2: &'_ str, s3: &'a str) -> &'a str {
    s3
}

fn foo<'a>(s1: String, s2: &'_ str, s3: &'a str) -> &'a str {
    s3
}

fn main() {}

I expected to see this happen: the code is correct.

Instead, this happened:

error: lifetime parameter `'a` only used once
 --> src/main.rs:4:14
  |
4 | async fn bar<'a>(s1: String, s2: &'_ str, s3: &'a str) -> &'a str {
  |              ^^ this lifetime...                           -- ...is used only here
  |
note: the lint level is defined here
 --> src/main.rs:1:8
  |
1 | #[deny(single_use_lifetimes)]
  |        ^^^^^^^^^^^^^^^^^^^^

Just swap the functions, then the code compiles:

#[deny(single_use_lifetimes)]

fn foo<'a>(s1: String, s2: &'_ str, s3: &'a str) -> &'a str {
    s3
}

async fn bar<'a>(s1: String, s2: &'_ str, s3: &'a str) -> &'a str {
    s3
}

fn main() {}

Update 20201014:

The examples may be misleading.
I forgot that #[deny(single_use_lifetimes)] takes effect on a single item.
It was a typo.

Here is a new example. It should be correct. But the compiler does not accept it.

#![deny(single_use_lifetimes)]

// error: lifetime parameter `'a` only used once
async fn bar<'a>(s1: String, s2: &'_ str, s3: &'a str) -> &'a str {
    s3
}

Meta

rustc --version --verbose:

rustc 1.46.0 (04488afe3 2020-08-24)
binary: rustc
commit-hash: 04488afe34512aa4c33566eb16d8c912a3ae04f9
commit-date: 2020-08-24
host: x86_64-unknown-linux-gnu
release: 1.46.0
LLVM version: 10.0

rustc +nightly --version --verbose:

rustc 1.48.0-nightly (8b4085359 2020-09-23)
binary: rustc
commit-hash: 8b4085359ae798dedb05c95ad42520557bd25320
commit-date: 2020-09-23
host: x86_64-unknown-linux-gnu
release: 1.48.0-nightly
LLVM version: 11.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-async-awaitArea: Async & AwaitA-lifetimesArea: Lifetimes / regionsA-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.AsyncAwait-TriagedAsync-await issues that have been triaged during a working group meeting.C-bugCategory: This is a bug.P-mediumMedium priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions