]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/misc_early/literal_suffix.rs
Auto merge of #9873 - smoelius:move-line-span, r=flip1995
[rust.git] / clippy_lints / src / misc_early / literal_suffix.rs
1 use clippy_utils::diagnostics::span_lint_and_sugg;
2 use rustc_ast::ast::Lit;
3 use rustc_errors::Applicability;
4 use rustc_lint::EarlyContext;
5
6 use super::{SEPARATED_LITERAL_SUFFIX, UNSEPARATED_LITERAL_SUFFIX};
7
8 pub(super) fn check(cx: &EarlyContext<'_>, lit: &Lit, lit_snip: &str, suffix: &str, sugg_type: &str) {
9     let Some(maybe_last_sep_idx) = lit_snip.len().checked_sub(suffix.len() + 1) else {
10         return; // It's useless so shouldn't lint.
11     };
12     // Do not lint when literal is unsuffixed.
13     if !suffix.is_empty() {
14         if lit_snip.as_bytes()[maybe_last_sep_idx] == b'_' {
15             span_lint_and_sugg(
16                 cx,
17                 SEPARATED_LITERAL_SUFFIX,
18                 lit.span,
19                 &format!("{sugg_type} type suffix should not be separated by an underscore"),
20                 "remove the underscore",
21                 format!("{}{suffix}", &lit_snip[..maybe_last_sep_idx]),
22                 Applicability::MachineApplicable,
23             );
24         } else {
25             span_lint_and_sugg(
26                 cx,
27                 UNSEPARATED_LITERAL_SUFFIX,
28                 lit.span,
29                 &format!("{sugg_type} type suffix should be separated by an underscore"),
30                 "add an underscore",
31                 format!("{}_{suffix}", &lit_snip[..=maybe_last_sep_idx]),
32                 Applicability::MachineApplicable,
33             );
34         }
35     }
36 }