Skip to content

Commit 2dfb246

Browse files
committed
refactor unseparated_literal_suffix
1 parent b1567f4 commit 2dfb246

File tree

3 files changed

+15
-29
lines changed

3 files changed

+15
-29
lines changed

clippy_lints/src/misc_early/mixed_case_hex_literals.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@ use rustc_lint::EarlyContext;
44

55
use super::MIXED_CASE_HEX_LITERALS;
66

7-
pub(super) fn check(cx: &EarlyContext<'_>, lit: &Lit, maybe_last_sep_idx: usize, lit_snip: String) {
7+
pub(super) fn check(cx: &EarlyContext<'_>, lit: &Lit, suffix: &str, lit_snip: String) {
8+
let maybe_last_sep_idx = if let Some(val) = lit_snip.len().checked_sub(suffix.len() + 1) {
9+
val
10+
} else {
11+
return; // It's useless so shouldn't lint.
12+
};
813
if maybe_last_sep_idx <= 2 {
914
// It's meaningless or causes range error.
1015
return;

clippy_lints/src/misc_early/mod.rs

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@ mod unneeded_wildcard_pattern;
77
mod unseparated_literal_suffix;
88
mod zero_prefixed_literal;
99

10-
use clippy_utils::diagnostics::{span_lint, span_lint_and_sugg};
10+
use clippy_utils::diagnostics::span_lint;
1111
use clippy_utils::source::snippet_opt;
1212
use rustc_ast::ast::{Expr, Generics, Lit, LitFloatType, LitIntType, LitKind, NodeId, Pat, PatKind};
1313
use rustc_ast::visit::FnKind;
1414
use rustc_data_structures::fx::FxHashMap;
15-
use rustc_errors::Applicability;
1615
use rustc_lint::{EarlyContext, EarlyLintPass, LintContext};
1716
use rustc_middle::lint::in_external_macro;
1817
use rustc_session::{declare_lint_pass, declare_tool_lint};
@@ -333,34 +332,17 @@ impl MiscEarlyLints {
333332
LitIntType::Unsigned(ty) => ty.name_str(),
334333
LitIntType::Unsuffixed => "",
335334
};
336-
337-
let maybe_last_sep_idx = if let Some(val) = lit_snip.len().checked_sub(suffix.len() + 1) {
338-
val
339-
} else {
340-
return; // It's useless so shouldn't lint.
341-
};
342-
// Do not lint when literal is unsuffixed.
343-
if !suffix.is_empty() && lit_snip.as_bytes()[maybe_last_sep_idx] != b'_' {
344-
span_lint_and_sugg(
345-
cx,
346-
UNSEPARATED_LITERAL_SUFFIX,
347-
lit.span,
348-
"integer type suffix should be separated by an underscore",
349-
"add an underscore",
350-
format!("{}_{}", &lit_snip[..=maybe_last_sep_idx], suffix),
351-
Applicability::MachineApplicable,
352-
);
353-
}
354-
335+
unseparated_literal_suffix::check(cx, lit, &lit_snip, suffix, "integer");
355336
if lit_snip.starts_with("0x") {
356-
mixed_case_hex_literals::check(cx, lit, maybe_last_sep_idx, lit_snip)
337+
mixed_case_hex_literals::check(cx, lit, suffix, lit_snip)
357338
} else if lit_snip.starts_with("0b") || lit_snip.starts_with("0o") {
358339
/* nothing to do */
359340
} else if value != 0 && lit_snip.starts_with('0') {
360341
zero_prefixed_literal::check(cx, lit, lit_snip)
361342
}
362343
} else if let LitKind::Float(_, LitFloatType::Suffixed(float_ty)) = lit.kind {
363-
unseparated_literal_suffix::check(cx, lit, float_ty, lit_snip)
344+
let suffix = float_ty.name_str();
345+
unseparated_literal_suffix::check(cx, lit, &lit_snip, suffix, "float")
364346
}
365347
}
366348
}

clippy_lints/src/misc_early/unseparated_literal_suffix.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
2-
use rustc_ast::ast::FloatTy;
32
use rustc_ast::ast::Lit;
43
use rustc_errors::Applicability;
54
use rustc_lint::EarlyContext;
65

76
use super::UNSEPARATED_LITERAL_SUFFIX;
87

9-
pub(super) fn check(cx: &EarlyContext<'_>, lit: &Lit, float_ty: FloatTy, lit_snip: String) {
10-
let suffix = float_ty.name_str();
8+
pub(super) fn check(cx: &EarlyContext<'_>, lit: &Lit, lit_snip: &String, suffix: &str, sugg_type: &str) {
119
let maybe_last_sep_idx = if let Some(val) = lit_snip.len().checked_sub(suffix.len() + 1) {
1210
val
1311
} else {
1412
return; // It's useless so shouldn't lint.
1513
};
16-
if lit_snip.as_bytes()[maybe_last_sep_idx] != b'_' {
14+
// Do not lint when literal is unsuffixed.
15+
if !suffix.is_empty() && lit_snip.as_bytes()[maybe_last_sep_idx] != b'_' {
1716
span_lint_and_sugg(
1817
cx,
1918
UNSEPARATED_LITERAL_SUFFIX,
2019
lit.span,
21-
"float type suffix should be separated by an underscore",
20+
&format!("{} type suffix should be separated by an underscore", sugg_type),
2221
"add an underscore",
2322
format!("{}_{}", &lit_snip[..=maybe_last_sep_idx], suffix),
2423
Applicability::MachineApplicable,

0 commit comments

Comments
 (0)