about summary refs log tree commit diff
path: root/clippy_lints/src/single_char_lifetime_names.rs
blob: ee82666b5affea88b54526cee4c203a405e25c9c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
use clippy_utils::diagnostics::span_lint_and_help;
use rustc_ast::ast::{GenericParam, GenericParamKind};
use rustc_lint::{EarlyContext, EarlyLintPass};
use rustc_middle::lint::in_external_macro;
use rustc_session::{declare_lint_pass, declare_tool_lint};

declare_clippy_lint! {
    /// ### What it does
    /// Checks for lifetimes with names which are one character
    /// long.
    ///
    /// ### Why is this bad?
    /// A single character is likely not enough to express the
    /// purpose of a lifetime. Using a longer name can make code
    /// easier to understand, especially for those who are new to
    /// Rust.
    ///
    /// ### Known problems
    /// Rust programmers and learning resources tend to use single
    /// character lifetimes, so this lint is at odds with the
    /// ecosystem at large. In addition, the lifetime's purpose may
    /// be obvious or, rarely, expressible in one character.
    ///
    /// ### Example
    /// ```rust
    /// struct DiagnosticCtx<'a> {
    ///     source: &'a str,
    /// }
    /// ```
    /// Use instead:
    /// ```rust
    /// struct DiagnosticCtx<'src> {
    ///     source: &'src str,
    /// }
    /// ```
    #[clippy::version = "1.59.0"]
    pub SINGLE_CHAR_LIFETIME_NAMES,
    restriction,
    "warns against single-character lifetime names"
}

declare_lint_pass!(SingleCharLifetimeNames => [SINGLE_CHAR_LIFETIME_NAMES]);

impl EarlyLintPass for SingleCharLifetimeNames {
    fn check_generic_param(&mut self, ctx: &EarlyContext<'_>, param: &GenericParam) {
        if in_external_macro(ctx.sess, param.ident.span) {
            return;
        }

        if let GenericParamKind::Lifetime = param.kind {
            if !param.is_placeholder && param.ident.as_str().len() <= 2 {
                span_lint_and_help(
                    ctx,
                    SINGLE_CHAR_LIFETIME_NAMES,
                    param.ident.span,
                    "single-character lifetime names are likely uninformative",
                    None,
                    "use a more informative name",
                );
            }
        }
    }
}