Skip to content

Suggest adding & when a lifetime immediately precedes a type in a generic arg list #59325

Closed

Description

@meithecatte

If one types

fn foo(bar: 'static SomeStruct) { ... }

the compiler could suggest adding a & before the lifetime. This can be more useful in a generic parameter, where a high concentration of special characters makes it easy to not notice the mistake:

fn foo(bar: &mut HashSet<'static SomeStruct>) { ... }

Currently, the error message is just expected one of `,` or `>`, found `SomeStruct` .

Activity

  1. added
    C-enhancementCategory: An issue proposing an enhancement or a PR with one.
    A-parserArea: The lexing & parsing of Rust source code to an AST
    A-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`
    on Mar 20, 2019
  2. added
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    and removed
    C-enhancementCategory: An issue proposing an enhancement or a PR with one.
    on Aug 30, 2024
  3. added
    A-diagnosticsArea: Messages for errors, warnings, and lints
    A-lifetimesArea: Lifetimes / regions
    D-lack-of-suggestionDiagnostics: Adding a (structured) suggestion would increase the quality of the diagnostic.
    and removed
    A-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`
    on Sep 20, 2026
  4. fmease commented on Sep 20, 2026

    @fmease
    Member

    I didn't know about this issue but I fixed it in PR #139854 a year ago.

    The compiler output now looks like this for your snippet:

    error: expected type, found lifetime
     --> src/lib.rs:1:13
      |
    1 | fn foo(bar: 'static SomeStruct) {}
      |             ^^^^^^^ expected type
      |
    help: you might have meant to write a reference type here
      |
    1 | fn foo(bar: &'static SomeStruct) {}
      |             +
  5. changed the title [-]Suggest & when a lifetime precedes a type.[/-] [+]Suggest adding `&` when a lifetime immediately precedes a type in a generic arg list[/+] on Sep 20, 2026
  6. fmease commented on Sep 20, 2026

    @fmease
    Member

    Ah but your 2nd example has not been addressed.

  7. reopened this on Sep 20, 2026
  8. added a commit that references this issue on Oct 3, 2026
    57373cd
  9. added a commit that references this issue on Oct 3, 2026
    99b3c3a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-lifetimesArea: Lifetimes / regionsA-parserArea: The lexing & parsing of Rust source code to an ASTD-lack-of-suggestionDiagnostics: Adding a (structured) suggestion would increase the quality of the diagnostic.T-compilerRelevant to the compiler 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