]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/misc_early/zero_prefixed_literal.rs
Rollup merge of #94145 - ssomers:binary_heap_tests, r=jyn514
[rust.git] / src / tools / clippy / clippy_lints / src / misc_early / zero_prefixed_literal.rs
1 use clippy_utils::diagnostics::span_lint_and_then;
2 use rustc_errors::Applicability;
3 use rustc_lint::EarlyContext;
4 use rustc_span::Span;
5
6 use super::ZERO_PREFIXED_LITERAL;
7
8 pub(super) fn check(cx: &EarlyContext<'_>, lit_span: Span, lit_snip: &str) {
9     let trimmed_lit_snip = lit_snip.trim_start_matches(|c| c == '_' || c == '0');
10     span_lint_and_then(
11         cx,
12         ZERO_PREFIXED_LITERAL,
13         lit_span,
14         "this is a decimal constant",
15         |diag| {
16             diag.span_suggestion(
17                 lit_span,
18                 "if you mean to use a decimal constant, remove the `0` to avoid confusion",
19                 trimmed_lit_snip.to_string(),
20                 Applicability::MaybeIncorrect,
21             );
22             // do not advise to use octal form if the literal cannot be expressed in base 8.
23             if !lit_snip.contains(|c| c == '8' || c == '9') {
24                 diag.span_suggestion(
25                     lit_span,
26                     "if you mean to use an octal constant, use `0o`",
27                     format!("0o{trimmed_lit_snip}"),
28                     Applicability::MaybeIncorrect,
29                 );
30             }
31         },
32     );
33 }