Skip to content

mismatched_lifetime_syntaxes lint is suggesting a wrong code #154493

@baehyunsol

Description

@baehyunsol

Code

pub struct Foo<'a, 'b> {
    pub ptr1: &'a str,
    pub ptr2: &'b str,
}
pub struct Bar {}

// rustc suggests me that the return type should be `Foo<'_>`, but it should be `Foo<'_, '_>`
pub fn foo(_: &Bar) -> Foo {
    todo!()
}

Current output

warning: hiding a lifetime that's elided elsewhere is confusing
 --> src/lib.rs:8:15
  |
8 | pub fn foo(_: &Bar) -> Foo {
  |               ^^^^     ^^^
  |               |        |
  |               |        the same lifetime is hidden here
  |               |        the same lifetime is hidden here
  |               the lifetime is elided here
  |
  = help: the same lifetime is referred to in inconsistent ways, making the signature confusing
  = note: `#[warn(mismatched_lifetime_syntaxes)]` on by default
help: use `'_` for type paths
  |
8 | pub fn foo(_: &Bar) -> Foo<'_> {
  |                           ++++

warning: `playground` (lib) generated 1 warning (run `cargo fix --lib -p playground` to apply 1 suggestion)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.59s

Desired output

There are 2 lifetimes in Foo, so it has to be Foo<'_, '_>.

Rationale and extra context

Other cases

Rust Version

rustc 1.94.1 (e408947bf 2026-03-25)
binary: rustc
commit-hash: e408947bfd200af42db322daf0fadfe7e26d3bd1
commit-date: 2026-03-25
host: x86_64-unknown-linux-gnu
release: 1.94.1
LLVM version: 21.1.8

Anything else?

No response

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsD-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.D-papercutDiagnostics: An error or lint that needs small tweaks.L-mismatched_lifetime_syntaxesLint: mismatched_lifetime_syntaxesT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions