From 0773d8afdb792a3147de53ad590b620be9eef006 Mon Sep 17 00:00:00 2001 From: Takayuki Date: Wed, 5 May 2021 02:21:26 +0900 Subject: [PATCH] move mixed_case_hex_literals to its own module --- .../src/misc_early/mixed_case_hex_literals.rs | 29 +++++++++++++++++++ clippy_lints/src/misc_early/mod.rs | 23 ++------------- 2 files changed, 31 insertions(+), 21 deletions(-) create mode 100644 clippy_lints/src/misc_early/mixed_case_hex_literals.rs diff --git a/clippy_lints/src/misc_early/mixed_case_hex_literals.rs b/clippy_lints/src/misc_early/mixed_case_hex_literals.rs new file mode 100644 index 000000000000..fbcbfa2556fe --- /dev/null +++ b/clippy_lints/src/misc_early/mixed_case_hex_literals.rs @@ -0,0 +1,29 @@ +use clippy_utils::diagnostics::span_lint; +use rustc_ast::ast::Lit; +use rustc_lint::EarlyContext; + +use super::MIXED_CASE_HEX_LITERALS; + +pub(super) fn check(cx: &EarlyContext<'_>, lit: &Lit, maybe_last_sep_idx: usize, lit_snip: String) { + if maybe_last_sep_idx <= 2 { + // It's meaningless or causes range error. + return; + } + let mut seen = (false, false); + for ch in lit_snip.as_bytes()[2..=maybe_last_sep_idx].iter() { + match ch { + b'a'..=b'f' => seen.0 = true, + b'A'..=b'F' => seen.1 = true, + _ => {}, + } + if seen.0 && seen.1 { + span_lint( + cx, + MIXED_CASE_HEX_LITERALS, + lit.span, + "inconsistent casing in hexadecimal literal", + ); + break; + } + } +} diff --git a/clippy_lints/src/misc_early/mod.rs b/clippy_lints/src/misc_early/mod.rs index 182218e77f64..0bfa6833aace 100644 --- a/clippy_lints/src/misc_early/mod.rs +++ b/clippy_lints/src/misc_early/mod.rs @@ -1,5 +1,6 @@ mod builtin_type_shadow; mod double_neg; +mod mixed_case_hex_literals; mod redundant_pattern; mod unneeded_field_pattern; mod unneeded_wildcard_pattern; @@ -351,27 +352,7 @@ impl MiscEarlyLints { } if lit_snip.starts_with("0x") { - if maybe_last_sep_idx <= 2 { - // It's meaningless or causes range error. - return; - } - let mut seen = (false, false); - for ch in lit_snip.as_bytes()[2..=maybe_last_sep_idx].iter() { - match ch { - b'a'..=b'f' => seen.0 = true, - b'A'..=b'F' => seen.1 = true, - _ => {}, - } - if seen.0 && seen.1 { - span_lint( - cx, - MIXED_CASE_HEX_LITERALS, - lit.span, - "inconsistent casing in hexadecimal literal", - ); - break; - } - } + mixed_case_hex_literals::check(cx, lit, maybe_last_sep_idx, lit_snip) } else if lit_snip.starts_with("0b") || lit_snip.starts_with("0o") { /* nothing to do */ } else if value != 0 && lit_snip.starts_with('0') {