Skip to content

Commit 9570ed8

Browse files
committed
ptr_as_ptr: don't allow in proc-macros
notice that this stops `inline!` from working as well
1 parent e19a11f commit 9570ed8

File tree

4 files changed

+40
-42
lines changed

4 files changed

+40
-42
lines changed

clippy_lints/src/casts/ptr_as_ptr.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
2+
use clippy_utils::is_from_proc_macro;
23
use clippy_utils::msrvs::{self, Msrv};
34
use clippy_utils::source::snippet_with_applicability;
45
use clippy_utils::sugg::Sugg;
@@ -25,7 +26,7 @@ impl OmitFollowedCastReason<'_> {
2526
}
2627
}
2728

28-
pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, msrv: Msrv) {
29+
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>, msrv: Msrv) {
2930
if let ExprKind::Cast(cast_expr, cast_to_hir_ty) = expr.kind
3031
&& let (cast_from, cast_to) = (cx.typeck_results().expr_ty(cast_expr), cx.typeck_results().expr_ty(expr))
3132
&& let ty::RawPtr(_, from_mutbl) = cast_from.kind()
@@ -36,6 +37,7 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, msrv: Msrv) {
3637
// as explained here: https://github.com/rust-lang/rust/issues/60602.
3738
&& to_pointee_ty.is_sized(cx.tcx, cx.typing_env())
3839
&& msrv.meets(cx, msrvs::POINTER_CAST)
40+
&& !is_from_proc_macro(cx, expr)
3941
{
4042
let mut app = Applicability::MachineApplicable;
4143
let turbofish = match &cast_to_hir_ty.kind {

tests/ui/ptr_as_ptr.fixed

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#![warn(clippy::ptr_as_ptr)]
44

55
extern crate proc_macros;
6-
use proc_macros::{external, inline_macros};
6+
use proc_macros::{external, inline_macros, with_span};
77

88
mod issue_11278_a {
99
#[derive(Debug)]
@@ -53,11 +53,13 @@ fn main() {
5353
//~^ ptr_as_ptr
5454

5555
// Make sure the lint is triggered inside a macro
56-
let _ = inline!($ptr.cast::<i32>());
57-
//~^ ptr_as_ptr
56+
// FIXME: `is_from_proc_macro` incorrectly stops the lint from firing here
57+
let _ = inline!($ptr as *const i32);
5858

5959
// Do not lint inside macros from external crates
6060
let _ = external!($ptr as *const i32);
61+
62+
let _ = with_span!(expr $ptr as *const i32);
6163
}
6264

6365
#[clippy::msrv = "1.37"]

tests/ui/ptr_as_ptr.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#![warn(clippy::ptr_as_ptr)]
44

55
extern crate proc_macros;
6-
use proc_macros::{external, inline_macros};
6+
use proc_macros::{external, inline_macros, with_span};
77

88
mod issue_11278_a {
99
#[derive(Debug)]
@@ -53,11 +53,13 @@ fn main() {
5353
//~^ ptr_as_ptr
5454

5555
// Make sure the lint is triggered inside a macro
56+
// FIXME: `is_from_proc_macro` incorrectly stops the lint from firing here
5657
let _ = inline!($ptr as *const i32);
57-
//~^ ptr_as_ptr
5858

5959
// Do not lint inside macros from external crates
6060
let _ = external!($ptr as *const i32);
61+
62+
let _ = with_span!(expr $ptr as *const i32);
6163
}
6264

6365
#[clippy::msrv = "1.37"]

tests/ui/ptr_as_ptr.stderr

Lines changed: 28 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -38,174 +38,166 @@ LL | let _: *mut i32 = mut_ptr as _;
3838
| ^^^^^^^^^^^^ help: try `pointer::cast`, a safer alternative: `mut_ptr.cast()`
3939

4040
error: `as` casting between raw pointers without changing their constness
41-
--> tests/ui/ptr_as_ptr.rs:56:21
42-
|
43-
LL | let _ = inline!($ptr as *const i32);
44-
| ^^^^^^^^^^^^^^^^^^ help: try `pointer::cast`, a safer alternative: `$ptr.cast::<i32>()`
45-
|
46-
= note: this error originates in the macro `__inline_mac_fn_main` (in Nightly builds, run with -Z macro-backtrace for more info)
47-
48-
error: `as` casting between raw pointers without changing their constness
49-
--> tests/ui/ptr_as_ptr.rs:78:13
41+
--> tests/ui/ptr_as_ptr.rs:80:13
5042
|
5143
LL | let _ = ptr as *const i32;
5244
| ^^^^^^^^^^^^^^^^^ help: try `pointer::cast`, a safer alternative: `ptr.cast::<i32>()`
5345

5446
error: `as` casting between raw pointers without changing their constness
55-
--> tests/ui/ptr_as_ptr.rs:80:13
47+
--> tests/ui/ptr_as_ptr.rs:82:13
5648
|
5749
LL | let _ = mut_ptr as *mut i32;
5850
| ^^^^^^^^^^^^^^^^^^^ help: try `pointer::cast`, a safer alternative: `mut_ptr.cast::<i32>()`
5951

6052
error: `as` casting between raw pointers without changing their constness
61-
--> tests/ui/ptr_as_ptr.rs:88:9
53+
--> tests/ui/ptr_as_ptr.rs:90:9
6254
|
6355
LL | ptr::null_mut() as *mut u32
6456
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try call directly: `ptr::null_mut::<u32>()`
6557

6658
error: `as` casting between raw pointers without changing their constness
67-
--> tests/ui/ptr_as_ptr.rs:93:9
59+
--> tests/ui/ptr_as_ptr.rs:95:9
6860
|
6961
LL | std::ptr::null_mut() as *mut u32
7062
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try call directly: `std::ptr::null_mut::<u32>()`
7163

7264
error: `as` casting between raw pointers without changing their constness
73-
--> tests/ui/ptr_as_ptr.rs:99:9
65+
--> tests/ui/ptr_as_ptr.rs:101:9
7466
|
7567
LL | ptr::null_mut() as *mut u32
7668
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try call directly: `ptr::null_mut::<u32>()`
7769

7870
error: `as` casting between raw pointers without changing their constness
79-
--> tests/ui/ptr_as_ptr.rs:104:9
71+
--> tests/ui/ptr_as_ptr.rs:106:9
8072
|
8173
LL | core::ptr::null_mut() as *mut u32
8274
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try call directly: `core::ptr::null_mut::<u32>()`
8375

8476
error: `as` casting between raw pointers without changing their constness
85-
--> tests/ui/ptr_as_ptr.rs:110:9
77+
--> tests/ui/ptr_as_ptr.rs:112:9
8678
|
8779
LL | ptr::null() as *const u32
8880
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: try call directly: `ptr::null::<u32>()`
8981

9082
error: `as` casting between raw pointers without changing their constness
91-
--> tests/ui/ptr_as_ptr.rs:115:9
83+
--> tests/ui/ptr_as_ptr.rs:117:9
9284
|
9385
LL | std::ptr::null() as *const u32
9486
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try call directly: `std::ptr::null::<u32>()`
9587

9688
error: `as` casting between raw pointers without changing their constness
97-
--> tests/ui/ptr_as_ptr.rs:121:9
89+
--> tests/ui/ptr_as_ptr.rs:123:9
9890
|
9991
LL | ptr::null() as *const u32
10092
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: try call directly: `ptr::null::<u32>()`
10193

10294
error: `as` casting between raw pointers without changing their constness
103-
--> tests/ui/ptr_as_ptr.rs:126:9
95+
--> tests/ui/ptr_as_ptr.rs:128:9
10496
|
10597
LL | core::ptr::null() as *const u32
10698
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try call directly: `core::ptr::null::<u32>()`
10799

108100
error: `as` casting between raw pointers without changing their constness
109-
--> tests/ui/ptr_as_ptr.rs:134:9
101+
--> tests/ui/ptr_as_ptr.rs:136:9
110102
|
111103
LL | ptr::null_mut() as *mut _
112104
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: try call directly: `ptr::null_mut()`
113105

114106
error: `as` casting between raw pointers without changing their constness
115-
--> tests/ui/ptr_as_ptr.rs:139:9
107+
--> tests/ui/ptr_as_ptr.rs:141:9
116108
|
117109
LL | std::ptr::null_mut() as *mut _
118110
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try call directly: `std::ptr::null_mut()`
119111

120112
error: `as` casting between raw pointers without changing their constness
121-
--> tests/ui/ptr_as_ptr.rs:145:9
113+
--> tests/ui/ptr_as_ptr.rs:147:9
122114
|
123115
LL | ptr::null_mut() as *mut _
124116
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: try call directly: `ptr::null_mut()`
125117

126118
error: `as` casting between raw pointers without changing their constness
127-
--> tests/ui/ptr_as_ptr.rs:150:9
119+
--> tests/ui/ptr_as_ptr.rs:152:9
128120
|
129121
LL | core::ptr::null_mut() as *mut _
130122
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try call directly: `core::ptr::null_mut()`
131123

132124
error: `as` casting between raw pointers without changing their constness
133-
--> tests/ui/ptr_as_ptr.rs:156:9
125+
--> tests/ui/ptr_as_ptr.rs:158:9
134126
|
135127
LL | ptr::null() as *const _
136128
| ^^^^^^^^^^^^^^^^^^^^^^^ help: try call directly: `ptr::null()`
137129

138130
error: `as` casting between raw pointers without changing their constness
139-
--> tests/ui/ptr_as_ptr.rs:161:9
131+
--> tests/ui/ptr_as_ptr.rs:163:9
140132
|
141133
LL | std::ptr::null() as *const _
142134
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try call directly: `std::ptr::null()`
143135

144136
error: `as` casting between raw pointers without changing their constness
145-
--> tests/ui/ptr_as_ptr.rs:167:9
137+
--> tests/ui/ptr_as_ptr.rs:169:9
146138
|
147139
LL | ptr::null() as *const _
148140
| ^^^^^^^^^^^^^^^^^^^^^^^ help: try call directly: `ptr::null()`
149141

150142
error: `as` casting between raw pointers without changing their constness
151-
--> tests/ui/ptr_as_ptr.rs:172:9
143+
--> tests/ui/ptr_as_ptr.rs:174:9
152144
|
153145
LL | core::ptr::null() as *const _
154146
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try call directly: `core::ptr::null()`
155147

156148
error: `as` casting between raw pointers without changing their constness
157-
--> tests/ui/ptr_as_ptr.rs:180:9
149+
--> tests/ui/ptr_as_ptr.rs:182:9
158150
|
159151
LL | ptr::null_mut() as _
160152
| ^^^^^^^^^^^^^^^^^^^^ help: try call directly: `ptr::null_mut()`
161153

162154
error: `as` casting between raw pointers without changing their constness
163-
--> tests/ui/ptr_as_ptr.rs:185:9
155+
--> tests/ui/ptr_as_ptr.rs:187:9
164156
|
165157
LL | std::ptr::null_mut() as _
166158
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: try call directly: `std::ptr::null_mut()`
167159

168160
error: `as` casting between raw pointers without changing their constness
169-
--> tests/ui/ptr_as_ptr.rs:191:9
161+
--> tests/ui/ptr_as_ptr.rs:193:9
170162
|
171163
LL | ptr::null_mut() as _
172164
| ^^^^^^^^^^^^^^^^^^^^ help: try call directly: `ptr::null_mut()`
173165

174166
error: `as` casting between raw pointers without changing their constness
175-
--> tests/ui/ptr_as_ptr.rs:196:9
167+
--> tests/ui/ptr_as_ptr.rs:198:9
176168
|
177169
LL | core::ptr::null_mut() as _
178170
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try call directly: `core::ptr::null_mut()`
179171

180172
error: `as` casting between raw pointers without changing their constness
181-
--> tests/ui/ptr_as_ptr.rs:202:9
173+
--> tests/ui/ptr_as_ptr.rs:204:9
182174
|
183175
LL | ptr::null() as _
184176
| ^^^^^^^^^^^^^^^^ help: try call directly: `ptr::null()`
185177

186178
error: `as` casting between raw pointers without changing their constness
187-
--> tests/ui/ptr_as_ptr.rs:207:9
179+
--> tests/ui/ptr_as_ptr.rs:209:9
188180
|
189181
LL | std::ptr::null() as _
190182
| ^^^^^^^^^^^^^^^^^^^^^ help: try call directly: `std::ptr::null()`
191183

192184
error: `as` casting between raw pointers without changing their constness
193-
--> tests/ui/ptr_as_ptr.rs:213:9
185+
--> tests/ui/ptr_as_ptr.rs:215:9
194186
|
195187
LL | ptr::null() as _
196188
| ^^^^^^^^^^^^^^^^ help: try call directly: `ptr::null()`
197189

198190
error: `as` casting between raw pointers without changing their constness
199-
--> tests/ui/ptr_as_ptr.rs:218:9
191+
--> tests/ui/ptr_as_ptr.rs:220:9
200192
|
201193
LL | core::ptr::null() as _
202194
| ^^^^^^^^^^^^^^^^^^^^^^ help: try call directly: `core::ptr::null()`
203195

204196
error: `as` casting between raw pointers without changing their constness
205-
--> tests/ui/ptr_as_ptr.rs:226:43
197+
--> tests/ui/ptr_as_ptr.rs:228:43
206198
|
207199
LL | let _: fn() = std::mem::transmute(std::ptr::null::<()>() as *const u8);
208200
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try call directly: `std::ptr::null::<u8>()`
209201

210-
error: aborting due to 34 previous errors
202+
error: aborting due to 33 previous errors
211203

0 commit comments

Comments
 (0)