]> git.lizzy.rs Git - rust.git/commitdiff
Auto merge of #8442 - rsmantini:issue-8120-fix, r=Manishearth
authorbors <bors@rust-lang.org>
Thu, 17 Feb 2022 22:57:23 +0000 (22:57 +0000)
committerbors <bors@rust-lang.org>
Thu, 17 Feb 2022 22:57:23 +0000 (22:57 +0000)
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`

clippy_lints/src/casts/mod.rs
tests/ui/auxiliary/macro_rules.rs
tests/ui/ptr_as_ptr.fixed
tests/ui/ptr_as_ptr.rs
tests/ui/ptr_as_ptr.stderr

index aee1e50b94a2a549d199daa74d19d7dabbfa0a69..51d47b35454c2ea97c7cb236abb5c2300586b765 100644 (file)
@@ -419,6 +419,10 @@ pub fn new(msrv: Option<RustcVersion>) -> Self {
 
 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 @@ fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
         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);
index 0251fada9e85a6ea8aa5dbfc954292835fab394c..9f283337c7e132d8d6d23de3c28ac50614d243ef 100644 (file)
@@ -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
+    };
+}
index 8346a9454f4eee45040c9bcae8905de7cd64c494..bea6be66a8e025ef2f4468b5bb41e556c4c4614c 100644 (file)
@@ -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() {
index b68d4bc0aaca1f91cc3ed1244953124fe29aaf49..ca2616b0069a07003d5d99e6505f0c86922b9e4c 100644 (file)
@@ -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() {
index 854906dc111dfec2bcd5a4dc3852ee99f5a2ec74..c58c55cfd83a15c5ac671f19f37ff4f64be212a8 100644 (file)
@@ -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