Skip to content

let unnecessary_cast work for trivial non_literal expressions #9576

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 25 additions & 18 deletions clippy_lints/src/casts/unnecessary_cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -50,39 +52,44 @@ pub(super) fn check<'tcx>(

match lit.node {
LitKind::Int(_, LitIntType::Unsuffixed) if cast_to.is_integral() => {
lint_unnecessary_cast(cx, expr, &literal_str, cast_from, cast_to);
lint_unnecessary_cast(cx, expr, literal_str, cast_from, cast_to);
return false;
},
LitKind::Float(_, LitFloatType::Unsuffixed) if cast_to.is_floating_point() => {
lint_unnecessary_cast(cx, expr, &literal_str, cast_from, cast_to);
lint_unnecessary_cast(cx, expr, literal_str, cast_from, cast_to);
return false;
},
LitKind::Int(_, LitIntType::Unsuffixed) | LitKind::Float(_, LitFloatType::Unsuffixed) => {
return false;
},
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, 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);
return true;
}
}
},
_ => {
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",
literal_str,
Applicability::MachineApplicable,
);
return true;
}
},
_ => {},
}
}

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
}

Expand Down
1 change: 1 addition & 0 deletions tests/ui/floating_point_exp.fixed
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// run-rustfix
#![warn(clippy::imprecise_flops)]
#![allow(clippy::unnecessary_cast)]

fn main() {
let x = 2f32;
Expand Down
1 change: 1 addition & 0 deletions tests/ui/floating_point_exp.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// run-rustfix
#![warn(clippy::imprecise_flops)]
#![allow(clippy::unnecessary_cast)]

fn main() {
let x = 2f32;
Expand Down
10 changes: 5 additions & 5 deletions tests/ui/floating_point_exp.stderr
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
error: (e.pow(x) - 1) can be computed more accurately
--> $DIR/floating_point_exp.rs:6:13
--> $DIR/floating_point_exp.rs:7:13
|
LL | let _ = x.exp() - 1.0;
| ^^^^^^^^^^^^^ help: consider using: `x.exp_m1()`
|
= note: `-D clippy::imprecise-flops` implied by `-D warnings`

error: (e.pow(x) - 1) can be computed more accurately
--> $DIR/floating_point_exp.rs:7:13
--> $DIR/floating_point_exp.rs:8:13
|
LL | let _ = x.exp() - 1.0 + 2.0;
| ^^^^^^^^^^^^^ help: consider using: `x.exp_m1()`

error: (e.pow(x) - 1) can be computed more accurately
--> $DIR/floating_point_exp.rs:8:13
--> $DIR/floating_point_exp.rs:9:13
|
LL | let _ = (x as f32).exp() - 1.0 + 2.0;
| ^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `(x as f32).exp_m1()`

error: (e.pow(x) - 1) can be computed more accurately
--> $DIR/floating_point_exp.rs:14:13
--> $DIR/floating_point_exp.rs:15:13
|
LL | let _ = x.exp() - 1.0;
| ^^^^^^^^^^^^^ help: consider using: `x.exp_m1()`

error: (e.pow(x) - 1) can be computed more accurately
--> $DIR/floating_point_exp.rs:15:13
--> $DIR/floating_point_exp.rs:16:13
|
LL | let _ = x.exp() - 1.0 + 2.0;
| ^^^^^^^^^^^^^ help: consider using: `x.exp_m1()`
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/floating_point_log.fixed
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// run-rustfix
#![allow(dead_code, clippy::double_parens)]
#![allow(dead_code, clippy::double_parens, clippy::unnecessary_cast)]
#![warn(clippy::suboptimal_flops, clippy::imprecise_flops)]

const TWO: f32 = 2.0;
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/floating_point_log.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// run-rustfix
#![allow(dead_code, clippy::double_parens)]
#![allow(dead_code, clippy::double_parens, clippy::unnecessary_cast)]
#![warn(clippy::suboptimal_flops, clippy::imprecise_flops)]

const TWO: f32 = 2.0;
Expand Down
1 change: 1 addition & 0 deletions tests/ui/floating_point_logbase.fixed
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// run-rustfix
#![warn(clippy::suboptimal_flops)]
#![allow(clippy::unnecessary_cast)]

fn main() {
let x = 3f32;
Expand Down
1 change: 1 addition & 0 deletions tests/ui/floating_point_logbase.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// run-rustfix
#![warn(clippy::suboptimal_flops)]
#![allow(clippy::unnecessary_cast)]

fn main() {
let x = 3f32;
Expand Down
10 changes: 5 additions & 5 deletions tests/ui/floating_point_logbase.stderr
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
error: log base can be expressed more clearly
--> $DIR/floating_point_logbase.rs:7:13
--> $DIR/floating_point_logbase.rs:8:13
|
LL | let _ = x.ln() / y.ln();
| ^^^^^^^^^^^^^^^ help: consider using: `x.log(y)`
|
= note: `-D clippy::suboptimal-flops` implied by `-D warnings`

error: log base can be expressed more clearly
--> $DIR/floating_point_logbase.rs:8:13
--> $DIR/floating_point_logbase.rs:9:13
|
LL | let _ = (x as f32).ln() / y.ln();
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `(x as f32).log(y)`

error: log base can be expressed more clearly
--> $DIR/floating_point_logbase.rs:9:13
--> $DIR/floating_point_logbase.rs:10:13
|
LL | let _ = x.log2() / y.log2();
| ^^^^^^^^^^^^^^^^^^^ help: consider using: `x.log(y)`

error: log base can be expressed more clearly
--> $DIR/floating_point_logbase.rs:10:13
--> $DIR/floating_point_logbase.rs:11:13
|
LL | let _ = x.log10() / y.log10();
| ^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.log(y)`

error: log base can be expressed more clearly
--> $DIR/floating_point_logbase.rs:11:13
--> $DIR/floating_point_logbase.rs:12:13
|
LL | let _ = x.log(5f32) / y.log(5f32);
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.log(y)`
Expand Down
1 change: 1 addition & 0 deletions tests/ui/floating_point_powf.fixed
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// run-rustfix
#![warn(clippy::suboptimal_flops, clippy::imprecise_flops)]
#![allow(clippy::unnecessary_cast)]

fn main() {
let x = 3f32;
Expand Down
1 change: 1 addition & 0 deletions tests/ui/floating_point_powf.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// run-rustfix
#![warn(clippy::suboptimal_flops, clippy::imprecise_flops)]
#![allow(clippy::unnecessary_cast)]

fn main() {
let x = 3f32;
Expand Down
Loading