Included binary and octal cases.
This commit is contained in:
parent
6673cc8329
commit
73deb723dd
4 changed files with 23 additions and 10 deletions
|
@ -131,7 +131,7 @@ pub fn format_numeric_literal(lit: &str, type_suffix: Option<&str>, float: bool)
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub(super) struct NumericLiteral<'a> {
|
pub(super) struct NumericLiteral<'a> {
|
||||||
/// Which radix the literal was represented in.
|
/// Which radix the literal was represented in.
|
||||||
pub radix: Radix,
|
radix: Radix,
|
||||||
/// The radix prefix, if present.
|
/// The radix prefix, if present.
|
||||||
prefix: Option<&'a str>,
|
prefix: Option<&'a str>,
|
||||||
|
|
||||||
|
@ -147,6 +147,10 @@ pub(super) struct NumericLiteral<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> NumericLiteral<'a> {
|
impl<'a> NumericLiteral<'a> {
|
||||||
|
fn from_lit(src: &'a str, lit: &Lit) -> Option<NumericLiteral<'a>> {
|
||||||
|
NumericLiteral::from_lit_kind(src, &lit.kind)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn from_lit_kind(src: &'a str, lit_kind: &LitKind) -> Option<NumericLiteral<'a>> {
|
pub fn from_lit_kind(src: &'a str, lit_kind: &LitKind) -> Option<NumericLiteral<'a>> {
|
||||||
if lit_kind.is_numeric() && src.chars().next().map_or(false, |c| c.is_digit(10)) {
|
if lit_kind.is_numeric() && src.chars().next().map_or(false, |c| c.is_digit(10)) {
|
||||||
let (unsuffixed, suffix) = split_suffix(&src, lit_kind);
|
let (unsuffixed, suffix) = split_suffix(&src, lit_kind);
|
||||||
|
@ -157,10 +161,6 @@ impl<'a> NumericLiteral<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn from_lit(src: &'a str, lit: &Lit) -> Option<NumericLiteral<'a>> {
|
|
||||||
NumericLiteral::from_lit_kind(src, &lit.kind)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[must_use]
|
#[must_use]
|
||||||
fn new(lit: &'a str, suffix: Option<&'a str>, float: bool) -> Self {
|
fn new(lit: &'a str, suffix: Option<&'a str>, float: bool) -> Self {
|
||||||
// Determine delimiter for radix prefix, if present, and radix.
|
// Determine delimiter for radix prefix, if present, and radix.
|
||||||
|
@ -199,6 +199,10 @@ impl<'a> NumericLiteral<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn is_decimal(&self) -> bool {
|
||||||
|
self.radix == Radix::Decimal
|
||||||
|
}
|
||||||
|
|
||||||
fn split_digit_parts(digits: &str, float: bool) -> (&str, Option<&str>, Option<(char, &str)>) {
|
fn split_digit_parts(digits: &str, float: bool) -> (&str, Option<&str>, Option<(char, &str)>) {
|
||||||
let mut integer = digits;
|
let mut integer = digits;
|
||||||
let mut fraction = None;
|
let mut fraction = None;
|
||||||
|
|
|
@ -27,7 +27,7 @@ use rustc_target::spec::abi::Abi;
|
||||||
use rustc_typeck::hir_ty_to_ty;
|
use rustc_typeck::hir_ty_to_ty;
|
||||||
|
|
||||||
use crate::consts::{constant, Constant};
|
use crate::consts::{constant, Constant};
|
||||||
use crate::literal_representation::{NumericLiteral, Radix};
|
use crate::literal_representation::NumericLiteral;
|
||||||
use crate::utils::paths;
|
use crate::utils::paths;
|
||||||
use crate::utils::{
|
use crate::utils::{
|
||||||
clip, comparisons, differing_macro_contexts, higher, in_constant, int_bits, last_path_segment, match_def_path,
|
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 {
|
||||||
let from_nbits = 128 - n.leading_zeros();
|
let from_nbits = 128 - n.leading_zeros();
|
||||||
let to_nbits = fp_ty_mantissa_nbits(cast_to);
|
let to_nbits = fp_ty_mantissa_nbits(cast_to);
|
||||||
if let Some(num_lit) = NumericLiteral::from_lit_kind(&src, &lit.node) {
|
if let Some(num_lit) = NumericLiteral::from_lit_kind(&src, &lit.node) {
|
||||||
if from_nbits != 0 && to_nbits != 0 && from_nbits <= to_nbits &&
|
if from_nbits != 0 && to_nbits != 0 && from_nbits <= to_nbits && num_lit.is_decimal() {
|
||||||
num_lit.radix != Radix::Hexadecimal {
|
|
||||||
span_lint_and_sugg(
|
span_lint_and_sugg(
|
||||||
cx,
|
cx,
|
||||||
UNNECESSARY_CAST,
|
UNNECESSARY_CAST,
|
||||||
|
|
|
@ -14,5 +14,10 @@ fn main() {
|
||||||
&v as &[i32];
|
&v as &[i32];
|
||||||
1.0 as f64;
|
1.0 as f64;
|
||||||
1 as u64;
|
1 as u64;
|
||||||
0x42 as f32;
|
0x10 as f32;
|
||||||
|
0o10 as f32;
|
||||||
|
0b10 as f32;
|
||||||
|
0x11 as f64;
|
||||||
|
0o11 as f64;
|
||||||
|
0b11 as f64;
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,5 +14,10 @@ fn main() {
|
||||||
&v as &[i32];
|
&v as &[i32];
|
||||||
1.0 as f64;
|
1.0 as f64;
|
||||||
1 as u64;
|
1 as u64;
|
||||||
0x42 as f32;
|
0x10 as f32;
|
||||||
|
0o10 as f32;
|
||||||
|
0b10 as f32;
|
||||||
|
0x11 as f64;
|
||||||
|
0o11 as f64;
|
||||||
|
0b11 as f64;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue