Skip to content

Commit 6673cc8

Browse files
committed
Resolve false positives for hex int cast.
1 parent 9ff4581 commit 6673cc8

File tree

2 files changed

+29
-18
lines changed

2 files changed

+29
-18
lines changed

clippy_lints/src/literal_representation.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ pub fn format_numeric_literal(lit: &str, type_suffix: Option<&str>, float: bool)
131131
#[derive(Debug)]
132132
pub(super) struct NumericLiteral<'a> {
133133
/// Which radix the literal was represented in.
134-
radix: Radix,
134+
pub radix: Radix,
135135
/// The radix prefix, if present.
136136
prefix: Option<&'a str>,
137137

@@ -147,16 +147,20 @@ pub(super) struct NumericLiteral<'a> {
147147
}
148148

149149
impl<'a> NumericLiteral<'a> {
150-
fn from_lit(src: &'a str, lit: &Lit) -> Option<NumericLiteral<'a>> {
151-
if lit.kind.is_numeric() && src.chars().next().map_or(false, |c| c.is_digit(10)) {
152-
let (unsuffixed, suffix) = split_suffix(&src, &lit.kind);
153-
let float = if let LitKind::Float(..) = lit.kind { true } else { false };
150+
pub fn from_lit_kind(src: &'a str, lit_kind: &LitKind) -> Option<NumericLiteral<'a>> {
151+
if lit_kind.is_numeric() && src.chars().next().map_or(false, |c| c.is_digit(10)) {
152+
let (unsuffixed, suffix) = split_suffix(&src, lit_kind);
153+
let float = if let LitKind::Float(..) = lit_kind { true } else { false };
154154
Some(NumericLiteral::new(unsuffixed, suffix, float))
155155
} else {
156156
None
157157
}
158158
}
159159

160+
fn from_lit(src: &'a str, lit: &Lit) -> Option<NumericLiteral<'a>> {
161+
NumericLiteral::from_lit_kind(src, &lit.kind)
162+
}
163+
160164
#[must_use]
161165
fn new(lit: &'a str, suffix: Option<&'a str>, float: bool) -> Self {
162166
// Determine delimiter for radix prefix, if present, and radix.

clippy_lints/src/types.rs

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ use rustc_target::spec::abi::Abi;
2727
use rustc_typeck::hir_ty_to_ty;
2828

2929
use crate::consts::{constant, Constant};
30+
use crate::literal_representation::{NumericLiteral, Radix};
3031
use crate::utils::paths;
3132
use crate::utils::{
3233
clip, comparisons, differing_macro_contexts, higher, in_constant, int_bits, last_path_segment, match_def_path,
@@ -1210,21 +1211,27 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Casts {
12101211
let (cast_from, cast_to) = (cx.tables.expr_ty(ex), cx.tables.expr_ty(expr));
12111212
lint_fn_to_numeric_cast(cx, expr, ex, cast_from, cast_to);
12121213
if let ExprKind::Lit(ref lit) = ex.kind {
1213-
if let LitKind::Int(n, _) = lit.node {
1214-
if cast_to.is_floating_point() {
1214+
if_chain! {
1215+
if let LitKind::Int(n, _) = lit.node;
1216+
if let Some(src) = snippet_opt(cx, lit.span);
1217+
if cast_to.is_floating_point();
1218+
then {
12151219
let from_nbits = 128 - n.leading_zeros();
12161220
let to_nbits = fp_ty_mantissa_nbits(cast_to);
1217-
if from_nbits != 0 && to_nbits != 0 && from_nbits <= to_nbits {
1218-
span_lint_and_sugg(
1219-
cx,
1220-
UNNECESSARY_CAST,
1221-
expr.span,
1222-
&format!("casting integer literal to `{}` is unnecessary", cast_to),
1223-
"try",
1224-
format!("{}_{}", n, cast_to),
1225-
Applicability::MachineApplicable,
1226-
);
1227-
return;
1221+
if let Some(num_lit) = NumericLiteral::from_lit_kind(&src, &lit.node) {
1222+
if from_nbits != 0 && to_nbits != 0 && from_nbits <= to_nbits &&
1223+
num_lit.radix != Radix::Hexadecimal {
1224+
span_lint_and_sugg(
1225+
cx,
1226+
UNNECESSARY_CAST,
1227+
expr.span,
1228+
&format!("casting integer literal to `{}` is unnecessary", cast_to),
1229+
"try",
1230+
format!("{}_{}", n, cast_to),
1231+
Applicability::MachineApplicable,
1232+
);
1233+
return;
1234+
}
12281235
}
12291236
}
12301237
}

0 commit comments

Comments
 (0)