Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit b1567f4

Browse files
committed
move zero_prefixed_literal to its own module
1 parent 0773d8a commit b1567f4

File tree

2 files changed

+32
-21
lines changed

2 files changed

+32
-21
lines changed

clippy_lints/src/misc_early/mod.rs

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ mod redundant_pattern;
55
mod unneeded_field_pattern;
66
mod unneeded_wildcard_pattern;
77
mod unseparated_literal_suffix;
8+
mod zero_prefixed_literal;
89

9-
use clippy_utils::diagnostics::{span_lint, span_lint_and_sugg, span_lint_and_then};
10+
use clippy_utils::diagnostics::{span_lint, span_lint_and_sugg};
1011
use clippy_utils::source::snippet_opt;
1112
use rustc_ast::ast::{Expr, Generics, Lit, LitFloatType, LitIntType, LitKind, NodeId, Pat, PatKind};
1213
use rustc_ast::visit::FnKind;
@@ -356,26 +357,7 @@ impl MiscEarlyLints {
356357
} else if lit_snip.starts_with("0b") || lit_snip.starts_with("0o") {
357358
/* nothing to do */
358359
} else if value != 0 && lit_snip.starts_with('0') {
359-
span_lint_and_then(
360-
cx,
361-
ZERO_PREFIXED_LITERAL,
362-
lit.span,
363-
"this is a decimal constant",
364-
|diag| {
365-
diag.span_suggestion(
366-
lit.span,
367-
"if you mean to use a decimal constant, remove the `0` to avoid confusion",
368-
lit_snip.trim_start_matches(|c| c == '_' || c == '0').to_string(),
369-
Applicability::MaybeIncorrect,
370-
);
371-
diag.span_suggestion(
372-
lit.span,
373-
"if you mean to use an octal constant, use `0o`",
374-
format!("0o{}", lit_snip.trim_start_matches(|c| c == '_' || c == '0')),
375-
Applicability::MaybeIncorrect,
376-
);
377-
},
378-
);
360+
zero_prefixed_literal::check(cx, lit, lit_snip)
379361
}
380362
} else if let LitKind::Float(_, LitFloatType::Suffixed(float_ty)) = lit.kind {
381363
unseparated_literal_suffix::check(cx, lit, float_ty, lit_snip)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
use clippy_utils::diagnostics::span_lint_and_then;
2+
use rustc_ast::ast::Lit;
3+
use rustc_errors::Applicability;
4+
use rustc_lint::EarlyContext;
5+
6+
use super::ZERO_PREFIXED_LITERAL;
7+
8+
pub(super) fn check(cx: &EarlyContext<'_>, lit: &Lit, lit_snip: String) {
9+
span_lint_and_then(
10+
cx,
11+
ZERO_PREFIXED_LITERAL,
12+
lit.span,
13+
"this is a decimal constant",
14+
|diag| {
15+
diag.span_suggestion(
16+
lit.span,
17+
"if you mean to use a decimal constant, remove the `0` to avoid confusion",
18+
lit_snip.trim_start_matches(|c| c == '_' || c == '0').to_string(),
19+
Applicability::MaybeIncorrect,
20+
);
21+
diag.span_suggestion(
22+
lit.span,
23+
"if you mean to use an octal constant, use `0o`",
24+
format!("0o{}", lit_snip.trim_start_matches(|c| c == '_' || c == '0')),
25+
Applicability::MaybeIncorrect,
26+
);
27+
},
28+
);
29+
}

0 commit comments

Comments
 (0)