Skip to content

Commit 384cf37

Browse files
committed
Auto merge of #10910 - Alexendoo:unnecessary-pointer-casts, r=llogiq
Ignore more pointer types in `unnecessary_cast` Spotted this because https://github.com/rust-lang/rust-clippy/blob/e2c655b4c07c912ef749be316aeea8453cb9d840/tests/ui/suspicious_to_owned.rs#L9-L10 currently fails on `aarch64-unknown-linux-gnu` as `c_char` is `u8` there The current implementation checks for `as alias`, `as _`. This adds things like - `as *const alias` - `as *const cfg_dependant` - `as *const _` changelog: none
2 parents e2c655b + 4346c99 commit 384cf37

File tree

4 files changed

+69
-37
lines changed

4 files changed

+69
-37
lines changed

clippy_lints/src/casts/unnecessary_cast.rs

+17-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::numeric_literal::NumericLiteral;
33
use clippy_utils::source::snippet_opt;
4-
use clippy_utils::{get_parent_expr, is_ty_alias, path_to_local};
4+
use clippy_utils::{get_parent_expr, is_hir_ty_cfg_dependant, is_ty_alias, path_to_local};
55
use if_chain::if_chain;
66
use rustc_ast::{LitFloatType, LitIntType, LitKind};
77
use rustc_errors::Applicability;
@@ -27,17 +27,23 @@ pub(super) fn check<'tcx>(
2727
// check both mutability and type are the same
2828
if cast_from.kind() == cast_to.kind();
2929
if let ExprKind::Cast(_, cast_to_hir) = expr.kind;
30+
// Ignore casts to e.g. type aliases and infer types
31+
// - p as pointer_alias
32+
// - p as _
33+
if let TyKind::Ptr(to_pointee) = cast_to_hir.kind;
3034
then {
31-
if_chain! {
32-
if let TyKind::Path(qpath) = cast_to_hir.kind;
33-
if is_ty_alias(&qpath);
34-
then {
35-
return false;
36-
}
37-
}
38-
39-
if let TyKind::Infer = cast_to_hir.kind {
40-
return false;
35+
match to_pointee.ty.kind {
36+
// Ignore casts to pointers that are aliases or cfg dependant, e.g.
37+
// - p as *const std::ffi::c_char (alias)
38+
// - p as *const std::os::raw::c_char (cfg dependant)
39+
TyKind::Path(qpath) => {
40+
if is_ty_alias(&qpath) || is_hir_ty_cfg_dependant(cx, to_pointee.ty) {
41+
return false;
42+
}
43+
},
44+
// Ignore `p as *const _`
45+
TyKind::Infer => return false,
46+
_ => {},
4147
}
4248

4349
span_lint_and_sugg(

tests/ui/unnecessary_cast.fixed

+13
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ fn main() {
4545
[1u8, 2].as_mut_ptr() as PtrConstU8;
4646
let _: *const u8 = [1u8, 2].as_ptr() as _;
4747
let _: *mut u8 = [1u8, 2].as_mut_ptr() as _;
48+
let _: *const u8 = [1u8, 2].as_ptr() as *const _;
49+
let _: *mut u8 = [1u8, 2].as_mut_ptr() as *mut _;
4850

4951
owo::<u32>([1u32].as_ptr());
5052
uwu::<u32, u8>([1u32].as_ptr());
@@ -71,6 +73,17 @@ fn main() {
7173
1 as I32Alias;
7274
&1 as &I32Alias;
7375

76+
let i8_ptr: *const i8 = &1;
77+
let u8_ptr: *const u8 = &1;
78+
79+
// cfg dependant pointees
80+
i8_ptr as *const std::os::raw::c_char;
81+
u8_ptr as *const std::os::raw::c_char;
82+
83+
// type aliased pointees
84+
i8_ptr as *const std::ffi::c_char;
85+
u8_ptr as *const std::ffi::c_char;
86+
7487
// issue #9960
7588
macro_rules! bind_var {
7689
($id:ident, $e:expr) => {{

tests/ui/unnecessary_cast.rs

+13
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ fn main() {
4545
[1u8, 2].as_mut_ptr() as PtrConstU8;
4646
let _: *const u8 = [1u8, 2].as_ptr() as _;
4747
let _: *mut u8 = [1u8, 2].as_mut_ptr() as _;
48+
let _: *const u8 = [1u8, 2].as_ptr() as *const _;
49+
let _: *mut u8 = [1u8, 2].as_mut_ptr() as *mut _;
4850

4951
owo::<u32>([1u32].as_ptr()) as *const u32;
5052
uwu::<u32, u8>([1u32].as_ptr()) as *const u8;
@@ -71,6 +73,17 @@ fn main() {
7173
1 as I32Alias;
7274
&1 as &I32Alias;
7375

76+
let i8_ptr: *const i8 = &1;
77+
let u8_ptr: *const u8 = &1;
78+
79+
// cfg dependant pointees
80+
i8_ptr as *const std::os::raw::c_char;
81+
u8_ptr as *const std::os::raw::c_char;
82+
83+
// type aliased pointees
84+
i8_ptr as *const std::ffi::c_char;
85+
u8_ptr as *const std::ffi::c_char;
86+
7487
// issue #9960
7588
macro_rules! bind_var {
7689
($id:ident, $e:expr) => {{

tests/ui/unnecessary_cast.stderr

+26-26
Original file line numberDiff line numberDiff line change
@@ -73,157 +73,157 @@ 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:49:5
76+
--> $DIR/unnecessary_cast.rs:51: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:50:5
82+
--> $DIR/unnecessary_cast.rs:52: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:52:5
88+
--> $DIR/unnecessary_cast.rs:54:5
8989
|
9090
LL | uwu::<u32, u32>([1u32].as_ptr()) as *const u32;
9191
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `uwu::<u32, u32>([1u32].as_ptr())`
9292

9393
error: casting integer literal to `f32` is unnecessary
94-
--> $DIR/unnecessary_cast.rs:93:9
94+
--> $DIR/unnecessary_cast.rs:106:9
9595
|
9696
LL | 100 as f32;
9797
| ^^^^^^^^^^ help: try: `100_f32`
9898

9999
error: casting integer literal to `f64` is unnecessary
100-
--> $DIR/unnecessary_cast.rs:94:9
100+
--> $DIR/unnecessary_cast.rs:107:9
101101
|
102102
LL | 100 as f64;
103103
| ^^^^^^^^^^ help: try: `100_f64`
104104

105105
error: casting integer literal to `f64` is unnecessary
106-
--> $DIR/unnecessary_cast.rs:95:9
106+
--> $DIR/unnecessary_cast.rs:108:9
107107
|
108108
LL | 100_i32 as f64;
109109
| ^^^^^^^^^^^^^^ help: try: `100_f64`
110110

111111
error: casting integer literal to `f32` is unnecessary
112-
--> $DIR/unnecessary_cast.rs:96:17
112+
--> $DIR/unnecessary_cast.rs:109:17
113113
|
114114
LL | let _ = -100 as f32;
115115
| ^^^^^^^^^^^ help: try: `-100_f32`
116116

117117
error: casting integer literal to `f64` is unnecessary
118-
--> $DIR/unnecessary_cast.rs:97:17
118+
--> $DIR/unnecessary_cast.rs:110:17
119119
|
120120
LL | let _ = -100 as f64;
121121
| ^^^^^^^^^^^ help: try: `-100_f64`
122122

123123
error: casting integer literal to `f64` is unnecessary
124-
--> $DIR/unnecessary_cast.rs:98:17
124+
--> $DIR/unnecessary_cast.rs:111:17
125125
|
126126
LL | let _ = -100_i32 as f64;
127127
| ^^^^^^^^^^^^^^^ help: try: `-100_f64`
128128

129129
error: casting float literal to `f32` is unnecessary
130-
--> $DIR/unnecessary_cast.rs:99:9
130+
--> $DIR/unnecessary_cast.rs:112:9
131131
|
132132
LL | 100. as f32;
133133
| ^^^^^^^^^^^ help: try: `100_f32`
134134

135135
error: casting float literal to `f64` is unnecessary
136-
--> $DIR/unnecessary_cast.rs:100:9
136+
--> $DIR/unnecessary_cast.rs:113:9
137137
|
138138
LL | 100. as f64;
139139
| ^^^^^^^^^^^ help: try: `100_f64`
140140

141141
error: casting integer literal to `u32` is unnecessary
142-
--> $DIR/unnecessary_cast.rs:112:9
142+
--> $DIR/unnecessary_cast.rs:125:9
143143
|
144144
LL | 1 as u32;
145145
| ^^^^^^^^ help: try: `1_u32`
146146

147147
error: casting integer literal to `i32` is unnecessary
148-
--> $DIR/unnecessary_cast.rs:113:9
148+
--> $DIR/unnecessary_cast.rs:126:9
149149
|
150150
LL | 0x10 as i32;
151151
| ^^^^^^^^^^^ help: try: `0x10_i32`
152152

153153
error: casting integer literal to `usize` is unnecessary
154-
--> $DIR/unnecessary_cast.rs:114:9
154+
--> $DIR/unnecessary_cast.rs:127:9
155155
|
156156
LL | 0b10 as usize;
157157
| ^^^^^^^^^^^^^ help: try: `0b10_usize`
158158

159159
error: casting integer literal to `u16` is unnecessary
160-
--> $DIR/unnecessary_cast.rs:115:9
160+
--> $DIR/unnecessary_cast.rs:128:9
161161
|
162162
LL | 0o73 as u16;
163163
| ^^^^^^^^^^^ help: try: `0o73_u16`
164164

165165
error: casting integer literal to `u32` is unnecessary
166-
--> $DIR/unnecessary_cast.rs:116:9
166+
--> $DIR/unnecessary_cast.rs:129:9
167167
|
168168
LL | 1_000_000_000 as u32;
169169
| ^^^^^^^^^^^^^^^^^^^^ help: try: `1_000_000_000_u32`
170170

171171
error: casting float literal to `f64` is unnecessary
172-
--> $DIR/unnecessary_cast.rs:118:9
172+
--> $DIR/unnecessary_cast.rs:131:9
173173
|
174174
LL | 1.0 as f64;
175175
| ^^^^^^^^^^ help: try: `1.0_f64`
176176

177177
error: casting float literal to `f32` is unnecessary
178-
--> $DIR/unnecessary_cast.rs:119:9
178+
--> $DIR/unnecessary_cast.rs:132:9
179179
|
180180
LL | 0.5 as f32;
181181
| ^^^^^^^^^^ help: try: `0.5_f32`
182182

183183
error: casting integer literal to `i32` is unnecessary
184-
--> $DIR/unnecessary_cast.rs:123:17
184+
--> $DIR/unnecessary_cast.rs:136:17
185185
|
186186
LL | let _ = -1 as i32;
187187
| ^^^^^^^^^ help: try: `-1_i32`
188188

189189
error: casting float literal to `f32` is unnecessary
190-
--> $DIR/unnecessary_cast.rs:124:17
190+
--> $DIR/unnecessary_cast.rs:137:17
191191
|
192192
LL | let _ = -1.0 as f32;
193193
| ^^^^^^^^^^^ help: try: `-1.0_f32`
194194

195195
error: casting to the same type is unnecessary (`i32` -> `i32`)
196-
--> $DIR/unnecessary_cast.rs:130:18
196+
--> $DIR/unnecessary_cast.rs:143:18
197197
|
198198
LL | let _ = &(x as i32);
199199
| ^^^^^^^^^^ help: try: `{ x }`
200200

201201
error: casting integer literal to `i32` is unnecessary
202-
--> $DIR/unnecessary_cast.rs:136:22
202+
--> $DIR/unnecessary_cast.rs:149:22
203203
|
204204
LL | let _: i32 = -(1) as i32;
205205
| ^^^^^^^^^^^ help: try: `-1_i32`
206206

207207
error: casting integer literal to `i64` is unnecessary
208-
--> $DIR/unnecessary_cast.rs:138:22
208+
--> $DIR/unnecessary_cast.rs:151:22
209209
|
210210
LL | let _: i64 = -(1) as i64;
211211
| ^^^^^^^^^^^ help: try: `-1_i64`
212212

213213
error: casting float literal to `f64` is unnecessary
214-
--> $DIR/unnecessary_cast.rs:145:22
214+
--> $DIR/unnecessary_cast.rs:158:22
215215
|
216216
LL | let _: f64 = (-8.0 as f64).exp();
217217
| ^^^^^^^^^^^^^ help: try: `(-8.0_f64)`
218218

219219
error: casting float literal to `f64` is unnecessary
220-
--> $DIR/unnecessary_cast.rs:147:23
220+
--> $DIR/unnecessary_cast.rs:160:23
221221
|
222222
LL | let _: f64 = -(8.0 as f64).exp(); // should suggest `-8.0_f64.exp()` here not to change code behavior
223223
| ^^^^^^^^^^^^ help: try: `8.0_f64`
224224

225225
error: casting to the same type is unnecessary (`f32` -> `f32`)
226-
--> $DIR/unnecessary_cast.rs:155:20
226+
--> $DIR/unnecessary_cast.rs:168:20
227227
|
228228
LL | let _num = foo() as f32;
229229
| ^^^^^^^^^^^^ help: try: `foo()`

0 commit comments

Comments
 (0)