1
Fork 0

Auto merge of #8350 - dswij:8331, r=Manishearth

fix bad suggestion on `numeric_literal`

closes #8331

changelog: [`numeric_literal`]  fix suggestion not showing sign
This commit is contained in:
bors 2022-01-26 19:19:28 +00:00
commit ea4db3a6c6
4 changed files with 52 additions and 13 deletions

View file

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

View file

@ -51,7 +51,14 @@ impl<'a> NumericLiteral<'a> {
} }
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)) { 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 (unsuffixed, suffix) = split_suffix(src, lit_kind);
let float = matches!(lit_kind, LitKind::Float(..)); let float = matches!(lit_kind, LitKind::Float(..));
Some(NumericLiteral::new(unsuffixed, suffix, float)) Some(NumericLiteral::new(unsuffixed, suffix, float))

View file

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

View file

@ -1,5 +1,5 @@
error: casting integer literal to `i32` is unnecessary error: casting integer literal to `i32` is unnecessary
--> $DIR/unnecessary_cast.rs:6:5 --> $DIR/unnecessary_cast.rs:7:5
| |
LL | 1i32 as i32; LL | 1i32 as i32;
| ^^^^^^^^^^^ help: try: `1_i32` | ^^^^^^^^^^^ help: try: `1_i32`
@ -7,16 +7,46 @@ LL | 1i32 as i32;
= note: `-D clippy::unnecessary-cast` implied by `-D warnings` = note: `-D clippy::unnecessary-cast` implied by `-D warnings`
error: casting float literal to `f32` is unnecessary error: casting float literal to `f32` is unnecessary
--> $DIR/unnecessary_cast.rs:7:5 --> $DIR/unnecessary_cast.rs:8:5
| |
LL | 1f32 as f32; LL | 1f32 as f32;
| ^^^^^^^^^^^ help: try: `1_f32` | ^^^^^^^^^^^ help: try: `1_f32`
error: casting to the same type is unnecessary (`bool` -> `bool`) 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; LL | false as bool;
| ^^^^^^^^^^^^^ help: try: `false` | ^^^^^^^^^^^^^ 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