Skip to content

Commit 6feed27

Browse files
committed
Auto merge of #3985 - phansch:move_some_cast_tests, r=flip1995
Move two cast_lossless tests to their correct files First part of checking off the `tests/ui/cast.rs` checkbox in #3630.
2 parents 95e537b + 8163a1a commit 6feed27

8 files changed

+56
-51
lines changed

tests/ui/cast.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
clippy::cast_precision_loss,
33
clippy::cast_possible_truncation,
44
clippy::cast_sign_loss,
5-
clippy::cast_possible_wrap,
6-
clippy::cast_lossless
5+
clippy::cast_possible_wrap
76
)]
87
#[allow(clippy::no_effect, clippy::unnecessary_operation)]
98
fn main() {
@@ -32,10 +31,6 @@ fn main() {
3231
1u32 as i32;
3332
1u64 as i64;
3433
1usize as isize;
35-
// Test clippy::cast_lossless with casts from floating-point types
36-
1.0f32 as f64;
37-
// Test clippy::cast_lossless with an expression wrapped in parens
38-
(1u8 + 1u8) as u16;
3934
// Test clippy::cast_sign_loss
4035
1i32 as u32;
4136
-1i32 as u32;

tests/ui/cast.stderr

Lines changed: 29 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,194 +1,180 @@
11
error: casting i32 to f32 causes a loss of precision (i32 is 32 bits wide, but f32's mantissa is only 23 bits wide)
2-
--> $DIR/cast.rs:12:5
2+
--> $DIR/cast.rs:11:5
33
|
44
LL | x0 as f32;
55
| ^^^^^^^^^
66
|
77
= note: `-D clippy::cast-precision-loss` implied by `-D warnings`
88

99
error: casting i64 to f32 causes a loss of precision (i64 is 64 bits wide, but f32's mantissa is only 23 bits wide)
10-
--> $DIR/cast.rs:14:5
10+
--> $DIR/cast.rs:13:5
1111
|
1212
LL | x1 as f32;
1313
| ^^^^^^^^^
1414

1515
error: casting i64 to f64 causes a loss of precision (i64 is 64 bits wide, but f64's mantissa is only 52 bits wide)
16-
--> $DIR/cast.rs:15:5
16+
--> $DIR/cast.rs:14:5
1717
|
1818
LL | x1 as f64;
1919
| ^^^^^^^^^
2020

2121
error: casting u32 to f32 causes a loss of precision (u32 is 32 bits wide, but f32's mantissa is only 23 bits wide)
22-
--> $DIR/cast.rs:17:5
22+
--> $DIR/cast.rs:16:5
2323
|
2424
LL | x2 as f32;
2525
| ^^^^^^^^^
2626

2727
error: casting u64 to f32 causes a loss of precision (u64 is 64 bits wide, but f32's mantissa is only 23 bits wide)
28-
--> $DIR/cast.rs:19:5
28+
--> $DIR/cast.rs:18:5
2929
|
3030
LL | x3 as f32;
3131
| ^^^^^^^^^
3232

3333
error: casting u64 to f64 causes a loss of precision (u64 is 64 bits wide, but f64's mantissa is only 52 bits wide)
34-
--> $DIR/cast.rs:20:5
34+
--> $DIR/cast.rs:19:5
3535
|
3636
LL | x3 as f64;
3737
| ^^^^^^^^^
3838

3939
error: casting f32 to i32 may truncate the value
40-
--> $DIR/cast.rs:22:5
40+
--> $DIR/cast.rs:21:5
4141
|
4242
LL | 1f32 as i32;
4343
| ^^^^^^^^^^^
4444
|
4545
= note: `-D clippy::cast-possible-truncation` implied by `-D warnings`
4646

4747
error: casting f32 to u32 may truncate the value
48-
--> $DIR/cast.rs:23:5
48+
--> $DIR/cast.rs:22:5
4949
|
5050
LL | 1f32 as u32;
5151
| ^^^^^^^^^^^
5252

5353
error: casting f32 to u32 may lose the sign of the value
54-
--> $DIR/cast.rs:23:5
54+
--> $DIR/cast.rs:22:5
5555
|
5656
LL | 1f32 as u32;
5757
| ^^^^^^^^^^^
5858
|
5959
= note: `-D clippy::cast-sign-loss` implied by `-D warnings`
6060

6161
error: casting f64 to f32 may truncate the value
62-
--> $DIR/cast.rs:24:5
62+
--> $DIR/cast.rs:23:5
6363
|
6464
LL | 1f64 as f32;
6565
| ^^^^^^^^^^^
6666

6767
error: casting i32 to i8 may truncate the value
68-
--> $DIR/cast.rs:25:5
68+
--> $DIR/cast.rs:24:5
6969
|
7070
LL | 1i32 as i8;
7171
| ^^^^^^^^^^
7272

7373
error: casting i32 to u8 may truncate the value
74-
--> $DIR/cast.rs:26:5
74+
--> $DIR/cast.rs:25:5
7575
|
7676
LL | 1i32 as u8;
7777
| ^^^^^^^^^^
7878

7979
error: casting f64 to isize may truncate the value
80-
--> $DIR/cast.rs:27:5
80+
--> $DIR/cast.rs:26:5
8181
|
8282
LL | 1f64 as isize;
8383
| ^^^^^^^^^^^^^
8484

8585
error: casting f64 to usize may truncate the value
86-
--> $DIR/cast.rs:28:5
86+
--> $DIR/cast.rs:27:5
8787
|
8888
LL | 1f64 as usize;
8989
| ^^^^^^^^^^^^^
9090

9191
error: casting f64 to usize may lose the sign of the value
92-
--> $DIR/cast.rs:28:5
92+
--> $DIR/cast.rs:27:5
9393
|
9494
LL | 1f64 as usize;
9595
| ^^^^^^^^^^^^^
9696

9797
error: casting u8 to i8 may wrap around the value
98-
--> $DIR/cast.rs:30:5
98+
--> $DIR/cast.rs:29:5
9999
|
100100
LL | 1u8 as i8;
101101
| ^^^^^^^^^
102102
|
103103
= note: `-D clippy::cast-possible-wrap` implied by `-D warnings`
104104

105105
error: casting u16 to i16 may wrap around the value
106-
--> $DIR/cast.rs:31:5
106+
--> $DIR/cast.rs:30:5
107107
|
108108
LL | 1u16 as i16;
109109
| ^^^^^^^^^^^
110110

111111
error: casting u32 to i32 may wrap around the value
112-
--> $DIR/cast.rs:32:5
112+
--> $DIR/cast.rs:31:5
113113
|
114114
LL | 1u32 as i32;
115115
| ^^^^^^^^^^^
116116

117117
error: casting u64 to i64 may wrap around the value
118-
--> $DIR/cast.rs:33:5
118+
--> $DIR/cast.rs:32:5
119119
|
120120
LL | 1u64 as i64;
121121
| ^^^^^^^^^^^
122122

123123
error: casting usize to isize may wrap around the value
124-
--> $DIR/cast.rs:34:5
124+
--> $DIR/cast.rs:33:5
125125
|
126126
LL | 1usize as isize;
127127
| ^^^^^^^^^^^^^^^
128128

129-
error: casting f32 to f64 may become silently lossy if types change
130-
--> $DIR/cast.rs:36:5
131-
|
132-
LL | 1.0f32 as f64;
133-
| ^^^^^^^^^^^^^ help: try: `f64::from(1.0f32)`
134-
|
135-
= note: `-D clippy::cast-lossless` implied by `-D warnings`
136-
137-
error: casting u8 to u16 may become silently lossy if types change
138-
--> $DIR/cast.rs:38:5
139-
|
140-
LL | (1u8 + 1u8) as u16;
141-
| ^^^^^^^^^^^^^^^^^^ help: try: `u16::from(1u8 + 1u8)`
142-
143129
error: casting i32 to u32 may lose the sign of the value
144-
--> $DIR/cast.rs:41:5
130+
--> $DIR/cast.rs:36:5
145131
|
146132
LL | -1i32 as u32;
147133
| ^^^^^^^^^^^^
148134

149135
error: casting isize to usize may lose the sign of the value
150-
--> $DIR/cast.rs:43:5
136+
--> $DIR/cast.rs:38:5
151137
|
152138
LL | -1isize as usize;
153139
| ^^^^^^^^^^^^^^^^
154140

155141
error: casting to the same type is unnecessary (`i32` -> `i32`)
156-
--> $DIR/cast.rs:52:5
142+
--> $DIR/cast.rs:47:5
157143
|
158144
LL | 1i32 as i32;
159145
| ^^^^^^^^^^^
160146
|
161147
= note: `-D clippy::unnecessary-cast` implied by `-D warnings`
162148

163149
error: casting to the same type is unnecessary (`f32` -> `f32`)
164-
--> $DIR/cast.rs:53:5
150+
--> $DIR/cast.rs:48:5
165151
|
166152
LL | 1f32 as f32;
167153
| ^^^^^^^^^^^
168154

169155
error: casting to the same type is unnecessary (`bool` -> `bool`)
170-
--> $DIR/cast.rs:54:5
156+
--> $DIR/cast.rs:49:5
171157
|
172158
LL | false as bool;
173159
| ^^^^^^^^^^^^^
174160

175161
error: casting integer literal to f32 is unnecessary
176-
--> $DIR/cast.rs:57:5
162+
--> $DIR/cast.rs:52:5
177163
|
178164
LL | 100 as f32;
179165
| ^^^^^^^^^^ help: try: `100_f32`
180166

181167
error: casting integer literal to f64 is unnecessary
182-
--> $DIR/cast.rs:58:5
168+
--> $DIR/cast.rs:53:5
183169
|
184170
LL | 100 as f64;
185171
| ^^^^^^^^^^ help: try: `100_f64`
186172

187173
error: casting integer literal to f64 is unnecessary
188-
--> $DIR/cast.rs:59:5
174+
--> $DIR/cast.rs:54:5
189175
|
190176
LL | 100_i32 as f64;
191177
| ^^^^^^^^^^^^^^ help: try: `100_f64`
192178

193-
error: aborting due to 30 previous errors
179+
error: aborting due to 28 previous errors
194180

tests/ui/cast_lossless_float.fixed

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ fn main() {
2121
f64::from(x4);
2222
let x5 = 1u32;
2323
f64::from(x5);
24+
25+
// Test with casts from floating-point types
26+
f64::from(1.0f32);
2427
}
2528

2629
// The lint would suggest using `f64::from(input)` here but the `XX::from` function is not const,

tests/ui/cast_lossless_float.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ fn main() {
2121
x4 as f64;
2222
let x5 = 1u32;
2323
x5 as f64;
24+
25+
// Test with casts from floating-point types
26+
1.0f32 as f64;
2427
}
2528

2629
// The lint would suggest using `f64::from(input)` here but the `XX::from` function is not const,

tests/ui/cast_lossless_float.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,11 @@ error: casting u32 to f64 may become silently lossy if types change
6060
LL | x5 as f64;
6161
| ^^^^^^^^^ help: try: `f64::from(x5)`
6262

63-
error: aborting due to 10 previous errors
63+
error: casting f32 to f64 may become silently lossy if types change
64+
--> $DIR/cast_lossless_float.rs:26:5
65+
|
66+
LL | 1.0f32 as f64;
67+
| ^^^^^^^^^^^^^ help: try: `f64::from(1.0f32)`
68+
69+
error: aborting due to 11 previous errors
6470

tests/ui/cast_lossless_integer.fixed

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ fn main() {
2323
i64::from(1i32);
2424
i64::from(1u32);
2525
u64::from(1u32);
26+
27+
// Test with an expression wrapped in parens
28+
u16::from(1u8 + 1u8);
2629
}
2730

2831
// The lint would suggest using `f64::from(input)` here but the `XX::from` function is not const,

tests/ui/cast_lossless_integer.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ fn main() {
2323
1i32 as i64;
2424
1u32 as i64;
2525
1u32 as u64;
26+
27+
// Test with an expression wrapped in parens
28+
(1u8 + 1u8) as u16;
2629
}
2730

2831
// The lint would suggest using `f64::from(input)` here but the `XX::from` function is not const,

tests/ui/cast_lossless_integer.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,5 +108,11 @@ error: casting u32 to u64 may become silently lossy if types change
108108
LL | 1u32 as u64;
109109
| ^^^^^^^^^^^ help: try: `u64::from(1u32)`
110110

111-
error: aborting due to 18 previous errors
111+
error: casting u8 to u16 may become silently lossy if types change
112+
--> $DIR/cast_lossless_integer.rs:28:5
113+
|
114+
LL | (1u8 + 1u8) as u16;
115+
| ^^^^^^^^^^^^^^^^^^ help: try: `u16::from(1u8 + 1u8)`
116+
117+
error: aborting due to 19 previous errors
112118

0 commit comments

Comments
 (0)