Skip to content

Commit d7a98f5

Browse files
committed
also ensure generics with the same type are linted
1 parent d2de576 commit d7a98f5

File tree

3 files changed

+34
-24
lines changed

3 files changed

+34
-24
lines changed

tests/ui/unnecessary_cast.fixed

+2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ fn main() {
4848

4949
owo::<u32>([1u32].as_ptr());
5050
uwu::<u32, u8>([1u32].as_ptr());
51+
// this will not lint in the function body even though they have the same type, instead here
52+
uwu::<u32, u32>([1u32].as_ptr());
5153

5254
// macro version
5355
macro_rules! foo {

tests/ui/unnecessary_cast.rs

+2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ fn main() {
4848

4949
owo::<u32>([1u32].as_ptr()) as *const u32;
5050
uwu::<u32, u8>([1u32].as_ptr()) as *const u8;
51+
// this will not lint in the function body even though they have the same type, instead here
52+
uwu::<u32, u32>([1u32].as_ptr()) as *const u32;
5153

5254
// macro version
5355
macro_rules! foo {

tests/ui/unnecessary_cast.stderr

+30-24
Original file line numberDiff line numberDiff line change
@@ -84,143 +84,149 @@ error: casting raw pointers to the same type and constness is unnecessary (`*con
8484
LL | uwu::<u32, u8>([1u32].as_ptr()) as *const u8;
8585
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `uwu::<u32, u8>([1u32].as_ptr())`
8686

87+
error: casting raw pointers to the same type and constness is unnecessary (`*const u32` -> `*const u32`)
88+
--> $DIR/unnecessary_cast.rs:52:5
89+
|
90+
LL | uwu::<u32, u32>([1u32].as_ptr()) as *const u32;
91+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `uwu::<u32, u32>([1u32].as_ptr())`
92+
8793
error: casting integer literal to `f32` is unnecessary
88-
--> $DIR/unnecessary_cast.rs:91:9
94+
--> $DIR/unnecessary_cast.rs:93:9
8995
|
9096
LL | 100 as f32;
9197
| ^^^^^^^^^^ help: try: `100_f32`
9298

9399
error: casting integer literal to `f64` is unnecessary
94-
--> $DIR/unnecessary_cast.rs:92:9
100+
--> $DIR/unnecessary_cast.rs:94:9
95101
|
96102
LL | 100 as f64;
97103
| ^^^^^^^^^^ help: try: `100_f64`
98104

99105
error: casting integer literal to `f64` is unnecessary
100-
--> $DIR/unnecessary_cast.rs:93:9
106+
--> $DIR/unnecessary_cast.rs:95:9
101107
|
102108
LL | 100_i32 as f64;
103109
| ^^^^^^^^^^^^^^ help: try: `100_f64`
104110

105111
error: casting integer literal to `f32` is unnecessary
106-
--> $DIR/unnecessary_cast.rs:94:17
112+
--> $DIR/unnecessary_cast.rs:96:17
107113
|
108114
LL | let _ = -100 as f32;
109115
| ^^^^^^^^^^^ help: try: `-100_f32`
110116

111117
error: casting integer literal to `f64` is unnecessary
112-
--> $DIR/unnecessary_cast.rs:95:17
118+
--> $DIR/unnecessary_cast.rs:97:17
113119
|
114120
LL | let _ = -100 as f64;
115121
| ^^^^^^^^^^^ help: try: `-100_f64`
116122

117123
error: casting integer literal to `f64` is unnecessary
118-
--> $DIR/unnecessary_cast.rs:96:17
124+
--> $DIR/unnecessary_cast.rs:98:17
119125
|
120126
LL | let _ = -100_i32 as f64;
121127
| ^^^^^^^^^^^^^^^ help: try: `-100_f64`
122128

123129
error: casting float literal to `f32` is unnecessary
124-
--> $DIR/unnecessary_cast.rs:97:9
130+
--> $DIR/unnecessary_cast.rs:99:9
125131
|
126132
LL | 100. as f32;
127133
| ^^^^^^^^^^^ help: try: `100_f32`
128134

129135
error: casting float literal to `f64` is unnecessary
130-
--> $DIR/unnecessary_cast.rs:98:9
136+
--> $DIR/unnecessary_cast.rs:100:9
131137
|
132138
LL | 100. as f64;
133139
| ^^^^^^^^^^^ help: try: `100_f64`
134140

135141
error: casting integer literal to `u32` is unnecessary
136-
--> $DIR/unnecessary_cast.rs:110:9
142+
--> $DIR/unnecessary_cast.rs:112:9
137143
|
138144
LL | 1 as u32;
139145
| ^^^^^^^^ help: try: `1_u32`
140146

141147
error: casting integer literal to `i32` is unnecessary
142-
--> $DIR/unnecessary_cast.rs:111:9
148+
--> $DIR/unnecessary_cast.rs:113:9
143149
|
144150
LL | 0x10 as i32;
145151
| ^^^^^^^^^^^ help: try: `0x10_i32`
146152

147153
error: casting integer literal to `usize` is unnecessary
148-
--> $DIR/unnecessary_cast.rs:112:9
154+
--> $DIR/unnecessary_cast.rs:114:9
149155
|
150156
LL | 0b10 as usize;
151157
| ^^^^^^^^^^^^^ help: try: `0b10_usize`
152158

153159
error: casting integer literal to `u16` is unnecessary
154-
--> $DIR/unnecessary_cast.rs:113:9
160+
--> $DIR/unnecessary_cast.rs:115:9
155161
|
156162
LL | 0o73 as u16;
157163
| ^^^^^^^^^^^ help: try: `0o73_u16`
158164

159165
error: casting integer literal to `u32` is unnecessary
160-
--> $DIR/unnecessary_cast.rs:114:9
166+
--> $DIR/unnecessary_cast.rs:116:9
161167
|
162168
LL | 1_000_000_000 as u32;
163169
| ^^^^^^^^^^^^^^^^^^^^ help: try: `1_000_000_000_u32`
164170

165171
error: casting float literal to `f64` is unnecessary
166-
--> $DIR/unnecessary_cast.rs:116:9
172+
--> $DIR/unnecessary_cast.rs:118:9
167173
|
168174
LL | 1.0 as f64;
169175
| ^^^^^^^^^^ help: try: `1.0_f64`
170176

171177
error: casting float literal to `f32` is unnecessary
172-
--> $DIR/unnecessary_cast.rs:117:9
178+
--> $DIR/unnecessary_cast.rs:119:9
173179
|
174180
LL | 0.5 as f32;
175181
| ^^^^^^^^^^ help: try: `0.5_f32`
176182

177183
error: casting integer literal to `i32` is unnecessary
178-
--> $DIR/unnecessary_cast.rs:121:17
184+
--> $DIR/unnecessary_cast.rs:123:17
179185
|
180186
LL | let _ = -1 as i32;
181187
| ^^^^^^^^^ help: try: `-1_i32`
182188

183189
error: casting float literal to `f32` is unnecessary
184-
--> $DIR/unnecessary_cast.rs:122:17
190+
--> $DIR/unnecessary_cast.rs:124:17
185191
|
186192
LL | let _ = -1.0 as f32;
187193
| ^^^^^^^^^^^ help: try: `-1.0_f32`
188194

189195
error: casting to the same type is unnecessary (`i32` -> `i32`)
190-
--> $DIR/unnecessary_cast.rs:128:18
196+
--> $DIR/unnecessary_cast.rs:130:18
191197
|
192198
LL | let _ = &(x as i32);
193199
| ^^^^^^^^^^ help: try: `{ x }`
194200

195201
error: casting integer literal to `i32` is unnecessary
196-
--> $DIR/unnecessary_cast.rs:134:22
202+
--> $DIR/unnecessary_cast.rs:136:22
197203
|
198204
LL | let _: i32 = -(1) as i32;
199205
| ^^^^^^^^^^^ help: try: `-1_i32`
200206

201207
error: casting integer literal to `i64` is unnecessary
202-
--> $DIR/unnecessary_cast.rs:136:22
208+
--> $DIR/unnecessary_cast.rs:138:22
203209
|
204210
LL | let _: i64 = -(1) as i64;
205211
| ^^^^^^^^^^^ help: try: `-1_i64`
206212

207213
error: casting float literal to `f64` is unnecessary
208-
--> $DIR/unnecessary_cast.rs:143:22
214+
--> $DIR/unnecessary_cast.rs:145:22
209215
|
210216
LL | let _: f64 = (-8.0 as f64).exp();
211217
| ^^^^^^^^^^^^^ help: try: `(-8.0_f64)`
212218

213219
error: casting float literal to `f64` is unnecessary
214-
--> $DIR/unnecessary_cast.rs:145:23
220+
--> $DIR/unnecessary_cast.rs:147:23
215221
|
216222
LL | let _: f64 = -(8.0 as f64).exp(); // should suggest `-8.0_f64.exp()` here not to change code behavior
217223
| ^^^^^^^^^^^^ help: try: `8.0_f64`
218224

219225
error: casting to the same type is unnecessary (`f32` -> `f32`)
220-
--> $DIR/unnecessary_cast.rs:153:20
226+
--> $DIR/unnecessary_cast.rs:155:20
221227
|
222228
LL | let _num = foo() as f32;
223229
| ^^^^^^^^^^^^ help: try: `foo()`
224230

225-
error: aborting due to 37 previous errors
231+
error: aborting due to 38 previous errors
226232

0 commit comments

Comments
 (0)