2021-05-06 11:51:22 +02:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_then;
|
|
|
|
use rustc_errors::Applicability;
|
|
|
|
use rustc_lint::EarlyContext;
|
2022-10-10 13:40:56 +11:00
|
|
|
use rustc_span::Span;
|
2021-05-06 11:51:22 +02:00
|
|
|
|
|
|
|
use super::ZERO_PREFIXED_LITERAL;
|
|
|
|
|
2022-10-10 13:40:56 +11:00
|
|
|
pub(super) fn check(cx: &EarlyContext<'_>, lit_span: Span, lit_snip: &str) {
|
2022-10-23 15:18:45 +02:00
|
|
|
let trimmed_lit_snip = lit_snip.trim_start_matches(|c| c == '_' || c == '0');
|
2021-05-06 11:51:22 +02:00
|
|
|
span_lint_and_then(
|
|
|
|
cx,
|
|
|
|
ZERO_PREFIXED_LITERAL,
|
2022-10-10 13:40:56 +11:00
|
|
|
lit_span,
|
2021-05-06 11:51:22 +02:00
|
|
|
"this is a decimal constant",
|
|
|
|
|diag| {
|
|
|
|
diag.span_suggestion(
|
2022-10-10 13:40:56 +11:00
|
|
|
lit_span,
|
2021-05-06 11:51:22 +02:00
|
|
|
"if you mean to use a decimal constant, remove the `0` to avoid confusion",
|
2022-10-23 15:18:45 +02:00
|
|
|
trimmed_lit_snip.to_string(),
|
2021-05-06 11:51:22 +02:00
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
);
|
2022-10-23 15:18:45 +02:00
|
|
|
// do not advise to use octal form if the literal cannot be expressed in base 8.
|
|
|
|
if !lit_snip.contains(|c| c == '8' || c == '9') {
|
|
|
|
diag.span_suggestion(
|
2022-10-10 13:40:56 +11:00
|
|
|
lit_span,
|
2022-10-23 15:18:45 +02:00
|
|
|
"if you mean to use an octal constant, use `0o`",
|
|
|
|
format!("0o{trimmed_lit_snip}"),
|
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
);
|
|
|
|
}
|
2021-05-06 11:51:22 +02:00
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|