Skip to content

Commit d2de576

Browse files
committed
add generics in test
1 parent 94e586e commit d2de576

File tree

3 files changed

+88
-38
lines changed

3 files changed

+88
-38
lines changed

tests/ui/unnecessary_cast.fixed

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//@run-rustfix
22
#![warn(clippy::unnecessary_cast)]
33
#![allow(
4-
unused_must_use,
4+
unused,
55
clippy::borrow_as_ptr,
66
clippy::no_effect,
77
clippy::nonstandard_macro_braces,
@@ -11,6 +11,14 @@
1111
type PtrConstU8 = *const u8;
1212
type PtrMutU8 = *mut u8;
1313

14+
fn owo<T>(ptr: *const T) -> *const T {
15+
ptr
16+
}
17+
18+
fn uwu<T, U>(ptr: *const T) -> *const U {
19+
ptr as *const U
20+
}
21+
1422
#[rustfmt::skip]
1523
fn main() {
1624
// Test cast_unnecessary
@@ -25,6 +33,8 @@ fn main() {
2533
1_i32;
2634
1_f32;
2735

36+
let _: *mut u8 = [1u8, 2].as_ptr() as *mut u8;
37+
2838
[1u8, 2].as_ptr();
2939
[1u8, 2].as_ptr() as *mut u8;
3040
[1u8, 2].as_mut_ptr();
@@ -36,6 +46,9 @@ fn main() {
3646
let _: *const u8 = [1u8, 2].as_ptr() as _;
3747
let _: *mut u8 = [1u8, 2].as_mut_ptr() as _;
3848

49+
owo::<u32>([1u32].as_ptr());
50+
uwu::<u32, u8>([1u32].as_ptr());
51+
3952
// macro version
4053
macro_rules! foo {
4154
($a:ident, $b:ident) => {

tests/ui/unnecessary_cast.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//@run-rustfix
22
#![warn(clippy::unnecessary_cast)]
33
#![allow(
4-
unused_must_use,
4+
unused,
55
clippy::borrow_as_ptr,
66
clippy::no_effect,
77
clippy::nonstandard_macro_braces,
@@ -11,6 +11,14 @@
1111
type PtrConstU8 = *const u8;
1212
type PtrMutU8 = *mut u8;
1313

14+
fn owo<T>(ptr: *const T) -> *const T {
15+
ptr as *const T
16+
}
17+
18+
fn uwu<T, U>(ptr: *const T) -> *const U {
19+
ptr as *const U
20+
}
21+
1422
#[rustfmt::skip]
1523
fn main() {
1624
// Test cast_unnecessary
@@ -25,6 +33,8 @@ fn main() {
2533
1_i32 as i32;
2634
1_f32 as f32;
2735

36+
let _: *mut u8 = [1u8, 2].as_ptr() as *const u8 as *mut u8;
37+
2838
[1u8, 2].as_ptr() as *const u8;
2939
[1u8, 2].as_ptr() as *mut u8;
3040
[1u8, 2].as_mut_ptr() as *mut u8;
@@ -36,6 +46,9 @@ fn main() {
3646
let _: *const u8 = [1u8, 2].as_ptr() as _;
3747
let _: *mut u8 = [1u8, 2].as_mut_ptr() as _;
3848

49+
owo::<u32>([1u32].as_ptr()) as *const u32;
50+
uwu::<u32, u8>([1u32].as_ptr()) as *const u8;
51+
3952
// macro version
4053
macro_rules! foo {
4154
($a:ident, $b:ident) => {

tests/ui/unnecessary_cast.stderr

Lines changed: 60 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,202 +1,226 @@
1+
error: casting raw pointers to the same type and constness is unnecessary (`*const T` -> `*const T`)
2+
--> $DIR/unnecessary_cast.rs:15:5
3+
|
4+
LL | ptr as *const T
5+
| ^^^^^^^^^^^^^^^ help: try: `ptr`
6+
|
7+
= note: `-D clippy::unnecessary-cast` implied by `-D warnings`
8+
19
error: casting integer literal to `i32` is unnecessary
2-
--> $DIR/unnecessary_cast.rs:17:5
10+
--> $DIR/unnecessary_cast.rs:25:5
311
|
412
LL | 1i32 as i32;
513
| ^^^^^^^^^^^ help: try: `1_i32`
6-
|
7-
= note: `-D clippy::unnecessary-cast` implied by `-D warnings`
814

915
error: casting float literal to `f32` is unnecessary
10-
--> $DIR/unnecessary_cast.rs:18:5
16+
--> $DIR/unnecessary_cast.rs:26:5
1117
|
1218
LL | 1f32 as f32;
1319
| ^^^^^^^^^^^ help: try: `1_f32`
1420

1521
error: casting to the same type is unnecessary (`bool` -> `bool`)
16-
--> $DIR/unnecessary_cast.rs:19:5
22+
--> $DIR/unnecessary_cast.rs:27:5
1723
|
1824
LL | false as bool;
1925
| ^^^^^^^^^^^^^ help: try: `false`
2026

2127
error: casting integer literal to `i32` is unnecessary
22-
--> $DIR/unnecessary_cast.rs:22:5
28+
--> $DIR/unnecessary_cast.rs:30:5
2329
|
2430
LL | -1_i32 as i32;
2531
| ^^^^^^^^^^^^^ help: try: `-1_i32`
2632

2733
error: casting integer literal to `i32` is unnecessary
28-
--> $DIR/unnecessary_cast.rs:23:5
34+
--> $DIR/unnecessary_cast.rs:31:5
2935
|
3036
LL | - 1_i32 as i32;
3137
| ^^^^^^^^^^^^^^ help: try: `- 1_i32`
3238

3339
error: casting float literal to `f32` is unnecessary
34-
--> $DIR/unnecessary_cast.rs:24:5
40+
--> $DIR/unnecessary_cast.rs:32:5
3541
|
3642
LL | -1f32 as f32;
3743
| ^^^^^^^^^^^^ help: try: `-1_f32`
3844

3945
error: casting integer literal to `i32` is unnecessary
40-
--> $DIR/unnecessary_cast.rs:25:5
46+
--> $DIR/unnecessary_cast.rs:33:5
4147
|
4248
LL | 1_i32 as i32;
4349
| ^^^^^^^^^^^^ help: try: `1_i32`
4450

4551
error: casting float literal to `f32` is unnecessary
46-
--> $DIR/unnecessary_cast.rs:26:5
52+
--> $DIR/unnecessary_cast.rs:34:5
4753
|
4854
LL | 1_f32 as f32;
4955
| ^^^^^^^^^^^^ help: try: `1_f32`
5056

5157
error: casting raw pointers to the same type and constness is unnecessary (`*const u8` -> `*const u8`)
52-
--> $DIR/unnecessary_cast.rs:28:5
58+
--> $DIR/unnecessary_cast.rs:36:22
59+
|
60+
LL | let _: *mut u8 = [1u8, 2].as_ptr() as *const u8 as *mut u8;
61+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `[1u8, 2].as_ptr()`
62+
63+
error: casting raw pointers to the same type and constness is unnecessary (`*const u8` -> `*const u8`)
64+
--> $DIR/unnecessary_cast.rs:38:5
5365
|
5466
LL | [1u8, 2].as_ptr() as *const u8;
5567
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `[1u8, 2].as_ptr()`
5668

5769
error: casting raw pointers to the same type and constness is unnecessary (`*mut u8` -> `*mut u8`)
58-
--> $DIR/unnecessary_cast.rs:30:5
70+
--> $DIR/unnecessary_cast.rs:40:5
5971
|
6072
LL | [1u8, 2].as_mut_ptr() as *mut u8;
6173
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `[1u8, 2].as_mut_ptr()`
6274

75+
error: casting raw pointers to the same type and constness is unnecessary (`*const u32` -> `*const u32`)
76+
--> $DIR/unnecessary_cast.rs:49:5
77+
|
78+
LL | owo::<u32>([1u32].as_ptr()) as *const u32;
79+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `owo::<u32>([1u32].as_ptr())`
80+
81+
error: casting raw pointers to the same type and constness is unnecessary (`*const u8` -> `*const u8`)
82+
--> $DIR/unnecessary_cast.rs:50:5
83+
|
84+
LL | uwu::<u32, u8>([1u32].as_ptr()) as *const u8;
85+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `uwu::<u32, u8>([1u32].as_ptr())`
86+
6387
error: casting integer literal to `f32` is unnecessary
64-
--> $DIR/unnecessary_cast.rs:78:9
88+
--> $DIR/unnecessary_cast.rs:91:9
6589
|
6690
LL | 100 as f32;
6791
| ^^^^^^^^^^ help: try: `100_f32`
6892

6993
error: casting integer literal to `f64` is unnecessary
70-
--> $DIR/unnecessary_cast.rs:79:9
94+
--> $DIR/unnecessary_cast.rs:92:9
7195
|
7296
LL | 100 as f64;
7397
| ^^^^^^^^^^ help: try: `100_f64`
7498

7599
error: casting integer literal to `f64` is unnecessary
76-
--> $DIR/unnecessary_cast.rs:80:9
100+
--> $DIR/unnecessary_cast.rs:93:9
77101
|
78102
LL | 100_i32 as f64;
79103
| ^^^^^^^^^^^^^^ help: try: `100_f64`
80104

81105
error: casting integer literal to `f32` is unnecessary
82-
--> $DIR/unnecessary_cast.rs:81:17
106+
--> $DIR/unnecessary_cast.rs:94:17
83107
|
84108
LL | let _ = -100 as f32;
85109
| ^^^^^^^^^^^ help: try: `-100_f32`
86110

87111
error: casting integer literal to `f64` is unnecessary
88-
--> $DIR/unnecessary_cast.rs:82:17
112+
--> $DIR/unnecessary_cast.rs:95:17
89113
|
90114
LL | let _ = -100 as f64;
91115
| ^^^^^^^^^^^ help: try: `-100_f64`
92116

93117
error: casting integer literal to `f64` is unnecessary
94-
--> $DIR/unnecessary_cast.rs:83:17
118+
--> $DIR/unnecessary_cast.rs:96:17
95119
|
96120
LL | let _ = -100_i32 as f64;
97121
| ^^^^^^^^^^^^^^^ help: try: `-100_f64`
98122

99123
error: casting float literal to `f32` is unnecessary
100-
--> $DIR/unnecessary_cast.rs:84:9
124+
--> $DIR/unnecessary_cast.rs:97:9
101125
|
102126
LL | 100. as f32;
103127
| ^^^^^^^^^^^ help: try: `100_f32`
104128

105129
error: casting float literal to `f64` is unnecessary
106-
--> $DIR/unnecessary_cast.rs:85:9
130+
--> $DIR/unnecessary_cast.rs:98:9
107131
|
108132
LL | 100. as f64;
109133
| ^^^^^^^^^^^ help: try: `100_f64`
110134

111135
error: casting integer literal to `u32` is unnecessary
112-
--> $DIR/unnecessary_cast.rs:97:9
136+
--> $DIR/unnecessary_cast.rs:110:9
113137
|
114138
LL | 1 as u32;
115139
| ^^^^^^^^ help: try: `1_u32`
116140

117141
error: casting integer literal to `i32` is unnecessary
118-
--> $DIR/unnecessary_cast.rs:98:9
142+
--> $DIR/unnecessary_cast.rs:111:9
119143
|
120144
LL | 0x10 as i32;
121145
| ^^^^^^^^^^^ help: try: `0x10_i32`
122146

123147
error: casting integer literal to `usize` is unnecessary
124-
--> $DIR/unnecessary_cast.rs:99:9
148+
--> $DIR/unnecessary_cast.rs:112:9
125149
|
126150
LL | 0b10 as usize;
127151
| ^^^^^^^^^^^^^ help: try: `0b10_usize`
128152

129153
error: casting integer literal to `u16` is unnecessary
130-
--> $DIR/unnecessary_cast.rs:100:9
154+
--> $DIR/unnecessary_cast.rs:113:9
131155
|
132156
LL | 0o73 as u16;
133157
| ^^^^^^^^^^^ help: try: `0o73_u16`
134158

135159
error: casting integer literal to `u32` is unnecessary
136-
--> $DIR/unnecessary_cast.rs:101:9
160+
--> $DIR/unnecessary_cast.rs:114:9
137161
|
138162
LL | 1_000_000_000 as u32;
139163
| ^^^^^^^^^^^^^^^^^^^^ help: try: `1_000_000_000_u32`
140164

141165
error: casting float literal to `f64` is unnecessary
142-
--> $DIR/unnecessary_cast.rs:103:9
166+
--> $DIR/unnecessary_cast.rs:116:9
143167
|
144168
LL | 1.0 as f64;
145169
| ^^^^^^^^^^ help: try: `1.0_f64`
146170

147171
error: casting float literal to `f32` is unnecessary
148-
--> $DIR/unnecessary_cast.rs:104:9
172+
--> $DIR/unnecessary_cast.rs:117:9
149173
|
150174
LL | 0.5 as f32;
151175
| ^^^^^^^^^^ help: try: `0.5_f32`
152176

153177
error: casting integer literal to `i32` is unnecessary
154-
--> $DIR/unnecessary_cast.rs:108:17
178+
--> $DIR/unnecessary_cast.rs:121:17
155179
|
156180
LL | let _ = -1 as i32;
157181
| ^^^^^^^^^ help: try: `-1_i32`
158182

159183
error: casting float literal to `f32` is unnecessary
160-
--> $DIR/unnecessary_cast.rs:109:17
184+
--> $DIR/unnecessary_cast.rs:122:17
161185
|
162186
LL | let _ = -1.0 as f32;
163187
| ^^^^^^^^^^^ help: try: `-1.0_f32`
164188

165189
error: casting to the same type is unnecessary (`i32` -> `i32`)
166-
--> $DIR/unnecessary_cast.rs:115:18
190+
--> $DIR/unnecessary_cast.rs:128:18
167191
|
168192
LL | let _ = &(x as i32);
169193
| ^^^^^^^^^^ help: try: `{ x }`
170194

171195
error: casting integer literal to `i32` is unnecessary
172-
--> $DIR/unnecessary_cast.rs:121:22
196+
--> $DIR/unnecessary_cast.rs:134:22
173197
|
174198
LL | let _: i32 = -(1) as i32;
175199
| ^^^^^^^^^^^ help: try: `-1_i32`
176200

177201
error: casting integer literal to `i64` is unnecessary
178-
--> $DIR/unnecessary_cast.rs:123:22
202+
--> $DIR/unnecessary_cast.rs:136:22
179203
|
180204
LL | let _: i64 = -(1) as i64;
181205
| ^^^^^^^^^^^ help: try: `-1_i64`
182206

183207
error: casting float literal to `f64` is unnecessary
184-
--> $DIR/unnecessary_cast.rs:130:22
208+
--> $DIR/unnecessary_cast.rs:143:22
185209
|
186210
LL | let _: f64 = (-8.0 as f64).exp();
187211
| ^^^^^^^^^^^^^ help: try: `(-8.0_f64)`
188212

189213
error: casting float literal to `f64` is unnecessary
190-
--> $DIR/unnecessary_cast.rs:132:23
214+
--> $DIR/unnecessary_cast.rs:145:23
191215
|
192216
LL | let _: f64 = -(8.0 as f64).exp(); // should suggest `-8.0_f64.exp()` here not to change code behavior
193217
| ^^^^^^^^^^^^ help: try: `8.0_f64`
194218

195219
error: casting to the same type is unnecessary (`f32` -> `f32`)
196-
--> $DIR/unnecessary_cast.rs:140:20
220+
--> $DIR/unnecessary_cast.rs:153:20
197221
|
198222
LL | let _num = foo() as f32;
199223
| ^^^^^^^^^^^^ help: try: `foo()`
200224

201-
error: aborting due to 33 previous errors
225+
error: aborting due to 37 previous errors
202226

0 commit comments

Comments
 (0)