mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-15 01:17:16 +00:00
literal representation restructure 1
Combine macro expansion checks. Indentation is a little strange to avoid rustfmt issue.
This commit is contained in:
parent
180f87065f
commit
8f5b4f3f5c
1 changed files with 47 additions and 51 deletions
|
@ -358,67 +358,63 @@ impl EarlyLintPass for LiteralDigitGrouping {
|
|||
impl LiteralDigitGrouping {
|
||||
fn check_lit(cx: &EarlyContext<'_>, lit: &Lit) {
|
||||
let in_macro = in_macro(lit.span);
|
||||
|
||||
if_chain! {
|
||||
if let Some(src) = snippet_opt(cx, lit.span);
|
||||
if let Some(firstch) = src.chars().next();
|
||||
if char::is_digit(firstch, 10);
|
||||
then {
|
||||
|
||||
|
||||
match lit.kind {
|
||||
LitKind::Int(..) => {
|
||||
// Lint integral literals.
|
||||
if_chain! {
|
||||
if let Some(src) = snippet_opt(cx, lit.span);
|
||||
if let Some(firstch) = src.chars().next();
|
||||
if char::is_digit(firstch, 10);
|
||||
then {
|
||||
let digit_info = DigitInfo::new(&src, false);
|
||||
let _ = Self::do_lint(digit_info.digits, digit_info.suffix, in_macro).map_err(|warning_type| {
|
||||
warning_type.display(&digit_info.grouping_hint(), cx, lit.span)
|
||||
});
|
||||
}
|
||||
}
|
||||
let digit_info = DigitInfo::new(&src, false);
|
||||
let _ = Self::do_lint(digit_info.digits, digit_info.suffix, in_macro).map_err(|warning_type| {
|
||||
warning_type.display(&digit_info.grouping_hint(), cx, lit.span)
|
||||
});
|
||||
},
|
||||
LitKind::Float(..) => {
|
||||
// Lint floating-point literals.
|
||||
if_chain! {
|
||||
if let Some(src) = snippet_opt(cx, lit.span);
|
||||
if let Some(firstch) = src.chars().next();
|
||||
if char::is_digit(firstch, 10);
|
||||
then {
|
||||
let digit_info = DigitInfo::new(&src, true);
|
||||
// Separate digits into integral and fractional parts.
|
||||
let parts: Vec<&str> = digit_info
|
||||
.digits
|
||||
.split_terminator('.')
|
||||
.collect();
|
||||
let digit_info = DigitInfo::new(&src, true);
|
||||
// Separate digits into integral and fractional parts.
|
||||
let parts: Vec<&str> = digit_info
|
||||
.digits
|
||||
.split_terminator('.')
|
||||
.collect();
|
||||
|
||||
// Lint integral and fractional parts separately, and then check consistency of digit
|
||||
// groups if both pass.
|
||||
let _ = Self::do_lint(parts[0], digit_info.suffix, in_macro)
|
||||
.map(|integral_group_size| {
|
||||
if parts.len() > 1 {
|
||||
// Lint the fractional part of literal just like integral part, but reversed.
|
||||
let fractional_part = &parts[1].chars().rev().collect::<String>();
|
||||
let _ = Self::do_lint(fractional_part, None, in_macro)
|
||||
.map(|fractional_group_size| {
|
||||
let consistent = Self::parts_consistent(integral_group_size,
|
||||
fractional_group_size,
|
||||
parts[0].len(),
|
||||
parts[1].len());
|
||||
if !consistent {
|
||||
WarningType::InconsistentDigitGrouping.display(
|
||||
&digit_info.grouping_hint(),
|
||||
cx,
|
||||
lit.span,
|
||||
);
|
||||
}
|
||||
})
|
||||
.map_err(|warning_type| warning_type.display(&digit_info.grouping_hint(),
|
||||
cx,
|
||||
lit.span));
|
||||
}
|
||||
})
|
||||
.map_err(|warning_type| warning_type.display(&digit_info.grouping_hint(), cx, lit.span));
|
||||
}
|
||||
}
|
||||
// Lint integral and fractional parts separately, and then check consistency of digit
|
||||
// groups if both pass.
|
||||
let _ = Self::do_lint(parts[0], digit_info.suffix, in_macro)
|
||||
.map(|integral_group_size| {
|
||||
if parts.len() > 1 {
|
||||
// Lint the fractional part of literal just like integral part, but reversed.
|
||||
let fractional_part = &parts[1].chars().rev().collect::<String>();
|
||||
let _ = Self::do_lint(fractional_part, None, in_macro)
|
||||
.map(|fractional_group_size| {
|
||||
let consistent = Self::parts_consistent(integral_group_size,
|
||||
fractional_group_size,
|
||||
parts[0].len(),
|
||||
parts[1].len());
|
||||
if !consistent {
|
||||
WarningType::InconsistentDigitGrouping.display(
|
||||
&digit_info.grouping_hint(),
|
||||
cx,
|
||||
lit.span,
|
||||
);
|
||||
}
|
||||
})
|
||||
.map_err(|warning_type| warning_type.display(&digit_info.grouping_hint(),
|
||||
cx,
|
||||
lit.span));
|
||||
}
|
||||
})
|
||||
.map_err(|warning_type| warning_type.display(&digit_info.grouping_hint(), cx, lit.span));
|
||||
},
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Given the sizes of the digit groups of both integral and fractional
|
||||
|
|
Loading…
Reference in a new issue