about summary refs log tree commit diff
diff options
context:
space:
mode:
authorAlex Macleod <alex@macleod.io>2023-06-08 18:08:06 +0000
committerAlex Macleod <alex@macleod.io>2023-06-08 18:19:35 +0000
commit4346c992cb555b79b68265535f5720033f2bdca7 (patch)
tree1dd2fbcef60d552f9b89b5d13473d3cd05179e0b
parentb7c330fc7834fde83a804ff85c39c8a11833c1cc (diff)
downloadrust-4346c992cb555b79b68265535f5720033f2bdca7.tar.gz
rust-4346c992cb555b79b68265535f5720033f2bdca7.zip
Ignore more pointer types in `unnecessary_cast`
-rw-r--r--clippy_lints/src/casts/unnecessary_cast.rs28
-rw-r--r--tests/ui/unnecessary_cast.fixed13
-rw-r--r--tests/ui/unnecessary_cast.rs13
-rw-r--r--tests/ui/unnecessary_cast.stderr52
4 files changed, 69 insertions, 37 deletions
diff --git a/clippy_lints/src/casts/unnecessary_cast.rs b/clippy_lints/src/casts/unnecessary_cast.rs
index 08ed8a63e1a..d3d80ff4100 100644
--- a/clippy_lints/src/casts/unnecessary_cast.rs
+++ b/clippy_lints/src/casts/unnecessary_cast.rs
@@ -1,7 +1,7 @@
 use clippy_utils::diagnostics::span_lint_and_sugg;
 use clippy_utils::numeric_literal::NumericLiteral;
 use clippy_utils::source::snippet_opt;
-use clippy_utils::{get_parent_expr, is_ty_alias, path_to_local};
+use clippy_utils::{get_parent_expr, is_hir_ty_cfg_dependant, is_ty_alias, path_to_local};
 use if_chain::if_chain;
 use rustc_ast::{LitFloatType, LitIntType, LitKind};
 use rustc_errors::Applicability;
@@ -27,17 +27,23 @@ pub(super) fn check<'tcx>(
         // check both mutability and type are the same
         if cast_from.kind() == cast_to.kind();
         if let ExprKind::Cast(_, cast_to_hir) = expr.kind;
