summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/clippy_lints/src/misc_early/zero_prefixed_literal.rs
blob: 4963bba82f2da169887da3f88a3a5c7641a64000 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
use clippy_utils::diagnostics::span_lint_and_then;
use rustc_ast::ast::Lit;
use rustc_errors::Applicability;
use rustc_lint::EarlyContext;

use super::ZERO_PREFIXED_LITERAL;

pub(super) fn check(cx: &EarlyContext<'_>, lit: &Lit, lit_snip: &str) {
    span_lint_and_then(
        cx,
        ZERO_PREFIXED_LITERAL,
        lit.span,
        "this is a decimal constant",
        |diag| {
            diag.span_suggestion(
                lit.span,
                "if you mean to use a decimal constant, remove the `0` to avoid confusion",
                lit_snip.trim_start_matches(|c| c == '_' || c == '0').to_string(),
                Applicability::MaybeIncorrect,
            );
            diag.span_suggestion(
                lit.span,
                "if you mean to use an octal constant, use `0o`",
                format!("0o{}", lit_snip.trim_start_matches(|c| c == '_' || c == '0')),
                Applicability::MaybeIncorrect,
            );
        },
    );
}