diff options
| author | TennyZhuang <zty0826@gmail.com> | 2022-10-02 17:12:08 +0800 |
|---|---|---|
| committer | TennyZhuang <zty0826@gmail.com> | 2022-10-02 23:02:11 +0800 |
| commit | 081f73954b24e87bbbd95ed20f9a504680b21d46 (patch) | |
| tree | a8cf7ca73ce92e1b88a583f45d7f3771e3afe4da | |
| parent | ac12011315e0447cf7294102a342793c1d8c7cb8 (diff) | |
| download | rust-081f73954b24e87bbbd95ed20f9a504680b21d46.tar.gz rust-081f73954b24e87bbbd95ed20f9a504680b21d46.zip | |
let unnecessary_cast work for trivial non_literal expressions
Signed-off-by: TennyZhuang <zty0826@gmail.com>
| -rw-r--r-- | clippy_lints/src/casts/unnecessary_cast.rs | 15 | ||||
| -rw-r--r-- | tests/ui/unnecessary_cast.fixed | 8 | ||||
| -rw-r--r-- | tests/ui/unnecessary_cast.rs | 8 | ||||
| -rw-r--r-- | tests/ui/unnecessary_cast.stderr | 8 |
4 files changed, 37 insertions, 2 deletions
diff --git a/clippy_lints/src/casts/unnecessary_cast.rs b/clippy_lints/src/casts/unnecessary_cast.rs index 031cedb9072..071fab1165d 100644 --- a/clippy_lints/src/casts/unnecessary_cast.rs +++ b/clippy_lints/src/casts/unnecessary_cast.rs @@ -31,8 +31,10 @@ pub(super) fn check<'tcx>( } } + let cast_str = snippet_opt(cx, cast_expr.span).unwrap_or_default(); + if let Some(lit) = get_numeric_literal(cast_expr) { - let literal_str = snippet_opt(cx, cast_expr.span).unwrap_or_default(); + let literal_str = cast_str; if_chain! { if let LitKind::Int(n, _) = lit.node; @@ -81,6 +83,17 @@ pub(super) fn check<'tcx>( } }, } + } else if cast_from.kind() == cast_to.kind() && !in_external_macro(cx.sess(), expr.span) { + span_lint_and_sugg( + cx, + UNNECESSARY_CAST, + expr.span, + &format!("casting to the same type is unnecessary (`{cast_from}` -> `{cast_to}`)"), + "try", + cast_str, + Applicability::MachineApplicable, + ); + return true; } false diff --git a/tests/ui/unnecessary_cast.fixed b/tests/ui/unnecessary_cast.fixed index 70ec3e94380..94dc9642726 100644 --- a/tests/ui/unnecessary_cast.fixed +++ b/tests/ui/unnecessary_cast.fixed @@ -103,4 +103,12 @@ mod fixable { #[allow(clippy::precedence)] let _: f64 = -8.0_f64.exp(); // should suggest `-8.0_f64.exp()` here not to change code behavior } + + fn issue_9562_non_literal() { + fn foo() -> f32 { + 0. + } + + let _num = foo(); + } } diff --git a/tests/ui/unnecessary_cast.rs b/tests/ui/unnecessary_cast.rs index 36c1a87fab6..e5150256f69 100644 --- a/tests/ui/unnecessary_cast.rs +++ b/tests/ui/unnecessary_cast.rs @@ -103,4 +103,12 @@ mod fixable { #[allow(clippy::precedence)] let _: f64 = -(8.0 as f64).exp(); // should suggest `-8.0_f64.exp()` here not to change code behavior } + + fn issue_9562_non_literal() { + fn foo() -> f32 { + 0. + } + + let _num = foo() as f32; + } } diff --git a/tests/ui/unnecessary_cast.stderr b/tests/ui/unnecessary_cast.stderr index a52b92c339c..e5c3dd5e53f 100644 --- a/tests/ui/unnecessary_cast.stderr +++ b/tests/ui/unnecessary_cast.stderr @@ -174,5 +174,11 @@ error: casting float literal to `f64` is unnecessary LL | let _: f64 = -(8.0 as f64).exp(); // should suggest `-8.0_f64.exp()` here not to change code behavior | ^^^^^^^^^^^^ help: try: `8.0_f64` -error: aborting due to 29 previous errors +error: casting to the same type is unnecessary (`f32` -> `f32`) + --> $DIR/unnecessary_cast.rs:112:20 + | +LL | let _num = foo() as f32; + | ^^^^^^^^^^^^ help: try: `foo()` + +error: aborting due to 30 previous errors |
