]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/misc_early/literal_suffix.rs
Rollup merge of #91562 - dtolnay:asyncspace, r=Mark-Simulacrum
[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_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 maybe_last_sep_idx = if let Some(val) = lit_snip.len().checked_sub(suffix.len() + 1) {
10         val
11     } else {
12         return; // It's useless so shouldn't lint.
13     };
14     // Do not lint when literal is unsuffixed.
15     if !suffix.is_empty() {
16         if lit_snip.as_bytes()[maybe_last_sep_idx] == b'_' {
17             span_lint_and_sugg(
18                 cx,
19                 SEPARATED_LITERAL_SUFFIX,
20                 lit.span,
21                 &format!("{} type suffix should not be separated by an underscore", sugg_type),
22                 "remove the underscore",
23                 format!("{}{}", &lit_snip[..maybe_last_sep_idx], suffix),
24                 Applicability::MachineApplicable,
25             );
26         } else {
27             span_lint_and_sugg(
28                 cx,
29                 UNSEPARATED_LITERAL_SUFFIX,
30                 lit.span,
31                 &format!("{} type suffix should be separated by an underscore", sugg_type),
32                 "add an underscore",
33                 format!("{}_{}", &lit_snip[..=maybe_last_sep_idx], suffix),
34                 Applicability::MachineApplicable,
35             );
36         }
37     }
38 }