Skip to content

Commit dcfc6a2

Browse files
committed
check for fully qualified paths in unnecessary_cast
1 parent 3dd6af9 commit dcfc6a2

File tree

4 files changed

+93
-40
lines changed

4 files changed

+93
-40
lines changed

clippy_lints/src/casts/unnecessary_cast.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -254,13 +254,20 @@ fn is_cast_from_ty_alias<'tcx>(cx: &LateContext<'tcx>, expr: impl Visitable<'tcx
254254
// function's declaration snippet is exactly equal to the `Ty`. That way, we can
255255
// see whether it's a type alias.
256256
//
257-
// Will this work for more complex types? Probably not!
257+
// FIXME: This won't work if the type is given an alias through `use`, should we
258+
// consider this a type alias as well?
258259
if !snippet
259260
.split("->")
260261
.skip(1)
261262
.map(|s| {
262263
s.trim() == cast_from.to_string()
263-
|| s.split("where").any(|ty| ty.trim() == cast_from.to_string())
264+
|| s.trim().contains(&format!("::{cast_from}"))
265+
|| s.split("where").any(|ty| {
266+
ty.trim() == cast_from.to_string()
267+
|| ty.trim() == cast_from.to_string()
268+
// Fully qualified path, or something silly like `::u32`
269+
|| s.trim().contains(&format!("::{cast_from}"))
270+
})
264271
})
265272
.any(|a| a)
266273
{

tests/ui/unnecessary_cast.fixed

+17
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,16 @@ mod fake_libc {
3838
}
3939
}
4040

