about summary refs log tree commit diff
path: root/tests/ui/lint
diff options
context:
space:
mode:
authorUrgau <urgau@numericable.fr>2023-07-12 16:17:58 +0200
committerUrgau <urgau@numericable.fr>2023-08-01 19:35:31 +0200
commit743ae5a2eb5be808704dccb97a013add2edcca20 (patch)
treec8e2d2e3a22035d12ebadfb05d216dd02280bddb /tests/ui/lint
parentc435af0d5c47a333c7cb7f359f3586d5c9dab3ab (diff)
downloadrust-743ae5a2eb5be808704dccb97a013add2edcca20.tar.gz
rust-743ae5a2eb5be808704dccb97a013add2edcca20.zip
Expand incorrect_fn_null_check lint with reference null checking
Diffstat (limited to 'tests/ui/lint')
-rw-r--r--tests/ui/lint/fn_null_check.rs21
-rw-r--r--tests/ui/lint/fn_null_check.stderr82
2 files changed, 94 insertions, 9 deletions
diff --git a/tests/ui/lint/fn_null_check.rs b/tests/ui/lint/fn_null_check.rs
index 7f01f2c4283..87b120fc131 100644
--- a/tests/ui/lint/fn_null_check.rs
+++ b/tests/ui/lint/fn_null_check.rs
@@ -3,6 +3,7 @@
 fn main() {
     let fn_ptr = main;
 
+    // ------------- Function pointers ---------------
     if (fn_ptr as *mut ()).is_null() {}
     //~^ WARN function pointers are not nullable
     if (fn_ptr as *const u8).is_null() {}
@@ -20,6 +21,26 @@ fn main() {
     if (fn_ptr as fn() as *const ()).is_null() {}
     //~^ WARN function pointers are not nullable
 
+    // ---------------- References ------------------
+    if (&mut 8 as *mut i32).is_null() {}
+    //~^ WARN references are not nullable
+    if (&8 as *const i32).is_null() {}
+    //~^ WARN references are not nullable
+    if (&8 as *const i32) == std::ptr::null() {}
+    //~^ WARN references are not nullable
+    let ref_num = &8;
+    if (ref_num as *const i32) == std::ptr::null() {}
+    //~^ WARN references are not nullable
+    if (b"\0" as *const u8).is_null() {}
+    //~^ WARN references are not nullable
+    if ("aa" as *const str).is_null() {}
+    //~^ WARN references are not nullable
+    if (&[1, 2] as *const i32).is_null() {}
+    //~^ WARN references are not nullable
+    if (&mut [1, 2] as *mut i32) == std::ptr::null_mut() {}
+    //~^ WARN references are not nullable
+
+    // ----------------------------------------------
     const ZPTR: *const () = 0 as *const _;
     const NOT_ZPTR: *const () = 1 as *const _;
 
diff --git a/tests/ui/lint/fn_null_check.stderr b/tests/ui/lint/fn_null_check.stderr
index 0398c0da50f..7a08c5bda2b 100644
--- a/tests/ui/lint/fn_null_check.stderr
+++ b/tests/ui/lint/fn_null_check.stderr
@@ -1,5 +1,5 @@
 warning: function pointers are not nullable, so checking them for null will always return false
-  --> $DIR/fn_null_check.rs:6:8
+  --> $DIR/fn_null_check.rs:7:8
    |
 LL |     if (fn_ptr as *mut ()).is_null() {}
    |        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -8,7 +8,7 @@ LL |     if (fn_ptr as *mut ()).is_null() {}
    = note: `#[warn(incorrect_fn_null_checks)]` on by default
 
 warning: function pointers are not nullable, so checking them for null will always return false
-  --> $DIR/fn_null_check.rs:8:8
+  --> $DIR/fn_null_check.rs:9:8
    |
 LL |     if (fn_ptr as *const u8).is_null() {}
    |        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -16,7 +16,7 @@ LL |     if (fn_ptr as *const u8).is_null() {}
    = help: wrap the function pointer inside an `Option` and use `Option::is_none` to check for null pointer value
 
 warning: function pointers are not nullable, so checking them for null will always return false
-  --> $DIR/fn_null_check.rs:10:8
+  --> $DIR/fn_null_check.rs:11:8
    |
 LL |     if (fn_ptr as *const ()) == std::ptr::null() {}
    |        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -24,7 +24,7 @@ LL |     if (fn_ptr as *const ()) == std::ptr::null() {}
    = help: wrap the function pointer inside an `Option` and use `Option::is_none` to check for null pointer value
 
 warning: function pointers are not nullable, so checking them for null will always return false
-  --> $DIR/fn_null_check.rs:12:8
+  --> $DIR/fn_null_check.rs:13:8
    |
 LL |     if (fn_ptr as *mut ()) == std::ptr::null_mut() {}
    |        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -32,7 +32,7 @@ LL |     if (fn_ptr as *mut ()) == std::ptr::null_mut() {}
    = help: wrap the function pointer inside an `Option` and use `Option::is_none` to check for null pointer value
 
 warning: function pointers are not nullable, so checking them for null will always return false
-  --> $DIR/fn_null_check.rs:14:8
+  --> $DIR/fn_null_check.rs:15:8
    |
 LL |     if (fn_ptr as *const ()) == (0 as *const ()) {}
    |        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -40,7 +40,7 @@ LL |     if (fn_ptr as *const ()) == (0 as *const ()) {}
    = help: wrap the function pointer inside an `Option` and use `Option::is_none` to check for null pointer value
 
 warning: function pointers are not nullable, so checking them for null will always return false
-  --> $DIR/fn_null_check.rs:16:8
+  --> $DIR/fn_null_check.rs:17:8
    |
 LL |     if <*const _>::is_null(fn_ptr as *const ()) {}
    |        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -48,7 +48,7 @@ LL |     if <*const _>::is_null(fn_ptr as *const ()) {}
    = help: wrap the function pointer inside an `Option` and use `Option::is_none` to check for null pointer value
 
 warning: function pointers are not nullable, so checking them for null will always return false
-  --> $DIR/fn_null_check.rs:18:8
+  --> $DIR/fn_null_check.rs:19:8
    |
 LL |     if (fn_ptr as *mut fn() as *const fn() as *const ()).is_null() {}
    |        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -56,12 +56,76 @@ LL |     if (fn_ptr as *mut fn() as *const fn() as *const ()).is_null() {}
    = help: wrap the function pointer inside an `Option` and use `Option::is_none` to check for null pointer value
 
 warning: function pointers are not nullable, so checking them for null will always return false
-  --> $DIR/fn_null_check.rs:20:8
+  --> $DIR/fn_null_check.rs:21:8
    |
 LL |     if (fn_ptr as fn() as *const ()).is_null() {}
    |        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
    = help: wrap the function pointer inside an `Option` and use `Option::is_none` to check for null pointer value
 
-warning: 8 warnings emitted
+warning: references are not nullable, so checking them for null will always return false
+  --> $DIR/fn_null_check.rs:25:8
+   |
+LL |     if (&mut 8 as *mut i32).is_null() {}
+   |        ^------^^^^^^^^^^^^^^^^^^^^^^^
+   |         |
+   |         expression has type `&mut i32`
+
+warning: references are not nullable, so checking them for null will always return false
+  --> $DIR/fn_null_check.rs:27:8
+   |
+LL |     if (&8 as *const i32).is_null() {}
+   |        ^--^^^^^^^^^^^^^^^^^^^^^^^^^
+   |         |
+   |         expression has type `&i32`
+
+warning: references are not nullable, so checking them for null will always return false
+  --> $DIR/fn_null_check.rs:29:8
+   |
+LL |     if (&8 as *const i32) == std::ptr::null() {}
+   |        ^--^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+   |         |
+   |         expression has type `&i32`
+
+warning: references are not nullable, so checking them for null will always return false
+  --> $DIR/fn_null_check.rs:32:8
+   |
+LL |     if (ref_num as *const i32) == std::ptr::null() {}
+   |        ^-------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+   |         |
+   |         expression has type `&i32`
+
+warning: references are not nullable, so checking them for null will always return false
+  --> $DIR/fn_null_check.rs:34:8
+   |
+LL |     if (b"\0" as *const u8).is_null() {}
+   |        ^-----^^^^^^^^^^^^^^^^^^^^^^^^
+   |         |
+   |         expression has type `&[u8; 1]`
+
+warning: references are not nullable, so checking them for null will always return false
+  --> $DIR/fn_null_check.rs:36:8
+   |
+LL |     if ("aa" as *const str).is_null() {}
+   |        ^----^^^^^^^^^^^^^^^^^^^^^^^^^
+   |         |
+   |         expression has type `&str`
+
+warning: references are not nullable, so checking them for null will always return false
+  --> $DIR/fn_null_check.rs:38:8
+   |
+LL |     if (&[1, 2] as *const i32).is_null() {}
+   |        ^-------^^^^^^^^^^^^^^^^^^^^^^^^^
+   |         |
+   |         expression has type `&[i32; 2]`
+
+warning: references are not nullable, so checking them for null will always return false
+  --> $DIR/fn_null_check.rs:40:8
+   |
+LL |     if (&mut [1, 2] as *mut i32) == std::ptr::null_mut() {}
+   |        ^-----------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+   |         |
+   |         expression has type `&mut [i32; 2]`
+
+warning: 16 warnings emitted