+        // Ignore casts to e.g. type aliases and infer types
+        // - p as pointer_alias
+        // - p as _
+        if let TyKind::Ptr(to_pointee) = cast_to_hir.kind;
         then {
-            if_chain! {
-                if let TyKind::Path(qpath) = cast_to_hir.kind;
-                if is_ty_alias(&qpath);
-                then {
-                    return false;
-                }
-            }
-
-            if let TyKind::Infer = cast_to_hir.kind {
-                return false;
+            match to_pointee.ty.kind {
+                // Ignore casts to pointers that are aliases or cfg dependant, e.g.
+                // - p as *const std::ffi::c_char (alias)
+                // - p as *const std::os::raw::c_char (cfg dependant)
+                TyKind::Path(qpath) => {
+                    if is_ty_alias(&qpath) || is_hir_ty_cfg_dependant(cx, to_pointee.ty) {
+                        return false;
+                    }
+                },
+                // Ignore `p as *const _`
+                TyKind::Infer => return false,
+                _ => {},
             }
 
             span_lint_and_sugg(
diff --git a/tests/ui/unnecessary_cast.fixed b/tests/ui/unnecessary_cast.fixed
index 0fd49e7c815..e74476044d9 100644
--- a/tests/ui/unnecessary_cast.fixed
+++ b/tests/ui/unnecessary_cast.fixed
@@ -45,6 +45,8 @@ fn main() {
     [1u8, 2].as_mut_ptr() as PtrConstU8;
     let _: *const u8 = [1u8, 2].as_ptr() as _;
     let _: *mut u8 = [1u8, 2].as_mut_ptr() as _;
+    let _: *const u8 = [1u8, 2].as_ptr() as *const _;
+    let _: *mut u8 = [1u8, 2].as_mut_ptr() as *mut _;
 
     owo::<u32>([1u32].as_ptr());
     uwu::<u32, u8>([1u32].as_ptr());
@@ -71,6 +73,17 @@ fn main() {
     1 as I32Alias;
     &1 as &I32Alias;
 
+    let i8_ptr: *const i8 = &1;
+    let u8_ptr: *const u8 = &1;
+
+    // cfg dependant pointees
+    i8_ptr as *const std::os::raw::c_char;
+    u8_ptr as *const std::os::raw::c_char;
+
+    // type aliased pointees
+    i8_ptr as *const std::ffi::c_char;
+    u8_ptr as *const std::ffi::c_char;
+
     // issue #9960
     macro_rules! bind_var {
         ($id:ident, $e:expr) => {{
diff --git a/tests/ui/unnecessary_cast.rs b/tests/ui/unnecessary_cast.rs
index 27669492329..a577fd79899 100644
--- a/tests/ui/unnecessary_cast.rs
+++ b/tests/ui/unnecessary_cast.rs
@@ -45,6 +45,8 @@ fn main() {
     [1u8, 2].as_mut_ptr() as PtrConstU8;
     let _: *const u8 = [1u8, 2].as_ptr() as _;
     let _: *mut u8 = [1u8, 2].as_mut_ptr() as _;
+    let _: *const u8 = [1u8, 2].as_ptr() as *const _;
+    let _: *mut u8 = [1u8, 2].as_mut_ptr() as *mut _;
 
     owo::<u32>([1u32].as_ptr()) as *const u32;
     uwu::<u32, u8>([1u32].as_ptr()) as *const u8;
@@ -71,6 +73,17 @@ fn main() {
     1 as I32Alias;
     &1 as &I32Alias;
 
+    let i8_ptr: *const i8 = &1;
+    let u8_ptr: *const u8 = &1;
+
+    // cfg dependant pointees
+    i8_ptr as *const std::os::raw::c_char;
+    u8_ptr as *const std::os::raw::c_char;
+
+    // type aliased pointees
+    i8_ptr as *const std::ffi::c_char;
+    u8_ptr as *const std::ffi::c_char;
+
     // issue #9960
     macro_rules! bind_var {
         ($id:ident, $e:expr) => {{
diff --git a/tests/ui/unnecessary_cast.stderr b/tests/ui/unnecessary_cast.stderr
index 0e511fb4c7a..7f748fcf4c2 100644
--- a/tests/ui/unnecessary_cast.stderr
+++ b/tests/ui/unnecessary_cast.stderr
@@ -73,157 +73,157 @@ LL |     [1u8, 2].as_mut_ptr() as *mut u8;
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `[1u8, 2].as_mut_ptr()`
 
 error: casting raw pointers to the same type and constness is unnecessary (`*const u32` -> `*const u32`)
-  --> $DIR/unnecessary_cast.rs:49:5
+  --> $DIR/unnecessary_cast.rs:51:5
    |
 LL |     owo::<u32>([1u32].as_ptr()) as *const u32;
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `owo::<u32>([1u32].as_ptr())`
 
 error: casting raw pointers to the same type and constness is unnecessary (`*const u8` -> `*const u8`)
-  --> $DIR/unnecessary_cast.rs:50:5
+  --> $DIR/unnecessary_cast.rs:52:5
    |
 LL |     uwu::<u32, u8>([1u32].as_ptr()) as *const u8;
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `uwu::<u32, u8>([1u32].as_ptr())`
 
 error: casting raw pointers to the same type and constness is unnecessary (`*const u32` -> `*const u32`)
-  --> $DIR/unnecessary_cast.rs:52:5
+  --> $DIR/unnecessary_cast.rs:54:5
    |
 LL |     uwu::<u32, u32>([1u32].as_ptr()) as *const u32;
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `uwu::<u32, u32>([1u32].as_ptr())`
 
 error: casting integer literal to `f32` is unnecessary
-  --> $DIR/unnecessary_cast.rs:93:9
+  --> $DIR/unnecessary_cast.rs:106:9
    |
 LL |         100 as f32;
    |         ^^^^^^^^^^ help: try: `100_f32`
 
 error: casting integer literal to `f64` is unnecessary
-  --> $DIR/unnecessary_cast.rs:94:9
+  --> $DIR/unnecessary_cast.rs:107:9
    |
 LL |         100 as f64;
    |         ^^^^^^^^^^ help: try: `100_f64`
 
 error: casting integer literal to `f64` is unnecessary
-  --> $DIR/unnecessary_cast.rs:95:9
+  --> $DIR/unnecessary_cast.rs:108:9
    |
 LL |         100_i32 as f64;
    |         ^^^^^^^^^^^^^^ help: try: `100_f64`
 
 error: casting integer literal to `f32` is unnecessary
-  --> $DIR/unnecessary_cast.rs:96:17
+  --> $DIR/unnecessary_cast.rs:109:17
    |
 LL |         let _ = -100 as f32;
    |                 ^^^^^^^^^^^ help: try: `-100_f32`
 
 error: casting integer literal to `f64` is unnecessary
-  --> $DIR/unnecessary_cast.rs:97:17
+  --> $DIR/unnecessary_cast.rs:110:17
    |
 LL |         let _ = -100 as f64;
    |                 ^^^^^^^^^^^ help: try: `-100_f64`
 
 error: casting integer literal to `f64` is unnecessary
-  --> $DIR/unnecessary_cast.rs:98:17
+  --> $DIR/unnecessary_cast.rs:111:17
    |
 LL |         let _ = -100_i32 as f64;
    |                 ^^^^^^^^^^^^^^^ help: try: `-100_f64`
 
 error: casting float literal to `f32` is unnecessary
-  --> $DIR/unnecessary_cast.rs:99:9
+  --> $DIR/unnecessary_cast.rs:112:9
    |
 LL |         100. as f32;
    |         ^^^^^^^^^^^ help: try: `100_f32`
 
 error: casting float literal to `f64` is unnecessary
-  --> $DIR/unnecessary_cast.rs:100:9
+  --> $DIR/unnecessary_cast.rs:113:9
    |
 LL |         100. as f64;
    |         ^^^^^^^^^^^ help: try: `100_f64`
 
 error: casting integer literal to `u32` is unnecessary
-  --> $DIR/unnecessary_cast.rs:112:9
+  --> $DIR/unnecessary_cast.rs:125:9
    |
 LL |         1 as u32;
    |         ^^^^^^^^ help: try: `1_u32`
 
 error: casting integer literal to `i32` is unnecessary
-  --> $DIR/unnecessary_cast.rs:113:9
+  --> $DIR/unnecessary_cast.rs:126:9
    |
 LL |         0x10 as i32;
    |         ^^^^^^^^^^^ help: try: `0x10_i32`
 
 error: casting integer literal to `usize` is unnecessary
-  --> $DIR/unnecessary_cast.rs:114:9
+  --> $DIR/unnecessary_cast.rs:127:9
    |
 LL |         0b10 as usize;
    |         ^^^^^^^^^^^^^ help: try: `0b10_usize`
 
 error: casting integer literal to `u16` is unnecessary
-  --> $DIR/unnecessary_cast.rs:115:9
+  --> $DIR/unnecessary_cast.rs:128:9
    |
 LL |         0o73 as u16;
    |         ^^^^^^^^^^^ help: try: `0o73_u16`
 
 error: casting integer literal to `u32` is unnecessary
-  --> $DIR/unnecessary_cast.rs:116:9
+  --> $DIR/unnecessary_cast.rs:129:9
    |
 LL |         1_000_000_000 as u32;
    |         ^^^^^^^^^^^^^^^^^^^^ help: try: `1_000_000_000_u32`
 
 error: casting float literal to `f64` is unnecessary
-  --> $DIR/unnecessary_cast.rs:118:9
+  --> $DIR/unnecessary_cast.rs:131:9
    |
 LL |         1.0 as f64;
    |         ^^^^^^^^^^ help: try: `1.0_f64`
 
 error: casting float literal to `f32` is unnecessary
-  --> $DIR/unnecessary_cast.rs:119:9
+  --> $DIR/unnecessary_cast.rs:132:9
    |
 LL |         0.5 as f32;
    |         ^^^^^^^^^^ help: try: `0.5_f32`
 
 error: casting integer literal to `i32` is unnecessary
-  --> $DIR/unnecessary_cast.rs:123:17
+  --> $DIR/unnecessary_cast.rs:136:17
    |
 LL |         let _ = -1 as i32;
    |                 ^^^^^^^^^ help: try: `-1_i32`
 
 error: casting float literal to `f32` is unnecessary
-  --> $DIR/unnecessary_cast.rs:124:17
+  --> $DIR/unnecessary_cast.rs:137:17
    |
 LL |         let _ = -1.0 as f32;
    |                 ^^^^^^^^^^^ help: try: `-1.0_f32`
 
 error: casting to the same type is unnecessary (`i32` -> `i32`)
-  --> $DIR/unnecessary_cast.rs:130:18
+  --> $DIR/unnecessary_cast.rs:143:18
    |
 LL |         let _ = &(x as i32);
    |                  ^^^^^^^^^^ help: try: `{ x }`
 
 error: casting integer literal to `i32` is unnecessary
-  --> $DIR/unnecessary_cast.rs:136:22
+  --> $DIR/unnecessary_cast.rs:149:22
    |
 LL |         let _: i32 = -(1) as i32;
    |                      ^^^^^^^^^^^ help: try: `-1_i32`
 
 error: casting integer literal to `i64` is unnecessary
-  --> $DIR/unnecessary_cast.rs:138:22
+  --> $DIR/unnecessary_cast.rs:151:22
    |
 LL |         let _: i64 = -(1) as i64;
    |                      ^^^^^^^^^^^ help: try: `-1_i64`
 
 error: casting float literal to `f64` is unnecessary
-  --> $DIR/unnecessary_cast.rs:145:22
+  --> $DIR/unnecessary_cast.rs:158:22
    |
 LL |         let _: f64 = (-8.0 as f64).exp();
    |                      ^^^^^^^^^^^^^ help: try: `(-8.0_f64)`
 
 error: casting float literal to `f64` is unnecessary
-  --> $DIR/unnecessary_cast.rs:147:23
+  --> $DIR/unnecessary_cast.rs:160:23
    |
 LL |         let _: f64 = -(8.0 as f64).exp(); // should suggest `-8.0_f64.exp()` here not to change code behavior
    |                       ^^^^^^^^^^^^ help: try: `8.0_f64`
 
 error: casting to the same type is unnecessary (`f32` -> `f32`)
-  --> $DIR/unnecessary_cast.rs:155:20
+  --> $DIR/unnecessary_cast.rs:168:20
    |
 LL |         let _num = foo() as f32;
    |                    ^^^^^^^^^^^^ help: try: `foo()`