mirror of
https://github.com/rust-lang/rust.git
synced 2025-02-18 01:44:04 +00:00
Resolve false positives for hex int cast.
This commit is contained in:
parent
9ff4581cd3
commit
6673cc8329
@ -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.
|
||||||
radix: Radix,
|
pub radix: Radix,
|
||||||
/// The radix prefix, if present.
|
/// The radix prefix, if present.
|
||||||
prefix: Option<&'a str>,
|
prefix: Option<&'a str>,
|
||||||
|
|
||||||
@ -147,16 +147,20 @@ pub(super) struct NumericLiteral<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> NumericLiteral<'a> {
|
impl<'a> NumericLiteral<'a> {
|
||||||
fn from_lit(src: &'a str, lit: &Lit) -> 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);
|
||||||
let float = if let LitKind::Float(..) = lit.kind { true } else { false };
|
let float = if let LitKind::Float(..) = lit_kind { true } else { false };
|
||||||
Some(NumericLiteral::new(unsuffixed, suffix, float))
|
Some(NumericLiteral::new(unsuffixed, suffix, float))
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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.
|
||||||
|
@ -27,6 +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::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,
|
||||||
@ -1210,21 +1211,27 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Casts {
|
|||||||
let (cast_from, cast_to) = (cx.tables.expr_ty(ex), cx.tables.expr_ty(expr));
|
let (cast_from, cast_to) = (cx.tables.expr_ty(ex), cx.tables.expr_ty(expr));
|
||||||
lint_fn_to_numeric_cast(cx, expr, ex, cast_from, cast_to);
|
lint_fn_to_numeric_cast(cx, expr, ex, cast_from, cast_to);
|
||||||
if let ExprKind::Lit(ref lit) = ex.kind {
|
if let ExprKind::Lit(ref lit) = ex.kind {
|
||||||
if let LitKind::Int(n, _) = lit.node {
|
if_chain! {
|
||||||
if cast_to.is_floating_point() {
|
if let LitKind::Int(n, _) = lit.node;
|
||||||
|
if let Some(src) = snippet_opt(cx, lit.span);
|
||||||
|
if cast_to.is_floating_point();
|
||||||
|
then {
|
||||||
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 from_nbits != 0 && to_nbits != 0 && from_nbits <= to_nbits {
|
if let Some(num_lit) = NumericLiteral::from_lit_kind(&src, &lit.node) {
|
||||||
span_lint_and_sugg(
|
if from_nbits != 0 && to_nbits != 0 && from_nbits <= to_nbits &&
|
||||||
cx,
|
num_lit.radix != Radix::Hexadecimal {
|
||||||
UNNECESSARY_CAST,
|
span_lint_and_sugg(
|
||||||
expr.span,
|
cx,
|
||||||
&format!("casting integer literal to `{}` is unnecessary", cast_to),
|
UNNECESSARY_CAST,
|
||||||
"try",
|
expr.span,
|
||||||
format!("{}_{}", n, cast_to),
|
&format!("casting integer literal to `{}` is unnecessary", cast_to),
|
||||||
Applicability::MachineApplicable,
|
"try",
|
||||||
);
|
format!("{}_{}", n, cast_to),
|
||||||
return;
|
Applicability::MachineApplicable,
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user