about summary refs log tree commit diff
diff options
context:
space:
mode:
authorUrgau <urgau@numericable.fr>2023-08-03 10:57:11 +0200
committerUrgau <urgau@numericable.fr>2023-08-03 10:57:11 +0200
commitee519532f69610e208fc61a68537aa662c3e8d16 (patch)
treeecc682cb0e3a874e08c66c6d0422080cd8d6ecd8
parent4b3dadbe5a9f3dd06932a9099abd37bae751cdd3 (diff)
downloadrust-ee519532f69610e208fc61a68537aa662c3e8d16.tar.gz
rust-ee519532f69610e208fc61a68537aa662c3e8d16.zip
Also add label with original type for function pointers
-rw-r--r--compiler/rustc_lint/messages.ftl1
-rw-r--r--compiler/rustc_lint/src/lints.rs6
-rw-r--r--compiler/rustc_lint/src/ptr_nulls.rs2
-rw-r--r--tests/ui/lint/ptr_null_checks.stderr44
4 files changed, 40 insertions, 13 deletions
diff --git a/compiler/rustc_lint/messages.ftl b/compiler/rustc_lint/messages.ftl
index 4897ffd0cec..027a10de9bb 100644
--- a/compiler/rustc_lint/messages.ftl
+++ b/compiler/rustc_lint/messages.ftl
@@ -449,6 +449,7 @@ lint_path_statement_no_effect = path statement with no effect
 
 lint_ptr_null_checks_fn_ptr = function pointers are not nullable, so checking them for null will always return false
     .help = wrap the function pointer inside an `Option` and use `Option::is_none` to check for null pointer value
+    .label = expression has type `{$orig_ty}`
 
 lint_ptr_null_checks_ref = references are not nullable, so checking them for null will always return false
     .label = expression has type `{$orig_ty}`