41+
fn aaa() -> ::std::primitive::u32 {
42+
0
43+
}
44+
45+
use std::primitive::u32 as UnsignedThirtyTwoBitInteger;
46+
47+
fn bbb() -> UnsignedThirtyTwoBitInteger {
48+
0
49+
}
50+
4151
#[rustfmt::skip]
4252
fn main() {
4353
// Test cast_unnecessary
@@ -105,6 +115,13 @@ fn main() {
105115
extern_fake_libc::getpid_SAFE_TRUTH() as i32;
106116
let pid = unsafe { fake_libc::getpid() };
107117
pid as i32;
118+
aaa();
119+
let x = aaa();
120+
aaa();
121+
// Will not lint currently.
122+
bbb() as u32;
123+
let x = bbb();
124+
bbb() as u32;
108125

109126
let i8_ptr: *const i8 = &1;
110127
let u8_ptr: *const u8 = &1;

tests/ui/unnecessary_cast.rs

+17
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,16 @@ mod fake_libc {
3838
}
3939
}
4040

41+
fn aaa() -> ::std::primitive::u32 {
42+
0
43+
}
44+
45+
use std::primitive::u32 as UnsignedThirtyTwoBitInteger;
46+
47+
fn bbb() -> UnsignedThirtyTwoBitInteger {
48+
0
49+
}
50+
4151
#[rustfmt::skip]
4252
fn main() {
4353
// Test cast_unnecessary
@@ -105,6 +115,13 @@ fn main() {
105115
extern_fake_libc::getpid_SAFE_TRUTH() as i32;
106116
let pid = unsafe { fake_libc::getpid() };
107117
pid as i32;
118+
aaa() as u32;
119+
let x = aaa();
120+
aaa() as u32;
121+
// Will not lint currently.
122+
bbb() as u32;
123+
let x = bbb();
124+
bbb() as u32;
108125

109126
let i8_ptr: *const i8 = &1;
110127
let u8_ptr: *const u8 = &1;

tests/ui/unnecessary_cast.stderr

+50-38
Original file line numberDiff line numberDiff line change
@@ -7,226 +7,238 @@ LL | ptr as *const T
77
= note: `-D clippy::unnecessary-cast` implied by `-D warnings`
88

99
error: casting integer literal to `i32` is unnecessary
10-
--> $DIR/unnecessary_cast.rs:44:5
10+
--> $DIR/unnecessary_cast.rs:54:5
1111
|
1212
LL | 1i32 as i32;
1313
| ^^^^^^^^^^^ help: try: `1_i32`
1414

1515
error: casting float literal to `f32` is unnecessary
16-
--> $DIR/unnecessary_cast.rs:45:5
16+
--> $DIR/unnecessary_cast.rs:55:5
1717
|
1818
LL | 1f32 as f32;
1919
| ^^^^^^^^^^^ help: try: `1_f32`
2020

2121
error: casting to the same type is unnecessary (`bool` -> `bool`)
22-
--> $DIR/unnecessary_cast.rs:46:5
22+
--> $DIR/unnecessary_cast.rs:56:5
2323
|
2424
LL | false as bool;
2525
| ^^^^^^^^^^^^^ help: try: `false`
2626

2727
error: casting integer literal to `i32` is unnecessary
28-
--> $DIR/unnecessary_cast.rs:49:5
28+
--> $DIR/unnecessary_cast.rs:59:5
2929
|
3030
LL | -1_i32 as i32;
3131
| ^^^^^^^^^^^^^ help: try: `-1_i32`
3232

3333
error: casting integer literal to `i32` is unnecessary
34-
--> $DIR/unnecessary_cast.rs:50:5
34+
--> $DIR/unnecessary_cast.rs:60:5
3535
|
3636
LL | - 1_i32 as i32;
3737
| ^^^^^^^^^^^^^^ help: try: `- 1_i32`
3838

3939
error: casting float literal to `f32` is unnecessary
40-
--> $DIR/unnecessary_cast.rs:51:5
40+
--> $DIR/unnecessary_cast.rs:61:5
4141
|
4242
LL | -1f32 as f32;
4343
| ^^^^^^^^^^^^ help: try: `-1_f32`
4444

4545
error: casting integer literal to `i32` is unnecessary
46-
--> $DIR/unnecessary_cast.rs:52:5
46+
--> $DIR/unnecessary_cast.rs:62:5
4747
|
4848
LL | 1_i32 as i32;
4949
| ^^^^^^^^^^^^ help: try: `1_i32`
5050

5151
error: casting float literal to `f32` is unnecessary
52-
--> $DIR/unnecessary_cast.rs:53:5
52+
--> $DIR/unnecessary_cast.rs:63:5
5353
|
5454
LL | 1_f32 as f32;
5555
| ^^^^^^^^^^^^ help: try: `1_f32`
5656

5757
error: casting raw pointers to the same type and constness is unnecessary (`*const u8` -> `*const u8`)
58-
--> $DIR/unnecessary_cast.rs:55:22
58+
--> $DIR/unnecessary_cast.rs:65:22
5959
|
6060
LL | let _: *mut u8 = [1u8, 2].as_ptr() as *const u8 as *mut u8;
6161
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `[1u8, 2].as_ptr()`
6262

6363
error: casting raw pointers to the same type and constness is unnecessary (`*const u8` -> `*const u8`)
64-
--> $DIR/unnecessary_cast.rs:57:5
64+
--> $DIR/unnecessary_cast.rs:67:5
6565
|
6666
LL | [1u8, 2].as_ptr() as *const u8;
6767
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `[1u8, 2].as_ptr()`
6868

6969
error: casting raw pointers to the same type and constness is unnecessary (`*mut u8` -> `*mut u8`)
70-
--> $DIR/unnecessary_cast.rs:59:5
70+
--> $DIR/unnecessary_cast.rs:69:5
7171
|
7272
LL | [1u8, 2].as_mut_ptr() as *mut u8;
7373
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `[1u8, 2].as_mut_ptr()`
7474

7575
error: casting raw pointers to the same type and constness is unnecessary (`*const u32` -> `*const u32`)
76-
--> $DIR/unnecessary_cast.rs:70:5
76+
--> $DIR/unnecessary_cast.rs:80:5
7777
|
7878
LL | owo::<u32>([1u32].as_ptr()) as *const u32;
7979
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `owo::<u32>([1u32].as_ptr())`
8080

8181
error: casting raw pointers to the same type and constness is unnecessary (`*const u8` -> `*const u8`)
82-
--> $DIR/unnecessary_cast.rs:71:5
82+
--> $DIR/unnecessary_cast.rs:81:5
8383
|
8484
LL | uwu::<u32, u8>([1u32].as_ptr()) as *const u8;
8585
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `uwu::<u32, u8>([1u32].as_ptr())`
8686

8787
error: casting raw pointers to the same type and constness is unnecessary (`*const u32` -> `*const u32`)
88-
--> $DIR/unnecessary_cast.rs:73:5
88+
--> $DIR/unnecessary_cast.rs:83:5
8989
|
9090
LL | uwu::<u32, u32>([1u32].as_ptr()) as *const u32;
9191
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `uwu::<u32, u32>([1u32].as_ptr())`
9292

93+
error: casting to the same type is unnecessary (`u32` -> `u32`)
94+
--> $DIR/unnecessary_cast.rs:118:5
95+
|
96+
LL | aaa() as u32;
97+
| ^^^^^^^^^^^^ help: try: `aaa()`
98+
99+
error: casting to the same type is unnecessary (`u32` -> `u32`)
100+
--> $DIR/unnecessary_cast.rs:120:5
101+
|
102+
LL | aaa() as u32;
103+
| ^^^^^^^^^^^^ help: try: `aaa()`
104+
93105
error: casting integer literal to `f32` is unnecessary
94-
--> $DIR/unnecessary_cast.rs:139:9
106+
--> $DIR/unnecessary_cast.rs:156:9
95107
|
96108
LL | 100 as f32;
97109
| ^^^^^^^^^^ help: try: `100_f32`
98110

99111
error: casting integer literal to `f64` is unnecessary
100-
--> $DIR/unnecessary_cast.rs:140:9
112+
--> $DIR/unnecessary_cast.rs:157:9
101113
|
102114
LL | 100 as f64;
103115
| ^^^^^^^^^^ help: try: `100_f64`
104116

105117
error: casting integer literal to `f64` is unnecessary
106-
--> $DIR/unnecessary_cast.rs:141:9
118+
--> $DIR/unnecessary_cast.rs:158:9
107119
|
108120
LL | 100_i32 as f64;
109121
| ^^^^^^^^^^^^^^ help: try: `100_f64`
110122

111123
error: casting integer literal to `f32` is unnecessary
112-
--> $DIR/unnecessary_cast.rs:142:17
124+
--> $DIR/unnecessary_cast.rs:159:17
113125
|
114126
LL | let _ = -100 as f32;
115127
| ^^^^^^^^^^^ help: try: `-100_f32`
116128

117129
error: casting integer literal to `f64` is unnecessary
118-
--> $DIR/unnecessary_cast.rs:143:17
130+
--> $DIR/unnecessary_cast.rs:160:17
119131
|
120132
LL | let _ = -100 as f64;
121133
| ^^^^^^^^^^^ help: try: `-100_f64`
122134

123135
error: casting integer literal to `f64` is unnecessary
124-
--> $DIR/unnecessary_cast.rs:144:17
136+
--> $DIR/unnecessary_cast.rs:161:17
125137
|
126138
LL | let _ = -100_i32 as f64;
127139
| ^^^^^^^^^^^^^^^ help: try: `-100_f64`
128140

129141
error: casting float literal to `f32` is unnecessary
130-
--> $DIR/unnecessary_cast.rs:145:9
142+
--> $DIR/unnecessary_cast.rs:162:9
131143
|
132144
LL | 100. as f32;
133145
| ^^^^^^^^^^^ help: try: `100_f32`
134146

135147
error: casting float literal to `f64` is unnecessary
136-
--> $DIR/unnecessary_cast.rs:146:9
148+
--> $DIR/unnecessary_cast.rs:163:9
137149
|
138150
LL | 100. as f64;
139151
| ^^^^^^^^^^^ help: try: `100_f64`
140152

141153
error: casting integer literal to `u32` is unnecessary
142-
--> $DIR/unnecessary_cast.rs:158:9
154+
--> $DIR/unnecessary_cast.rs:175:9
143155
|
144156
LL | 1 as u32;
145157
| ^^^^^^^^ help: try: `1_u32`
146158

147159
error: casting integer literal to `i32` is unnecessary
148-
--> $DIR/unnecessary_cast.rs:159:9
160+
--> $DIR/unnecessary_cast.rs:176:9
149161
|
150162
LL | 0x10 as i32;
151163
| ^^^^^^^^^^^ help: try: `0x10_i32`
152164

153165
error: casting integer literal to `usize` is unnecessary
154-
--> $DIR/unnecessary_cast.rs:160:9
166+
--> $DIR/unnecessary_cast.rs:177:9
155167
|
156168
LL | 0b10 as usize;
157169
| ^^^^^^^^^^^^^ help: try: `0b10_usize`
158170

159171
error: casting integer literal to `u16` is unnecessary
160-
--> $DIR/unnecessary_cast.rs:161:9
172+
--> $DIR/unnecessary_cast.rs:178:9
161173
|
162174
LL | 0o73 as u16;
163175
| ^^^^^^^^^^^ help: try: `0o73_u16`
164176

165177
error: casting integer literal to `u32` is unnecessary
166-
--> $DIR/unnecessary_cast.rs:162:9
178+
--> $DIR/unnecessary_cast.rs:179:9
167179
|
168180
LL | 1_000_000_000 as u32;
169181
| ^^^^^^^^^^^^^^^^^^^^ help: try: `1_000_000_000_u32`
170182

171183
error: casting float literal to `f64` is unnecessary
172-
--> $DIR/unnecessary_cast.rs:164:9
184+
--> $DIR/unnecessary_cast.rs:181:9
173185
|
174186
LL | 1.0 as f64;
175187
| ^^^^^^^^^^ help: try: `1.0_f64`
176188

177189
error: casting float literal to `f32` is unnecessary
178-
--> $DIR/unnecessary_cast.rs:165:9
190+
--> $DIR/unnecessary_cast.rs:182:9
179191
|
180192
LL | 0.5 as f32;
181193
| ^^^^^^^^^^ help: try: `0.5_f32`
182194

183195
error: casting integer literal to `i32` is unnecessary
184-
--> $DIR/unnecessary_cast.rs:169:17
196+
--> $DIR/unnecessary_cast.rs:186:17
185197
|
186198
LL | let _ = -1 as i32;
187199
| ^^^^^^^^^ help: try: `-1_i32`
188200

189201
error: casting float literal to `f32` is unnecessary
190-
--> $DIR/unnecessary_cast.rs:170:17
202+
--> $DIR/unnecessary_cast.rs:187:17
191203
|
192204
LL | let _ = -1.0 as f32;
193205
| ^^^^^^^^^^^ help: try: `-1.0_f32`
194206

195207
error: casting to the same type is unnecessary (`i32` -> `i32`)
196-
--> $DIR/unnecessary_cast.rs:176:18
208+
--> $DIR/unnecessary_cast.rs:193:18
197209
|
198210
LL | let _ = &(x as i32);
199211
| ^^^^^^^^^^ help: try: `{ x }`
200212

201213
error: casting integer literal to `i32` is unnecessary
202-
--> $DIR/unnecessary_cast.rs:182:22
214+
--> $DIR/unnecessary_cast.rs:199:22
203215
|
204216
LL | let _: i32 = -(1) as i32;
205217
| ^^^^^^^^^^^ help: try: `-1_i32`
206218

207219
error: casting integer literal to `i64` is unnecessary
208-
--> $DIR/unnecessary_cast.rs:184:22
220+
--> $DIR/unnecessary_cast.rs:201:22
209221
|
210222
LL | let _: i64 = -(1) as i64;
211223
| ^^^^^^^^^^^ help: try: `-1_i64`
212224

213225
error: casting float literal to `f64` is unnecessary
214-
--> $DIR/unnecessary_cast.rs:191:22
226+
--> $DIR/unnecessary_cast.rs:208:22
215227
|
216228
LL | let _: f64 = (-8.0 as f64).exp();
217229
| ^^^^^^^^^^^^^ help: try: `(-8.0_f64)`
218230

219231
error: casting float literal to `f64` is unnecessary
220-
--> $DIR/unnecessary_cast.rs:193:23
232+
--> $DIR/unnecessary_cast.rs:210:23
221233
|
222234
LL | let _: f64 = -(8.0 as f64).exp(); // should suggest `-8.0_f64.exp()` here not to change code behavior
223235
| ^^^^^^^^^^^^ help: try: `8.0_f64`
224236

225237
error: casting to the same type is unnecessary (`f32` -> `f32`)
226-
--> $DIR/unnecessary_cast.rs:201:20
238+
--> $DIR/unnecessary_cast.rs:218:20
227239
|
228240
LL | let _num = foo() as f32;
229241
| ^^^^^^^^^^^^ help: try: `foo()`
230242

231-
error: aborting due to 38 previous errors
243+
error: aborting due to 40 previous errors
232244

0 commit comments

Comments
 (0)