e67b2bf732
This change is needed for the uninlined_format-args lint to be merged. See https://github.com/rust-lang/rust-clippy/pull/9233
39 lines
1.4 KiB
Rust
39 lines
1.4 KiB
Rust
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!("{sugg_type} type suffix should not be separated by an underscore"),
|
|
"remove the underscore",
|
|
format!("{}{suffix}", &lit_snip[..maybe_last_sep_idx]),
|
|
Applicability::MachineApplicable,
|
|
);
|
|
} else {
|
|
span_lint_and_sugg(
|
|
cx,
|
|
UNSEPARATED_LITERAL_SUFFIX,
|
|
lit.span,
|
|
&format!("{sugg_type} type suffix should be separated by an underscore"),
|
|
"add an underscore",
|
|
format!("{}_{suffix}", &lit_snip[..=maybe_last_sep_idx]),
|
|
Applicability::MachineApplicable,
|
|
);
|
|
}
|
|
}
|
|
}
|