Skip to content

Introduce a lint for single letter lifetimes #8233

Closed
@cameron1024

Description

@cameron1024

What it does

Highlights when a named lifetime is introduced that is a single character long. This feels like a very opinionated lint, and definitely overly pedantic in many cases, but I think there could be contexts where it's useful.

For example, I had to explain what the following impl meant in our Rocket web service:

impl<'r, 'o: 'r> Responder<'r, 'o> for Error {
    // ...
}

After reading the docs, I found a comment saying that 'r was the lifetime of the request, and 'o was the lifetime of the response. Admittedly this is somewhat intuitive by looking at the uses of the references in the type signature of the function in the trait, but I can't help but feel if it was instead written as: impl <'req, 'res: 'req> ..., I wouldn't even have to look at the docs to understand, which is significantly more valuable to those less familiar with Rust and borrowing/lifetimes.

Lint Name

single_char_lifetime_name

Category

pedantic

Advantage

Easier to understand meaning of a specific named lifetime, especially if new to Rust

Drawbacks

Likely overly pedantic in some codebases/teams/contexts

  • some contexts have only one lifetime that is sensible, and a single letter lifetime would be fine, e.g.:
struct Foo<'foo> {
  inner: &'foo str
}

is not (in my opinion) more readable/understandable than:

struct Foo<'a> {
  inner: &'a str
}
  • some teams/companies are familiar enough with the language that reading function signatures to work out the meaning of lifetimes doesn't add significant overhead

Example

fn dispatch<'r, 'o: 'r>(req: &'r Request) -> &'o Repsonse {
  todo!()
}

Could be written as:

fn dispatch<'req, 'res: 'req>(req: &'req Request) -> &'res Repsonse {
  todo!()
}

Metadata

Metadata

Labels

A-lintArea: New lints

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions