fix bad suggestion on numeric_literal

This commit is contained in:
Dharma Saputra Wijaya 2022-01-25 22:19:19 +08:00 committed by dswij
parent a26c412e28
commit 0d7273fef6
4 changed files with 52 additions and 13 deletions

View File

@ -23,15 +23,14 @@ pub(super) fn check(
if_chain! {
if let LitKind::Int(n, _) = lit.node;
if let Some(src) = snippet_opt(cx, lit.span);
if let Some(src) = snippet_opt(cx, cast_expr.span);
if cast_to.is_floating_point();
if let Some(num_lit) = NumericLiteral::from_lit_kind(&src, &lit.node);
let from_nbits = 128 - n.leading_zeros();
let to_nbits = fp_ty_mantissa_nbits(cast_to);
if from_nbits != 0 && to_nbits != 0 && from_nbits <= to_nbits && num_lit.is_decimal();
then {
let literal_str = if is_unary_neg(cast_expr) { format!("-{}", num_lit.integer) } else { num_lit.integer.into() };
lint_unnecessary_cast(cx, expr, &literal_str, cast_from, cast_to);
lint_unnecessary_cast(cx, expr, num_lit.integer, cast_from, cast_to);
return true
}
}
@ -48,7 +47,7 @@ pub(super) fn check(
| LitKind::Float(_, LitFloatType::Suffixed(_))
if cast_from.kind() == cast_to.kind() =>
{
if let Some(src) = snippet_opt(cx, lit.span) {
if let Some(src) = snippet_opt(cx, cast_expr.span) {
if let Some(num_lit) = NumericLiteral::from_lit_kind(&src, &lit.node) {
lint_unnecessary_cast(cx, expr, num_lit.integer, cast_from, cast_to);
}
@ -113,7 +112,3 @@ fn fp_ty_mantissa_nbits(typ: Ty<'_>) -> u32 {
_ => 0,
}
}
fn is_unary_neg(expr: &Expr<'_>) -> bool {
matches!(expr.kind, ExprKind::Unary(UnOp::Neg, _))
}

View File

@ -51,7 +51,14 @@ pub 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)) {
let unsigned_src = src.strip_prefix('-').map_or(src, |s| s);
if lit_kind.is_numeric()
&& unsigned_src
.trim_start()
.chars()
.next()
.map_or(false, |c| c.is_digit(10))
{
let (unsuffixed, suffix) = split_suffix(src, lit_kind);
let float = matches!(lit_kind, LitKind::Float(..));
Some(NumericLiteral::new(unsuffixed, suffix, float))

View File

@ -1,6 +1,7 @@
#![warn(clippy::unnecessary_cast)]
#![allow(clippy::no_effect)]
#[rustfmt::skip]
fn main() {
// Test cast_unnecessary
1i32 as i32;
@ -8,6 +9,12 @@ fn main() {
false as bool;
&1i32 as &i32;
-1_i32 as i32;
- 1_i32 as i32;
-1f32 as f32;
1_i32 as i32;
1_f32 as f32;
// macro version
macro_rules! foo {
($a:ident, $b:ident) => {

View File

@ -1,5 +1,5 @@
error: casting integer literal to `i32` is unnecessary
--> $DIR/unnecessary_cast.rs:6:5
--> $DIR/unnecessary_cast.rs:7:5
|
LL | 1i32 as i32;
| ^^^^^^^^^^^ help: try: `1_i32`
@ -7,16 +7,46 @@ LL | 1i32 as i32;
= note: `-D clippy::unnecessary-cast` implied by `-D warnings`
error: casting float literal to `f32` is unnecessary
--> $DIR/unnecessary_cast.rs:7:5
--> $DIR/unnecessary_cast.rs:8:5
|
LL | 1f32 as f32;
| ^^^^^^^^^^^ help: try: `1_f32`
error: casting to the same type is unnecessary (`bool` -> `bool`)
--> $DIR/unnecessary_cast.rs:8:5
--> $DIR/unnecessary_cast.rs:9:5
|
LL | false as bool;
| ^^^^^^^^^^^^^ help: try: `false`
error: aborting due to 3 previous errors
error: casting integer literal to `i32` is unnecessary
--> $DIR/unnecessary_cast.rs:12:5
|
LL | -1_i32 as i32;
| ^^^^^^^^^^^^^ help: try: `-1_i32`
error: casting integer literal to `i32` is unnecessary
--> $DIR/unnecessary_cast.rs:13:5
|
LL | - 1_i32 as i32;
| ^^^^^^^^^^^^^^ help: try: `- 1_i32`
error: casting float literal to `f32` is unnecessary
--> $DIR/unnecessary_cast.rs:14:5
|
LL | -1f32 as f32;
| ^^^^^^^^^^^^ help: try: `-1_f32`
error: casting integer literal to `i32` is unnecessary
--> $DIR/unnecessary_cast.rs:15:5
|
LL | 1_i32 as i32;
| ^^^^^^^^^^^^ help: try: `1_i32`
error: casting float literal to `f32` is unnecessary
--> $DIR/unnecessary_cast.rs:16:5
|
LL | 1_f32 as f32;
| ^^^^^^^^^^^^ help: try: `1_f32`
error: aborting due to 8 previous errors