]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/misc_early/literal_suffix.rs
Rollup merge of #104433 - TaKO8Ki:fix-104392, r=estebank
[rust.git] / src / tools / clippy / clippy_lints / src / misc_early / literal_suffix.rs
1 use clippy_utils::diagnostics::span_lint_and_sugg;
2 use rustc_errors::Applicability;
3 use rustc_lint::EarlyContext;
4 use rustc_span::Span;
5
6 use super::{SEPARATED_LITERAL_SUFFIX, UNSEPARATED_LITERAL_SUFFIX};
7
8 pub(super) fn check(cx: &EarlyContext<'_>, lit_span: Span, 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 }