diff --git a/compiler/rustc_lint/src/lints.rs b/compiler/rustc_lint/src/lints.rs
index 9e1d5605260..c0a8d078dc4 100644
--- a/compiler/rustc_lint/src/lints.rs
+++ b/compiler/rustc_lint/src/lints.rs
@@ -618,7 +618,11 @@ pub struct ExpectationNote {
 pub enum PtrNullChecksDiag<'a> {
     #[diag(lint_ptr_null_checks_fn_ptr)]
     #[help(lint_help)]
-    FnPtr,
+    FnPtr {
+        orig_ty: Ty<'a>,
+        #[label]
+        label: Span,
+    },
     #[diag(lint_ptr_null_checks_ref)]
     Ref {
         orig_ty: Ty<'a>,
diff --git a/compiler/rustc_lint/src/ptr_nulls.rs b/compiler/rustc_lint/src/ptr_nulls.rs
index 64b86fec46b..02aff91032f 100644
--- a/compiler/rustc_lint/src/ptr_nulls.rs
+++ b/compiler/rustc_lint/src/ptr_nulls.rs
@@ -65,7 +65,7 @@ fn incorrect_check<'a>(cx: &LateContext<'a>, expr: &Expr<'_>) -> Option<PtrNullC
 
     let orig_ty = cx.typeck_results().expr_ty(expr);
     if orig_ty.is_fn() {
-        Some(PtrNullChecksDiag::FnPtr)
+        Some(PtrNullChecksDiag::FnPtr { orig_ty, label: expr.span })
     } else if orig_ty.is_ref() {
         Some(PtrNullChecksDiag::Ref { orig_ty, label: expr.span })
     } else {
diff --git a/tests/ui/lint/ptr_null_checks.stderr b/tests/ui/lint/ptr_null_checks.stderr
index 525d96c4919..3cee1804b62 100644
--- a/tests/ui/lint/ptr_null_checks.stderr
+++ b/tests/ui/lint/ptr_null_checks.stderr
@@ -2,7 +2,9 @@ warning: function pointers are not nullable, so checking them for null will alwa
   --> $DIR/ptr_null_checks.rs:14:8
    |
 LL |     if (fn_ptr as *mut ()).is_null() {}
-   |        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+   |        ^------^^^^^^^^^^^^^^^^^^^^^^
+   |         |
+   |         expression has type `fn() {main}`
    |
    = help: wrap the function pointer inside an `Option` and use `Option::is_none` to check for null pointer value
    = note: `#[warn(useless_ptr_null_checks)]` on by default
@@ -11,7 +13,9 @@ warning: function pointers are not nullable, so checking them for null will alwa
   --> $DIR/ptr_null_checks.rs:16:8
    |
 LL |     if (fn_ptr as *const u8).is_null() {}
-   |        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+   |        ^------^^^^^^^^^^^^^^^^^^^^^^^^
+   |         |
+   |         expression has type `fn() {main}`
    |
    = help: wrap the function pointer inside an `Option` and use `Option::is_none` to check for null pointer value
 
@@ -19,7 +23,9 @@ warning: function pointers are not nullable, so checking them for null will alwa
   --> $DIR/ptr_null_checks.rs:18:8
    |
 LL |     if (fn_ptr as *const ()) == std::ptr::null() {}
-   |        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+   |        ^------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+   |         |
+   |         expression has type `fn() {main}`
    |
    = help: wrap the function pointer inside an `Option` and use `Option::is_none` to check for null pointer value
 
@@ -27,7 +33,9 @@ warning: function pointers are not nullable, so checking them for null will alwa
   --> $DIR/ptr_null_checks.rs:20:8
    |
 LL |     if (fn_ptr as *mut ()) == std::ptr::null_mut() {}
-   |        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+   |        ^------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+   |         |
+   |         expression has type `fn() {main}`
    |
    = help: wrap the function pointer inside an `Option` and use `Option::is_none` to check for null pointer value
 
@@ -35,7 +43,9 @@ warning: function pointers are not nullable, so checking them for null will alwa
   --> $DIR/ptr_null_checks.rs:22:8
    |
 LL |     if (fn_ptr as *const ()) == (0 as *const ()) {}
-   |        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+   |        ^------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+   |         |
+   |         expression has type `fn() {main}`
    |
    = help: wrap the function pointer inside an `Option` and use `Option::is_none` to check for null pointer value
 
@@ -43,7 +53,9 @@ warning: function pointers are not nullable, so checking them for null will alwa
   --> $DIR/ptr_null_checks.rs:24:8
    |
 LL |     if <*const _>::is_null(fn_ptr as *const ()) {}
-   |        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+   |        ^^^^^^^^^^^^^^^^^^^^------^^^^^^^^^^^^^^
+   |                            |
+   |                            expression has type `fn() {main}`
    |
    = help: wrap the function pointer inside an `Option` and use `Option::is_none` to check for null pointer value
 
@@ -51,7 +63,9 @@ warning: function pointers are not nullable, so checking them for null will alwa
   --> $DIR/ptr_null_checks.rs:26:8
    |
 LL |     if (fn_ptr as *mut fn() as *const fn() as *const ()).is_null() {}
-   |        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+   |        ^------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+   |         |
+   |         expression has type `fn() {main}`
    |
    = help: wrap the function pointer inside an `Option` and use `Option::is_none` to check for null pointer value
 
@@ -59,7 +73,9 @@ warning: function pointers are not nullable, so checking them for null will alwa
   --> $DIR/ptr_null_checks.rs:28:8
    |
 LL |     if (fn_ptr as *mut fn() as *const fn()).cast_mut().is_null() {}
-   |        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+   |        ^------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+   |         |
+   |         expression has type `fn() {main}`
    |
    = help: wrap the function pointer inside an `Option` and use `Option::is_none` to check for null pointer value
 
@@ -67,7 +83,9 @@ warning: function pointers are not nullable, so checking them for null will alwa
   --> $DIR/ptr_null_checks.rs:30:8
    |
 LL |     if ((fn_ptr as *mut fn()).cast() as *const fn()).cast_mut().is_null() {}
-   |        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+   |        ^^------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+   |          |
+   |          expression has type `fn() {main}`
    |
    = help: wrap the function pointer inside an `Option` and use `Option::is_none` to check for null pointer value
 
@@ -75,7 +93,9 @@ warning: function pointers are not nullable, so checking them for null will alwa
   --> $DIR/ptr_null_checks.rs:32:8
    |
 LL |     if (fn_ptr as fn() as *const ()).is_null() {}
-   |        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+   |        ^--------------^^^^^^^^^^^^^^^^^^^^^^^^
+   |         |
+   |         expression has type `fn()`
    |
    = help: wrap the function pointer inside an `Option` and use `Option::is_none` to check for null pointer value
 
@@ -83,7 +103,9 @@ warning: function pointers are not nullable, so checking them for null will alwa
   --> $DIR/ptr_null_checks.rs:34:8
    |
 LL |     if (c_fn as *const fn()).is_null() {}
-   |        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+   |        ^----^^^^^^^^^^^^^^^^^^^^^^^^^^
+   |         |
+   |         expression has type `extern "C" fn() {c_fn}`
    |
    = help: wrap the function pointer inside an `Option` and use `Option::is_none` to check for null pointer value