From 6d2236f5036e89b69bdba4a364d73d159ac74187 Mon Sep 17 00:00:00 2001 From: iobtl Date: Sat, 13 Mar 2021 16:54:59 +0800 Subject: [PATCH 1/2] replace span_lint with span_lint_and_sugg along with error message --- clippy_lints/src/casts/unnecessary_cast.rs | 7 +++++-- tests/ui/unnecessary_cast.stderr | 6 +++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/clippy_lints/src/casts/unnecessary_cast.rs b/clippy_lints/src/casts/unnecessary_cast.rs index fa2a07ef1da0..171cac091c2e 100644 --- a/clippy_lints/src/casts/unnecessary_cast.rs +++ b/clippy_lints/src/casts/unnecessary_cast.rs @@ -7,7 +7,7 @@ use rustc_middle::ty::{self, FloatTy, InferTy, Ty}; use if_chain::if_chain; -use crate::utils::{numeric_literal::NumericLiteral, snippet_opt, span_lint, span_lint_and_sugg}; +use crate::utils::{numeric_literal::NumericLiteral, snippet_opt, span_lint_and_sugg}; use super::UNNECESSARY_CAST; @@ -46,7 +46,7 @@ pub(super) fn check( LitKind::Int(_, LitIntType::Unsuffixed) | LitKind::Float(_, LitFloatType::Unsuffixed) => {}, _ => { if cast_from.kind() == cast_to.kind() && !in_external_macro(cx.sess(), expr.span) { - span_lint( + span_lint_and_sugg( cx, UNNECESSARY_CAST, expr.span, @@ -54,6 +54,9 @@ pub(super) fn check( "casting to the same type is unnecessary (`{}` -> `{}`)", cast_from, cast_to ), + "try", + literal_str, + Applicability::MachineApplicable, ); return true; } diff --git a/tests/ui/unnecessary_cast.stderr b/tests/ui/unnecessary_cast.stderr index 8981d13e8eab..87261cd83331 100644 --- a/tests/ui/unnecessary_cast.stderr +++ b/tests/ui/unnecessary_cast.stderr @@ -2,7 +2,7 @@ error: casting to the same type is unnecessary (`i32` -> `i32`) --> $DIR/unnecessary_cast.rs:6:5 | LL | 1i32 as i32; - | ^^^^^^^^^^^ + | ^^^^^^^^^^^ help: try: `1i32` | = note: `-D clippy::unnecessary-cast` implied by `-D warnings` @@ -10,13 +10,13 @@ error: casting to the same type is unnecessary (`f32` -> `f32`) --> $DIR/unnecessary_cast.rs:7:5 | LL | 1f32 as f32; - | ^^^^^^^^^^^ + | ^^^^^^^^^^^ help: try: `1f32` error: casting to the same type is unnecessary (`bool` -> `bool`) --> $DIR/unnecessary_cast.rs:8:5 | LL | false as bool; - | ^^^^^^^^^^^^^ + | ^^^^^^^^^^^^^ help: try: `false` error: aborting due to 3 previous errors From 1054eb0c853aefec9787b57e103d3a1432169263 Mon Sep 17 00:00:00 2001 From: iobtl Date: Sun, 14 Mar 2021 08:09:08 +0800 Subject: [PATCH 2/2] use lint_unnecessary_cast for literals, suggest `_` if not present --- clippy_lints/src/casts/unnecessary_cast.rs | 9 +++++++++ tests/ui/unnecessary_cast.stderr | 8 ++++---- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/clippy_lints/src/casts/unnecessary_cast.rs b/clippy_lints/src/casts/unnecessary_cast.rs index 171cac091c2e..c43bbf329499 100644 --- a/clippy_lints/src/casts/unnecessary_cast.rs +++ b/clippy_lints/src/casts/unnecessary_cast.rs @@ -44,6 +44,15 @@ pub(super) fn check( lint_unnecessary_cast(cx, expr, &literal_str, cast_from, cast_to); }, LitKind::Int(_, LitIntType::Unsuffixed) | LitKind::Float(_, LitFloatType::Unsuffixed) => {}, + LitKind::Int(_, LitIntType::Signed(_) | LitIntType::Unsigned(_)) + | LitKind::Float(_, LitFloatType::Suffixed(_)) + if cast_from.kind() == cast_to.kind() => + { + if let Some(src) = snippet_opt(cx, lit.span) { + let num_lit = NumericLiteral::from_lit_kind(&src, &lit.node).unwrap(); + lint_unnecessary_cast(cx, expr, num_lit.integer, cast_from, cast_to); + } + }, _ => { if cast_from.kind() == cast_to.kind() && !in_external_macro(cx.sess(), expr.span) { span_lint_and_sugg( diff --git a/tests/ui/unnecessary_cast.stderr b/tests/ui/unnecessary_cast.stderr index 87261cd83331..70aa448af68e 100644 --- a/tests/ui/unnecessary_cast.stderr +++ b/tests/ui/unnecessary_cast.stderr @@ -1,16 +1,16 @@ -error: casting to the same type is unnecessary (`i32` -> `i32`) +error: casting integer literal to `i32` is unnecessary --> $DIR/unnecessary_cast.rs:6:5 | LL | 1i32 as i32; - | ^^^^^^^^^^^ help: try: `1i32` + | ^^^^^^^^^^^ help: try: `1_i32` | = note: `-D clippy::unnecessary-cast` implied by `-D warnings` -error: casting to the same type is unnecessary (`f32` -> `f32`) +error: casting float literal to `f32` is unnecessary --> $DIR/unnecessary_cast.rs:7:5 | LL | 1f32 as f32; - | ^^^^^^^^^^^ help: try: `1f32` + | ^^^^^^^^^^^ help: try: `1_f32` error: casting to the same type is unnecessary (`bool` -> `bool`) --> $DIR/unnecessary_cast.rs:8:5