about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2022-02-17 22:57:23 +0000
committerbors <bors@rust-lang.org>2022-02-17 22:57:23 +0000
commit76f91b6f5a538f7b0389a5e7cc8cb8170a6d5117 (patch)
tree02d5c4841e199c06bee6d2474bfcc41130641af3
parent668b3e47f90cf38d9d7cba88d991a165769024a5 (diff)
parentaaeeed6a598c098c222104dd07996ab17d78aa54 (diff)
downloadrust-76f91b6f5a538f7b0389a5e7cc8cb8170a6d5117.tar.gz
rust-76f91b6f5a538f7b0389a5e7cc8cb8170a6d5117.zip
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`
-rw-r--r--clippy_lints/src/casts/mod.rs5
-rw-r--r--tests/ui/auxiliary/macro_rules.rs7
-rw-r--r--tests/ui/ptr_as_ptr.fixed15
-rw-r--r--tests/ui/ptr_as_ptr.rs15
-rw-r--r--tests/ui/ptr_as_ptr.stderr27
5 files changed, 60 insertions, 9 deletions
diff --git a/clippy_lints/src/casts/mod.rs b/clippy_lints/src/casts/mod.rs
index aee1e50b94a..51d47b35454 100644
--- a/clippy_lints/src/casts/mod.rs
+++ b/clippy_lints/src/casts/mod.rs
@@ -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;
         }
@@ -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);
diff --git a/tests/ui/auxiliary/macro_rules.rs b/tests/ui/auxiliary/macro_rules.rs
index 0251fada9e8..9f283337c7e 100644
--- a/tests/ui/auxiliary/macro_rules.rs
+++ b/tests/ui/auxiliary/macro_rules.rs
@@ -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
+    };
+}
diff --git a/tests/ui/ptr_as_ptr.fixed b/tests/ui/ptr_as_ptr.fixed
index 8346a9454f4..bea6be66a8e 100644
--- a/tests/ui/ptr_as_ptr.fixed
+++ b/tests/ui/ptr_as_ptr.fixed
@@ -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;
@@ -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() {
diff --git a/tests/ui/ptr_as_ptr.rs b/tests/ui/ptr_as_ptr.rs
index b68d4bc0aac..ca2616b0069 100644
--- a/tests/ui/ptr_as_ptr.rs
+++ b/tests/ui/ptr_as_ptr.rs
@@ -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;
@@ -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() {
diff --git a/tests/ui/ptr_as_ptr.stderr b/tests/ui/ptr_as_ptr.stderr
index 854906dc111..c58c55cfd83 100644
--- a/tests/ui/ptr_as_ptr.stderr
+++ b/tests/ui/ptr_as_ptr.stderr
@@ -1,5 +1,5 @@
 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>()`
@@ -7,40 +7,51 @@ LL |     let _ = ptr as *const 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