rust-clippy/tests/ui/single_char_lifetime_names.rs
bors a6f80fccb1 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`]
2022-01-09 14:27:36 +00:00

43 lines
1 KiB
Rust

#![warn(clippy::single_char_lifetime_names)]
// Lifetimes should only be linted when they're introduced
struct DiagnosticCtx<'a, 'b>
where
'a: 'b,
{
_source: &'a str,
_unit: &'b (),
}
// Only the lifetimes on the `impl`'s generics should be linted
impl<'a, 'b> DiagnosticCtx<'a, 'b> {
fn new(source: &'a str, unit: &'b ()) -> DiagnosticCtx<'a, 'b> {
Self {
_source: source,
_unit: unit,
}
}
}
// No lifetimes should be linted here
impl<'src, 'unit> DiagnosticCtx<'src, 'unit> {
fn new_pass(source: &'src str, unit: &'unit ()) -> DiagnosticCtx<'src, 'unit> {
Self {
_source: source,
_unit: unit,
}
}
}
// Only 'a should be linted here
fn split_once<'a>(base: &'a str, other: &'_ str) -> (&'a str, Option<&'a str>) {
base.split_once(other)
.map(|(left, right)| (left, Some(right)))
.unwrap_or((base, None))
}
fn main() {
let src = "loop {}";
let unit = ();
DiagnosticCtx::new(src, &unit);
}