Skip to content

Commit 76f91b6

Browse files
committed
Auto merge of #8442 - rsmantini:issue-8120-fix, r=Manishearth
trigger `ptr_as_ptr` inside macros This PR makes `ptr_as_ptr` trigger inside macros Fixes issue #8120 changelog: ``[`ptr_as_ptr`]`` is now triggered inside macros r? `@llogiq`
2 parents 668b3e4 + aaeeed6 commit 76f91b6

File tree

5 files changed

+60
-9
lines changed

5 files changed

+60
-9
lines changed

clippy_lints/src/casts/mod.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,10 @@ impl_lint_pass!(Casts => [
419419

420420
impl<'tcx> LateLintPass<'tcx> for Casts {
421421
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
422+
if !in_external_macro(cx.sess(), expr.span) {
423+
ptr_as_ptr::check(cx, expr, &self.msrv);
424+
}
425+
422426
if expr.span.from_expansion() {
423427
return;
424428
}
@@ -455,7 +459,6 @@ impl<'tcx> LateLintPass<'tcx> for Casts {
455459
cast_ref_to_mut::check(cx, expr);
456460
cast_ptr_alignment::check(cx, expr);
457461
char_lit_as_u8::check(cx, expr);
458-
ptr_as_ptr::check(cx, expr, &self.msrv);
459462
}
460463

461464
extract_msrv_attr!(LateContext);

tests/ui/auxiliary/macro_rules.rs

+7
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,10 @@ macro_rules! mut_mut {
120120
let mut_mut_ty: &mut &mut u32 = &mut &mut 1u32;
121121
};
122122
}
123+
124+
#[macro_export]
125+
macro_rules! ptr_as_ptr_cast {
126+
($ptr: ident) => {
127+
$ptr as *const i32
128+
};
129+
}

tests/ui/ptr_as_ptr.fixed

+15
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
// run-rustfix
2+
// aux-build:macro_rules.rs
23

34
#![warn(clippy::ptr_as_ptr)]
45
#![feature(custom_inner_attributes)]
56

7+
extern crate macro_rules;
8+
9+
macro_rules! cast_it {
10+
($ptr: ident) => {
11+
$ptr.cast::<i32>()
12+
};
13+
}
14+
615
fn main() {
716
let ptr: *const u32 = &42_u32;
817
let mut_ptr: *mut u32 = &mut 42_u32;
@@ -28,6 +37,12 @@ fn main() {
2837
// Ensure the lint doesn't produce unnecessary turbofish for inferred types.
2938
let _: *const i32 = ptr.cast();
3039
let _: *mut i32 = mut_ptr.cast();
40+
41+
// Make sure the lint is triggered inside a macro
42+
let _ = cast_it!(ptr);
43+
44+
// Do not lint inside macros from external crates
45+
let _ = macro_rules::ptr_as_ptr_cast!(ptr);
3146
}
3247

3348
fn _msrv_1_37() {

tests/ui/ptr_as_ptr.rs

+15
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
// run-rustfix
2+
// aux-build:macro_rules.rs
23

34
#![warn(clippy::ptr_as_ptr)]
45
#![feature(custom_inner_attributes)]
56

7+
extern crate macro_rules;
8+
9+
macro_rules! cast_it {
10+
($ptr: ident) => {
11+
$ptr as *const i32
12+
};
13+
}
14+
615
fn main() {
716
let ptr: *const u32 = &42_u32;
817
let mut_ptr: *mut u32 = &mut 42_u32;
@@ -28,6 +37,12 @@ fn main() {
2837
// Ensure the lint doesn't produce unnecessary turbofish for inferred types.
2938
let _: *const i32 = ptr as *const _;
3039
let _: *mut i32 = mut_ptr as _;
40+
41+
// Make sure the lint is triggered inside a macro
42+
let _ = cast_it!(ptr);
43+
44+
// Do not lint inside macros from external crates
45+
let _ = macro_rules::ptr_as_ptr_cast!(ptr);
3146
}
3247

3348
fn _msrv_1_37() {

tests/ui/ptr_as_ptr.stderr

+19-8
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,57 @@
11
error: `as` casting between raw pointers without changing its mutability
2-
--> $DIR/ptr_as_ptr.rs:10:13
2+
--> $DIR/ptr_as_ptr.rs:19:13
33
|
44
LL | let _ = ptr as *const i32;
55
| ^^^^^^^^^^^^^^^^^ help: try `pointer::cast`, a safer alternative: `ptr.cast::<i32>()`
66
|
77
= note: `-D clippy::ptr-as-ptr` implied by `-D warnings`
88

99
error: `as` casting between raw pointers without changing its mutability
10-
--> $DIR/ptr_as_ptr.rs:11:13
10+
--> $DIR/ptr_as_ptr.rs:20:13
1111
|
1212
LL | let _ = mut_ptr as *mut i32;
1313
| ^^^^^^^^^^^^^^^^^^^ help: try `pointer::cast`, a safer alternative: `mut_ptr.cast::<i32>()`
1414

1515
error: `as` casting between raw pointers without changing its mutability
16-
--> $DIR/ptr_as_ptr.rs:16:17
16+
--> $DIR/ptr_as_ptr.rs:25:17
1717
|
1818
LL | let _ = *ptr_ptr as *const i32;
1919
| ^^^^^^^^^^^^^^^^^^^^^^ help: try `pointer::cast`, a safer alternative: `(*ptr_ptr).cast::<i32>()`
2020

2121
error: `as` casting between raw pointers without changing its mutability
22-
--> $DIR/ptr_as_ptr.rs:29:25
22+
--> $DIR/ptr_as_ptr.rs:38:25
2323
|
2424
LL | let _: *const i32 = ptr as *const _;
2525
| ^^^^^^^^^^^^^^^ help: try `pointer::cast`, a safer alternative: `ptr.cast()`
2626

2727
error: `as` casting between raw pointers without changing its mutability
28-
--> $DIR/ptr_as_ptr.rs:30:23
28+
--> $DIR/ptr_as_ptr.rs:39:23
2929
|
3030
LL | let _: *mut i32 = mut_ptr as _;
3131
| ^^^^^^^^^^^^ help: try `pointer::cast`, a safer alternative: `mut_ptr.cast()`
3232

3333
error: `as` casting between raw pointers without changing its mutability
34-
--> $DIR/ptr_as_ptr.rs:48:13
34+
--> $DIR/ptr_as_ptr.rs:11:9
35+
|
36+
LL | $ptr as *const i32
37+
| ^^^^^^^^^^^^^^^^^^ help: try `pointer::cast`, a safer alternative: `$ptr.cast::<i32>()`
38+
...
39+
LL | let _ = cast_it!(ptr);
40+
| ------------- in this macro invocation
41+
|
42+
= note: this error originates in the macro `cast_it` (in Nightly builds, run with -Z macro-backtrace for more info)
43+
44+
error: `as` casting between raw pointers without changing its mutability
45+
--> $DIR/ptr_as_ptr.rs:63:13
3546
|
3647
LL | let _ = ptr as *const i32;
3748
| ^^^^^^^^^^^^^^^^^ help: try `pointer::cast`, a safer alternative: `ptr.cast::<i32>()`
3849

3950
error: `as` casting between raw pointers without changing its mutability
40-
--> $DIR/ptr_as_ptr.rs:49:13
51+
--> $DIR/ptr_as_ptr.rs:64:13
4152
|
4253
LL | let _ = mut_ptr as *mut i32;
4354
| ^^^^^^^^^^^^^^^^^^^ help: try `pointer::cast`, a safer alternative: `mut_ptr.cast::<i32>()`
4455

45-
error: aborting due to 7 previous errors
56+
error: aborting due to 8 previous errors
4657

0 commit comments

Comments
 (0)