Skip to content

Commit f8ba192

Browse files
committed
Auto merge of #9576 - TennyZhuang:unnecessary_cast_for_non_literal_expr, r=llogiq
let unnecessary_cast work for trivial non_literal expressions Signed-off-by: TennyZhuang <[email protected]> --- changelog: [`unnecessary_cast`]: fix for trivial non_literal expressions Fixes #9562
2 parents 11a6d19 + c9b9314 commit f8ba192

24 files changed

+141
-100
lines changed

clippy_lints/src/casts/unnecessary_cast.rs

+25-18
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@ pub(super) fn check<'tcx>(
3131
}
3232
}
3333

34+
let cast_str = snippet_opt(cx, cast_expr.span).unwrap_or_default();
35+
3436
if let Some(lit) = get_numeric_literal(cast_expr) {
35-
let literal_str = snippet_opt(cx, cast_expr.span).unwrap_or_default();
37+
let literal_str = &cast_str;
3638

3739
if_chain! {
3840
if let LitKind::Int(n, _) = lit.node;
@@ -50,39 +52,44 @@ pub(super) fn check<'tcx>(
5052

5153
match lit.node {
5254
LitKind::Int(_, LitIntType::Unsuffixed) if cast_to.is_integral() => {
53-
lint_unnecessary_cast(cx, expr, &literal_str, cast_from, cast_to);
55+
lint_unnecessary_cast(cx, expr, literal_str, cast_from, cast_to);
56+
return false;
5457
},
5558
LitKind::Float(_, LitFloatType::Unsuffixed) if cast_to.is_floating_point() => {
56-
lint_unnecessary_cast(cx, expr, &literal_str, cast_from, cast_to);
59+
lint_unnecessary_cast(cx, expr, literal_str, cast_from, cast_to);
60+
return false;
61+
},
62+
LitKind::Int(_, LitIntType::Unsuffixed) | LitKind::Float(_, LitFloatType::Unsuffixed) => {
63+
return false;
5764
},
58-
LitKind::Int(_, LitIntType::Unsuffixed) | LitKind::Float(_, LitFloatType::Unsuffixed) => {},
5965
LitKind::Int(_, LitIntType::Signed(_) | LitIntType::Unsigned(_))
6066
| LitKind::Float(_, LitFloatType::Suffixed(_))
6167
if cast_from.kind() == cast_to.kind() =>
6268
{
6369
if let Some(src) = snippet_opt(cx, cast_expr.span) {
6470
if let Some(num_lit) = NumericLiteral::from_lit_kind(&src, &lit.node) {
6571
lint_unnecessary_cast(cx, expr, num_lit.integer, cast_from, cast_to);
72+
return true;
6673
}
6774
}
6875
},
69-
_ => {
70-
if cast_from.kind() == cast_to.kind() && !in_external_macro(cx.sess(), expr.span) {
71-
span_lint_and_sugg(
72-
cx,
73-
UNNECESSARY_CAST,
74-
expr.span,
75-
&format!("casting to the same type is unnecessary (`{cast_from}` -> `{cast_to}`)"),
76-
"try",
77-
literal_str,
78-
Applicability::MachineApplicable,
79-
);
80-
return true;
81-
}
82-
},
76+
_ => {},
8377
}
8478
}
8579

80+
if cast_from.kind() == cast_to.kind() && !in_external_macro(cx.sess(), expr.span) {
81+
span_lint_and_sugg(
82+
cx,
83+
UNNECESSARY_CAST,
84+
expr.span,
85+
&format!("casting to the same type is unnecessary (`{cast_from}` -> `{cast_to}`)"),
86+
"try",
87+
cast_str,
88+
Applicability::MachineApplicable,
89+
);
90+
return true;
91+
}
92+
8693
false
8794
}
8895

tests/ui/floating_point_exp.fixed

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// run-rustfix
22
#![warn(clippy::imprecise_flops)]
3+
#![allow(clippy::unnecessary_cast)]
34

45
fn main() {
56
let x = 2f32;

tests/ui/floating_point_exp.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// run-rustfix
22
#![warn(clippy::imprecise_flops)]
3+
#![allow(clippy::unnecessary_cast)]
34

45
fn main() {
56
let x = 2f32;

tests/ui/floating_point_exp.stderr

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
error: (e.pow(x) - 1) can be computed more accurately
2-
--> $DIR/floating_point_exp.rs:6:13
2+
--> $DIR/floating_point_exp.rs:7:13
33
|
44
LL | let _ = x.exp() - 1.0;
55
| ^^^^^^^^^^^^^ help: consider using: `x.exp_m1()`
66
|
77
= note: `-D clippy::imprecise-flops` implied by `-D warnings`
88

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

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

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

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

tests/ui/floating_point_log.fixed

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// run-rustfix
2-
#![allow(dead_code, clippy::double_parens)]
2+
#![allow(dead_code, clippy::double_parens, clippy::unnecessary_cast)]
33
#![warn(clippy::suboptimal_flops, clippy::imprecise_flops)]
44

55
const TWO: f32 = 2.0;

tests/ui/floating_point_log.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// run-rustfix
2-
#![allow(dead_code, clippy::double_parens)]
2+
#![allow(dead_code, clippy::double_parens, clippy::unnecessary_cast)]
33
#![warn(clippy::suboptimal_flops, clippy::imprecise_flops)]
44

55
const TWO: f32 = 2.0;

tests/ui/floating_point_logbase.fixed

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// run-rustfix
22
#![warn(clippy::suboptimal_flops)]
3+
#![allow(clippy::unnecessary_cast)]
34

45
fn main() {
56
let x = 3f32;

tests/ui/floating_point_logbase.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// run-rustfix
22
#![warn(clippy::suboptimal_flops)]
3+
#![allow(clippy::unnecessary_cast)]
34

45
fn main() {
56
let x = 3f32;

tests/ui/floating_point_logbase.stderr

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
error: log base can be expressed more clearly
2-
--> $DIR/floating_point_logbase.rs:7:13
2+
--> $DIR/floating_point_logbase.rs:8:13
33
|
44
LL | let _ = x.ln() / y.ln();
55
| ^^^^^^^^^^^^^^^ help: consider using: `x.log(y)`
66
|
77
= note: `-D clippy::suboptimal-flops` implied by `-D warnings`
88

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

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

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

2727
error: log base can be expressed more clearly
28-
--> $DIR/floating_point_logbase.rs:11:13
28+
--> $DIR/floating_point_logbase.rs:12:13
2929
|
3030
LL | let _ = x.log(5f32) / y.log(5f32);
3131
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `x.log(y)`

tests/ui/floating_point_powf.fixed

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// run-rustfix
22
#![warn(clippy::suboptimal_flops, clippy::imprecise_flops)]
3+
#![allow(clippy::unnecessary_cast)]
34

45
fn main() {
56
let x = 3f32;

tests/ui/floating_point_powf.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// run-rustfix
22
#![warn(clippy::suboptimal_flops, clippy::imprecise_flops)]
3+
#![allow(clippy::unnecessary_cast)]
34

45
fn main() {
56
let x = 3f32;

0 commit comments

Comments
 (0)