Skip to content

Commit bb0ce32

Browse files
committed
Lint unnecessary int-to-int and float-to-float casts
1 parent febd008 commit bb0ce32

File tree

6 files changed

+111
-41
lines changed

6 files changed

+111
-41
lines changed

clippy_lints/src/types.rs

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use std::borrow::Cow;
44
use std::cmp::Ordering;
55
use std::collections::BTreeMap;
6+
use std::fmt::Display;
67

78
use if_chain::if_chain;
89
use rustc_ast::{FloatTy, IntTy, LitFloatType, LitIntType, LitKind, UintTy};
@@ -1608,18 +1609,23 @@ impl<'tcx> LateLintPass<'tcx> for Casts {
16081609
let to_nbits = fp_ty_mantissa_nbits(cast_to);
16091610
if from_nbits != 0 && to_nbits != 0 && from_nbits <= to_nbits && num_lit.is_decimal();
16101611
then {
1611-
span_lint_and_sugg(
1612-
cx,
1613-
UNNECESSARY_CAST,
1614-
expr.span,
1615-
&format!("casting integer literal to `{}` is unnecessary", cast_to),
1616-
"try",
1617-
format!("{}_{}", n, cast_to),
1618-
Applicability::MachineApplicable,
1619-
);
1612+
show_unnecessary_cast(cx, expr, n , cast_from, cast_to);
16201613
return;
16211614
}
16221615
}
1616+
1617+
match lit.node {
1618+
LitKind::Int(num, LitIntType::Unsuffixed) if cast_to.is_integral() => {
1619+
show_unnecessary_cast(cx, expr, num, cast_from, cast_to);
1620+
return;
1621+
},
1622+
LitKind::Float(num, LitFloatType::Unsuffixed) if cast_to.is_floating_point() => {
1623+
show_unnecessary_cast(cx, expr, num, cast_from, cast_to);
1624+
return;
1625+
},
1626+
_ => (),
1627+
};
1628+
16231629
match lit.node {
16241630
LitKind::Int(_, LitIntType::Unsuffixed) | LitKind::Float(_, LitFloatType::Unsuffixed) => {},
16251631
_ => {
@@ -1646,6 +1652,25 @@ impl<'tcx> LateLintPass<'tcx> for Casts {
16461652
}
16471653
}
16481654

1655+
fn show_unnecessary_cast<Num: Display>(
1656+
cx: &LateContext<'_>,
1657+
expr: &Expr<'_>,
1658+
num: Num,
1659+
cast_from: Ty<'_>,
1660+
cast_to: Ty<'_>,
1661+
) {
1662+
let literal_kind_name = if cast_from.is_integral() { "integer" } else { "float" };
1663+
span_lint_and_sugg(
1664+
cx,
1665+
UNNECESSARY_CAST,
1666+
expr.span,
1667+
&format!("casting {} literal to `{}` is unnecessary", literal_kind_name, cast_to),
1668+
"try",
1669+
format!("{}_{}", num, cast_to),
1670+
Applicability::MachineApplicable,
1671+
);
1672+
}
1673+
16491674
fn lint_numeric_casts<'tcx>(
16501675
cx: &LateContext<'tcx>,
16511676
expr: &Expr<'tcx>,

tests/ui/eq_op.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#[allow(clippy::no_effect, unused_variables, clippy::unnecessary_operation, clippy::short_circuit_statement)]
77
#[allow(clippy::nonminimal_bool)]
88
#[allow(unused)]
9+
#[allow(clippy::unnecessary_cast)]
910
fn main() {
1011
// simple values and comparisons
1112
1 == 1;

tests/ui/eq_op.stderr

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,163 +1,163 @@
11
error: equal expressions as operands to `==`
2-
--> $DIR/eq_op.rs:11:5
2+
--> $DIR/eq_op.rs:12:5
33
|
44
LL | 1 == 1;
55
| ^^^^^^
66
|
77
= note: `-D clippy::eq-op` implied by `-D warnings`
88

99
error: equal expressions as operands to `==`
10-
--> $DIR/eq_op.rs:12:5
10+
--> $DIR/eq_op.rs:13:5
1111
|
1212
LL | "no" == "no";
1313
| ^^^^^^^^^^^^
1414

1515
error: equal expressions as operands to `!=`
16-
--> $DIR/eq_op.rs:14:5
16+
--> $DIR/eq_op.rs:15:5
1717
|
1818
LL | false != false;
1919
| ^^^^^^^^^^^^^^
2020

2121
error: equal expressions as operands to `<`
22-
--> $DIR/eq_op.rs:15:5
22+
--> $DIR/eq_op.rs:16:5
2323
|
2424
LL | 1.5 < 1.5;
2525
| ^^^^^^^^^
2626

2727
error: equal expressions as operands to `>=`
28-
--> $DIR/eq_op.rs:16:5
28+
--> $DIR/eq_op.rs:17:5
2929
|
3030
LL | 1u64 >= 1u64;
3131
| ^^^^^^^^^^^^
3232

3333
error: equal expressions as operands to `&`
34-
--> $DIR/eq_op.rs:19:5
34+
--> $DIR/eq_op.rs:20:5
3535
|
3636
LL | (1 as u64) & (1 as u64);
3737
| ^^^^^^^^^^^^^^^^^^^^^^^
3838

3939
error: equal expressions as operands to `^`
40-
--> $DIR/eq_op.rs:20:5
40+
--> $DIR/eq_op.rs:21:5
4141
|
4242
LL | 1 ^ ((((((1))))));
4343
| ^^^^^^^^^^^^^^^^^
4444

4545
error: equal expressions as operands to `<`
46-
--> $DIR/eq_op.rs:23:5
46+
--> $DIR/eq_op.rs:24:5
4747
|
4848
LL | (-(2) < -(2));
4949
| ^^^^^^^^^^^^^
5050

5151
error: equal expressions as operands to `==`
52-
--> $DIR/eq_op.rs:24:5
52+
--> $DIR/eq_op.rs:25:5
5353
|
5454
LL | ((1 + 1) & (1 + 1) == (1 + 1) & (1 + 1));
5555
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5656

5757
error: equal expressions as operands to `&`
58-
--> $DIR/eq_op.rs:24:6
58+
--> $DIR/eq_op.rs:25:6
5959
|
6060
LL | ((1 + 1) & (1 + 1) == (1 + 1) & (1 + 1));
6161
| ^^^^^^^^^^^^^^^^^
6262

6363
error: equal expressions as operands to `&`
64-
--> $DIR/eq_op.rs:24:27
64+
--> $DIR/eq_op.rs:25:27
6565
|
6666
LL | ((1 + 1) & (1 + 1) == (1 + 1) & (1 + 1));
6767
| ^^^^^^^^^^^^^^^^^
6868

6969
error: equal expressions as operands to `==`
70-
--> $DIR/eq_op.rs:25:5
70+
--> $DIR/eq_op.rs:26:5
7171
|
7272
LL | (1 * 2) + (3 * 4) == 1 * 2 + 3 * 4;
7373
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
7474

7575
error: equal expressions as operands to `!=`
76-
--> $DIR/eq_op.rs:28:5
76+
--> $DIR/eq_op.rs:29:5
7777
|
7878
LL | ([1] != [1]);
7979
| ^^^^^^^^^^^^
8080

8181
error: equal expressions as operands to `!=`
82-
--> $DIR/eq_op.rs:29:5
82+
--> $DIR/eq_op.rs:30:5
8383
|
8484
LL | ((1, 2) != (1, 2));
8585
| ^^^^^^^^^^^^^^^^^^
8686

8787
error: equal expressions as operands to `==`
88-
--> $DIR/eq_op.rs:33:5
88+
--> $DIR/eq_op.rs:34:5
8989
|
9090
LL | 1 + 1 == 2;
9191
| ^^^^^^^^^^
9292

9393
error: equal expressions as operands to `==`
94-
--> $DIR/eq_op.rs:34:5
94+
--> $DIR/eq_op.rs:35:5
9595
|
9696
LL | 1 - 1 == 0;
9797
| ^^^^^^^^^^
9898

9999
error: equal expressions as operands to `-`
100-
--> $DIR/eq_op.rs:34:5
100+
--> $DIR/eq_op.rs:35:5
101101
|
102102
LL | 1 - 1 == 0;
103103
| ^^^^^
104104

105105
error: equal expressions as operands to `-`
106-
--> $DIR/eq_op.rs:36:5
106+
--> $DIR/eq_op.rs:37:5
107107
|
108108
LL | 1 - 1;
109109
| ^^^^^
110110

111111
error: equal expressions as operands to `/`
112-
--> $DIR/eq_op.rs:37:5
112+
--> $DIR/eq_op.rs:38:5
113113
|
114114
LL | 1 / 1;
115115
| ^^^^^
116116

117117
error: equal expressions as operands to `&&`
118-
--> $DIR/eq_op.rs:38:5
118+
--> $DIR/eq_op.rs:39:5
119119
|
120120
LL | true && true;
121121
| ^^^^^^^^^^^^
122122

123123
error: equal expressions as operands to `||`
124-
--> $DIR/eq_op.rs:40:5
124+
--> $DIR/eq_op.rs:41:5
125125
|
126126
LL | true || true;
127127
| ^^^^^^^^^^^^
128128

129129
error: equal expressions as operands to `&&`
130-
--> $DIR/eq_op.rs:46:5
130+
--> $DIR/eq_op.rs:47:5
131131
|
132132
LL | a == b && b == a;
133133
| ^^^^^^^^^^^^^^^^
134134

135135
error: equal expressions as operands to `&&`
136-
--> $DIR/eq_op.rs:47:5
136+
--> $DIR/eq_op.rs:48:5
137137
|
138138
LL | a != b && b != a;
139139
| ^^^^^^^^^^^^^^^^
140140

141141
error: equal expressions as operands to `&&`
142-
--> $DIR/eq_op.rs:48:5
142+
--> $DIR/eq_op.rs:49:5
143143
|
144144
LL | a < b && b > a;
145145
| ^^^^^^^^^^^^^^
146146

147147
error: equal expressions as operands to `&&`
148-
--> $DIR/eq_op.rs:49:5
148+
--> $DIR/eq_op.rs:50:5
149149
|
150150
LL | a <= b && b >= a;
151151
| ^^^^^^^^^^^^^^^^
152152

153153
error: equal expressions as operands to `==`
154-
--> $DIR/eq_op.rs:52:5
154+
--> $DIR/eq_op.rs:53:5
155155
|
156156
LL | a == a;
157157
| ^^^^^^
158158

159159
error: equal expressions as operands to `/`
160-
--> $DIR/eq_op.rs:62:20
160+
--> $DIR/eq_op.rs:63:20
161161
|
162162
LL | const D: u32 = A / A;
163163
| ^^^^^

tests/ui/unnecessary_cast_fixable.fixed

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,19 @@ fn main() {
1212
#[rustfmt::skip]
1313
let v = vec!(1);
1414
&v as &[i32];
15-
1.0 as f64;
16-
1 as u64;
1715
0x10 as f32;
1816
0o10 as f32;
1917
0b10 as f32;
2018
0x11 as f64;
2119
0o11 as f64;
2220
0b11 as f64;
21+
22+
1_u32;
23+
16_i32;
24+
2_usize;
25+
26+
1.0_f64;
27+
0.5_f32;
28+
29+
1.0 as u16;
2330
}

tests/ui/unnecessary_cast_fixable.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,19 @@ fn main() {
1212
#[rustfmt::skip]
1313
let v = vec!(1);
1414
&v as &[i32];
15-
1.0 as f64;
16-
1 as u64;
1715
0x10 as f32;
1816
0o10 as f32;
1917
0b10 as f32;
2018
0x11 as f64;
2119
0o11 as f64;
2220
0b11 as f64;
21+
22+
1 as u32;
23+
0x10 as i32;
24+
0b10 as usize;
25+
26+
1.0 as f64;
27+
0.5 as f32;
28+
29+
1.0 as u16;
2330
}

tests/ui/unnecessary_cast_fixable.stderr

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,35 @@ error: casting integer literal to `f64` is unnecessary
1818
LL | 100_i32 as f64;
1919
| ^^^^^^^^^^^^^^ help: try: `100_f64`
2020

21-
error: aborting due to 3 previous errors
21+
error: casting integer literal to `u32` is unnecessary
22+
--> $DIR/unnecessary_cast_fixable.rs:22:5
23+
|
24+
LL | 1 as u32;
25+
| ^^^^^^^^ help: try: `1_u32`
26+
27+
error: casting integer literal to `i32` is unnecessary
28+
--> $DIR/unnecessary_cast_fixable.rs:23:5
29+
|
30+
LL | 0x10 as i32;
31+
| ^^^^^^^^^^^ help: try: `16_i32`
32+
33+
error: casting integer literal to `usize` is unnecessary
34+
--> $DIR/unnecessary_cast_fixable.rs:24:5
35+
|
36+
LL | 0b10 as usize;
37+
| ^^^^^^^^^^^^^ help: try: `2_usize`
38+
39+
error: casting float literal to `f64` is unnecessary
40+
--> $DIR/unnecessary_cast_fixable.rs:26:5
41+
|
42+
LL | 1.0 as f64;
43+
| ^^^^^^^^^^ help: try: `1.0_f64`
44+
45+
error: casting float literal to `f32` is unnecessary
46+
--> $DIR/unnecessary_cast_fixable.rs:27:5
47+
|
48+
LL | 0.5 as f32;
49+
| ^^^^^^^^^^ help: try: `0.5_f32`
50+
51+
error: aborting due to 8 previous errors
2252

0 commit comments

Comments
 (0)