1 use clippy_utils::diagnostics::span_lint_and_help;
2 use rustc_ast::ast::{GenericParam, GenericParamKind};
3 use rustc_lint::{EarlyContext, EarlyLintPass, LintContext};
4 use rustc_middle::lint::in_external_macro;
5 use rustc_session::{declare_lint_pass, declare_tool_lint};
9 /// Checks for lifetimes with names which are one character
12 /// ### Why is this bad?
13 /// A single character is likely not enough to express the
14 /// purpose of a lifetime. Using a longer name can make code
15 /// easier to understand, especially for those who are new to
18 /// ### Known problems
19 /// Rust programmers and learning resources tend to use single
20 /// character lifetimes, so this lint is at odds with the
21 /// ecosystem at large. In addition, the lifetime's purpose may
22 /// be obvious or, rarely, expressible in one character.
26 /// struct DiagnosticCtx<'a> {
32 /// struct DiagnosticCtx<'src> {
33 /// source: &'src str,
36 #[clippy::version = "1.60.0"]
37 pub SINGLE_CHAR_LIFETIME_NAMES,
39 "warns against single-character lifetime names"
42 declare_lint_pass!(SingleCharLifetimeNames => [SINGLE_CHAR_LIFETIME_NAMES]);
44 impl EarlyLintPass for SingleCharLifetimeNames {
45 fn check_generic_param(&mut self, ctx: &EarlyContext<'_>, param: &GenericParam) {
46 if in_external_macro(ctx.sess(), param.ident.span) {
50 if let GenericParamKind::Lifetime = param.kind {
51 if !param.is_placeholder && param.ident.as_str().len() <= 2 {
54 SINGLE_CHAR_LIFETIME_NAMES,
56 "single-character lifetime names are likely uninformative",
58 "use a more informative name",