Skip to content

Lint on overly-complex lifetime annotations when lifetime subtyping would handle it #148410

@scottmcm

Description

@scottmcm

A common mistake for beginners writing lifetime annotations is to write something like this:

pub fn min<'a: 'c, 'b: 'c, 'c, T: Ord>(a: &'a T, b: &'b T) -> &'c T {
    if b < a { b } else { a }
}

which while it's not wrong, is rather more complex than necessary.

It would be nice for the compiler to use its own knowledge of subtyping to suggest that the signature change to

pub fn min<'c, T: Ord>(a: &'c T, b: &'c T) -> &'c T {
    if b < a { b } else { a }
}

since that's equivalent.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lifetimesArea: Lifetimes / regionsA-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-feature-requestCategory: A feature request, i.e: not implemented / a PR.T-langRelevant to the language teamT-typesRelevant to the types team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions