Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

trigger ptr_as_ptr inside macros #8442

Merged
merged 1 commit into from
Feb 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion clippy_lints/src/casts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,10 @@ impl_lint_pass!(Casts => [

impl<'tcx> LateLintPass<'tcx> for Casts {
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
if !in_external_macro(cx.sess(), expr.span) {
ptr_as_ptr::check(cx, expr, &self.msrv);
}

if expr.span.from_expansion() {
return;
}
Expand Down Expand Up @@ -455,7 +459,6 @@ impl<'tcx> LateLintPass<'tcx> for Casts {
cast_ref_to_mut::check(cx, expr);
cast_ptr_alignment::check(cx, expr);
char_lit_as_u8::check(cx, expr);
ptr_as_ptr::check(cx, expr, &self.msrv);
}

extract_msrv_attr!(LateContext);
Expand Down
7 changes: 7 additions & 0 deletions tests/ui/auxiliary/macro_rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,10 @@ macro_rules! mut_mut {
let mut_mut_ty: &mut &mut u32 = &mut &mut 1u32;
};
}

#[macro_export]
macro_rules! ptr_as_ptr_cast {
($ptr: ident) => {
$ptr as *const i32
};
}
15 changes: 15 additions & 0 deletions tests/ui/ptr_as_ptr.fixed
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
// run-rustfix
// aux-build:macro_rules.rs

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

extern crate macro_rules;

macro_rules! cast_it {
($ptr: ident) => {
$ptr.cast::<i32>()
};
}

fn main() {
let ptr: *const u32 = &42_u32;
let mut_ptr: *mut u32 = &mut 42_u32;
Expand All @@ -28,6 +37,12 @@ fn main() {
// Ensure the lint doesn't produce unnecessary turbofish for inferred types.
let _: *const i32 = ptr.cast();
let _: *mut i32 = mut_ptr.cast();

// Make sure the lint is triggered inside a macro
let _ = cast_it!(ptr);

// Do not lint inside macros from external crates
let _ = macro_rules::ptr_as_ptr_cast!(ptr);
}

fn _msrv_1_37() {
Expand Down
15 changes: 15 additions & 0 deletions tests/ui/ptr_as_ptr.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
// run-rustfix
// aux-build:macro_rules.rs

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

extern crate macro_rules;

macro_rules! cast_it {
($ptr: ident) => {
$ptr as *const i32
};
}

fn main() {
let ptr: *const u32 = &42_u32;
let mut_ptr: *mut u32 = &mut 42_u32;
Expand All @@ -28,6 +37,12 @@ fn main() {
// Ensure the lint doesn't produce unnecessary turbofish for inferred types.
let _: *const i32 = ptr as *const _;
let _: *mut i32 = mut_ptr as _;

// Make sure the lint is triggered inside a macro
let _ = cast_it!(ptr);

// Do not lint inside macros from external crates
let _ = macro_rules::ptr_as_ptr_cast!(ptr);
}

fn _msrv_1_37() {
Expand Down
27 changes: 19 additions & 8 deletions tests/ui/ptr_as_ptr.stderr
Original file line number Diff line number Diff line change
@@ -1,46 +1,57 @@
error: `as` casting between raw pointers without changing its mutability
--> $DIR/ptr_as_ptr.rs:10:13
--> $DIR/ptr_as_ptr.rs:19:13
|
LL | let _ = ptr as *const i32;
| ^^^^^^^^^^^^^^^^^ help: try `pointer::cast`, a safer alternative: `ptr.cast::<i32>()`
|
= note: `-D clippy::ptr-as-ptr` implied by `-D warnings`

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

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

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

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

error: `as` casting between raw pointers without changing its mutability
--> $DIR/ptr_as_ptr.rs:48:13
--> $DIR/ptr_as_ptr.rs:11:9
|
LL | $ptr as *const i32
| ^^^^^^^^^^^^^^^^^^ help: try `pointer::cast`, a safer alternative: `$ptr.cast::<i32>()`
...
LL | let _ = cast_it!(ptr);
| ------------- in this macro invocation
|
= note: this error originates in the macro `cast_it` (in Nightly builds, run with -Z macro-backtrace for more info)

error: `as` casting between raw pointers without changing its mutability
--> $DIR/ptr_as_ptr.rs:63:13
|
LL | let _ = ptr as *const i32;
| ^^^^^^^^^^^^^^^^^ help: try `pointer::cast`, a safer alternative: `ptr.cast::<i32>()`

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

error: aborting due to 7 previous errors
error: aborting due to 8 previous errors