summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/clippy_lints/src/misc_early/literal_suffix.rs
blob: 1165c19a0cf0b442fbee3d8622e1637dff722849 (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
use clippy_utils::diagnostics::span_lint_and_sugg;
use rustc_ast::ast::Lit;
use rustc_errors::Applicability;
use rustc_lint::EarlyContext;

use super::{SEPARATED_LITERAL_SUFFIX, UNSEPARATED_LITERAL_SUFFIX};

pub(super) fn check(cx: &EarlyContext<'_>, lit: &Lit, lit_snip: &str, suffix: &str, sugg_type: &str) {
    let maybe_last_sep_idx = if let Some(val) = lit_snip.len().checked_sub(suffix.len() + 1) {
        val
    } else {
        return; // It's useless so shouldn't lint.
    };
    // Do not lint when literal is unsuffixed.
    if !suffix.is_empty() {
        if lit_snip.as_bytes()[maybe_last_sep_idx] == b'_' {
            span_lint_and_sugg(
                cx,
                SEPARATED_LITERAL_SUFFIX,
                lit.span,
                &format!("{} type suffix should not be separated by an underscore", sugg_type),
                "remove the underscore",
                format!("{}{}", &lit_snip[..maybe_last_sep_idx], suffix),
                Applicability::MachineApplicable,
            );
        } else {
            span_lint_and_sugg(
                cx,
                UNSEPARATED_LITERAL_SUFFIX,
                lit.span,
                &format!("{} type suffix should be separated by an underscore", sugg_type),
                "add an underscore",
                format!("{}_{}", &lit_snip[..=maybe_last_sep_idx], suffix),
                Applicability::MachineApplicable,
            );
        }
    }
}