You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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>forError{// ...}
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.:
structFoo<'foo>{inner:&'foostr}
is not (in my opinion) more readable/understandable than:
structFoo<'a>{inner:&'astr}
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
I happen to have already written this lint for a personal Dylint library, so I'd be glad to try porting it to Clippy. Not sure if rustbot is active here, but just in case... @rustbot claim
…ogiq
new lint: `single_char_lifetime_names`
This pull request adds a lint against single character lifetime names, as they might not divulge enough information about the purpose of the lifetime. This can make code harder to understand. I placed this in `restriction` rather than `pedantic` (as suggested in #8233) since most of the Rust ecosystem already uses single character lifetime names (to my knowledge, at least) and since single character lifetime names aren't incorrect. I'd be happy to change this upon request, however. Fixes#8233.
- [x] Followed lint naming conventions
- [x] Added passing UI tests (including committed `.stderr` file)
- [x] `cargo test` passes locally
- [x] Executed `cargo dev update_lints`
- [x] Added lint documentation
- [x] Run `cargo dev fmt`
changelog: new lint: [`single_char_lifetime_names`]
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:
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
is not (in my opinion) more readable/understandable than:
Example
Could be written as:
The text was updated successfully, but these errors were encountered: