summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/clippy_lints/src/literal_representation.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/tools/clippy/clippy_lints/src/literal_representation.rs')
-rw-r--r--src/tools/clippy/clippy_lints/src/literal_representation.rs36
1 files changed, 20 insertions, 16 deletions
diff --git a/src/tools/clippy/clippy_lints/src/literal_representation.rs b/src/tools/clippy/clippy_lints/src/literal_representation.rs
index 25f19b9c6..3a7b7835c 100644
--- a/src/tools/clippy/clippy_lints/src/literal_representation.rs
+++ b/src/tools/clippy/clippy_lints/src/literal_representation.rs
@@ -5,11 +5,13 @@ use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::numeric_literal::{NumericLiteral, Radix};
use clippy_utils::source::snippet_opt;
use if_chain::if_chain;
-use rustc_ast::ast::{Expr, ExprKind, Lit, LitKind};
+use rustc_ast::ast::{Expr, ExprKind, LitKind};
+use rustc_ast::token;
use rustc_errors::Applicability;
use rustc_lint::{EarlyContext, EarlyLintPass, LintContext};
use rustc_middle::lint::in_external_macro;
use rustc_session::{declare_tool_lint, impl_lint_pass};
+use rustc_span::Span;
use std::iter;
declare_clippy_lint! {
@@ -236,8 +238,8 @@ impl EarlyLintPass for LiteralDigitGrouping {
return;
}
- if let ExprKind::Lit(ref lit) = expr.kind {
- self.check_lit(cx, lit);
+ if let ExprKind::Lit(lit) = expr.kind {
+ self.check_lit(cx, lit, expr.span);
}
}
}
@@ -252,12 +254,13 @@ impl LiteralDigitGrouping {
}
}
- fn check_lit(self, cx: &EarlyContext<'_>, lit: &Lit) {
+ fn check_lit(self, cx: &EarlyContext<'_>, lit: token::Lit, span: Span) {
if_chain! {
- if let Some(src) = snippet_opt(cx, lit.span);
- if let Some(mut num_lit) = NumericLiteral::from_lit(&src, lit);
+ if let Some(src) = snippet_opt(cx, span);
+ if let Ok(lit_kind) = LitKind::from_token_lit(lit);
+ if let Some(mut num_lit) = NumericLiteral::from_lit_kind(&src, &lit_kind);
then {
- if !Self::check_for_mistyped_suffix(cx, lit.span, &mut num_lit) {
+ if !Self::check_for_mistyped_suffix(cx, span, &mut num_lit) {
return;
}
@@ -293,14 +296,14 @@ impl LiteralDigitGrouping {
| WarningType::InconsistentDigitGrouping
| WarningType::UnusualByteGroupings
| WarningType::LargeDigitGroups => {
- !lit.span.from_expansion()
+ !span.from_expansion()
}
WarningType::DecimalRepresentation | WarningType::MistypedLiteralSuffix => {
true
}
};
if should_warn {
- warning_type.display(num_lit.format(), cx, lit.span);
+ warning_type.display(num_lit.format(), cx, span);
}
}
}
@@ -458,8 +461,8 @@ impl EarlyLintPass for DecimalLiteralRepresentation {
return;
}
- if let ExprKind::Lit(ref lit) = expr.kind {
- self.check_lit(cx, lit);
+ if let ExprKind::Lit(lit) = expr.kind {
+ self.check_lit(cx, lit, expr.span);
}
}
}
@@ -469,19 +472,20 @@ impl DecimalLiteralRepresentation {
pub fn new(threshold: u64) -> Self {
Self { threshold }
}
- fn check_lit(self, cx: &EarlyContext<'_>, lit: &Lit) {
+ fn check_lit(self, cx: &EarlyContext<'_>, lit: token::Lit, span: Span) {
// Lint integral literals.
if_chain! {
- if let LitKind::Int(val, _) = lit.kind;
- if let Some(src) = snippet_opt(cx, lit.span);
- if let Some(num_lit) = NumericLiteral::from_lit(&src, lit);
+ if let Ok(lit_kind) = LitKind::from_token_lit(lit);
+ if let LitKind::Int(val, _) = lit_kind;
+ if let Some(src) = snippet_opt(cx, span);
+ if let Some(num_lit) = NumericLiteral::from_lit_kind(&src, &lit_kind);
if num_lit.radix == Radix::Decimal;
if val >= u128::from(self.threshold);
then {
let hex = format!("{val:#X}");
let num_lit = NumericLiteral::new(&hex, num_lit.suffix, false);
let _ = Self::do_lint(num_lit.integer).map_err(|warning_type| {
- warning_type.display(num_lit.format(), cx, lit.span);
+ warning_type.display(num_lit.format(), cx, span);
});
}
}