Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 73deb72

Browse files
committed
Included binary and octal cases.
1 parent 6673cc8 commit 73deb72

File tree

4 files changed

+23
-10
lines changed

4 files changed

+23
-10
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-
pub radix: Radix,
134+
radix: Radix,
135135
/// The radix prefix, if present.
136136
prefix: Option<&'a str>,
137137

@@ -147,6 +147,10 @@ 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+
NumericLiteral::from_lit_kind(src, &lit.kind)
152+
}
153+
150154
pub fn from_lit_kind(src: &'a str, lit_kind: &LitKind) -> Option<NumericLiteral<'a>> {
151155
if lit_kind.is_numeric() && src.chars().next().map_or(false, |c| c.is_digit(10)) {
152156
let (unsuffixed, suffix) = split_suffix(&src, lit_kind);
@@ -157,10 +161,6 @@ impl<'a> NumericLiteral<'a> {
157161
}
158162
}
159163

160-
fn from_lit(src: &'a str, lit: &Lit) -> Option<NumericLiteral<'a>> {
161-
NumericLiteral::from_lit_kind(src, &lit.kind)
162-
}
163-
164164
#[must_use]
165165
fn new(lit: &'a str, suffix: Option<&'a str>, float: bool) -> Self {
166166
// Determine delimiter for radix prefix, if present, and radix.
@@ -199,6 +199,10 @@ impl<'a> NumericLiteral<'a> {
199199
}
200200
}
201201

202+
pub fn is_decimal(&self) -> bool {
203+
self.radix == Radix::Decimal
204+
}
205+
202206
fn split_digit_parts(digits: &str, float: bool) -> (&str, Option<&str>, Option<(char, &str)>) {
203207
let mut integer = digits;
204208
let mut fraction = None;

clippy_lints/src/types.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +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};
30+
use crate::literal_representation::NumericLiteral;
3131
use crate::utils::paths;
3232
use crate::utils::{
3333
clip, comparisons, differing_macro_contexts, higher, in_constant, int_bits, last_path_segment, match_def_path,
@@ -1219,8 +1219,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Casts {
12191219
let from_nbits = 128 - n.leading_zeros();
12201220
let to_nbits = fp_ty_mantissa_nbits(cast_to);
12211221
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 {
1222+
if from_nbits != 0 && to_nbits != 0 && from_nbits <= to_nbits && num_lit.is_decimal() {
12241223
span_lint_and_sugg(
12251224
cx,
12261225
UNNECESSARY_CAST,

tests/ui/unnecessary_cast_fixable.fixed

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,10 @@ fn main() {
1414
&v as &[i32];
1515
1.0 as f64;
1616
1 as u64;
17-
0x42 as f32;
17+
0x10 as f32;
18+
0o10 as f32;
19+
0b10 as f32;
20+
0x11 as f64;
21+
0o11 as f64;
22+
0b11 as f64;
1823
}

tests/ui/unnecessary_cast_fixable.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,10 @@ fn main() {
1414
&v as &[i32];
1515
1.0 as f64;
1616
1 as u64;
17-
0x42 as f32;
17+
0x10 as f32;
18+
0o10 as f32;
19+
0b10 as f32;
20+
0x11 as f64;
21+
0o11 as f64;
22+
0b11 as f64;
1823
}

0 commit comments

Comments
 (0)