Also add label with original type for function pointers

This commit is contained in:
Urgau 2023-08-03 10:57:11 +02:00
parent 4b3dadbe5a
commit ee519532f6
4 changed files with 40 additions and 13 deletions

View file

@ -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 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 .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 lint_ptr_null_checks_ref = references are not nullable, so checking them for null will always return false
.label = expression has type `{$orig_ty}` .label = expression has type `{$orig_ty}`

View file

@ -618,7 +618,11 @@ pub struct ExpectationNote {
pub enum PtrNullChecksDiag<'a> { pub enum PtrNullChecksDiag<'a> {
#[diag(lint_ptr_null_checks_fn_ptr)] #[diag(lint_ptr_null_checks_fn_ptr)]
#[help(lint_help)] #[help(lint_help)]
FnPtr, FnPtr {
orig_ty: Ty<'a>,
#[label]
label: Span,
},
#[diag(lint_ptr_null_checks_ref)] #[diag(lint_ptr_null_checks_ref)]
Ref { Ref {
orig_ty: Ty<'a>, orig_ty: Ty<'a>,

View file

@ -65,7 +65,7 @@ fn incorrect_check<'a>(cx: &LateContext<'a>, expr: &Expr<'_>) -> Option<PtrNullC
let orig_ty = cx.typeck_results().expr_ty(expr); let orig_ty = cx.typeck_results().expr_ty(expr);
if orig_ty.is_fn() { if orig_ty.is_fn() {
Some(PtrNullChecksDiag::FnPtr) Some(PtrNullChecksDiag::FnPtr { orig_ty, label: expr.span })
} else if orig_ty.is_ref() { } else if orig_ty.is_ref() {
Some(PtrNullChecksDiag::Ref { orig_ty, label: expr.span }) Some(PtrNullChecksDiag::Ref { orig_ty, label: expr.span })
} else { } else {

View file

@ -2,7 +2,9 @@ warning: function pointers are not nullable, so checking them for null will alwa
--> $DIR/ptr_null_checks.rs:14:8 --> $DIR/ptr_null_checks.rs:14:8
| |
LL | if (fn_ptr as *mut ()).is_null() {} 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 = 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 = 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 --> $DIR/ptr_null_checks.rs:16:8
| |
LL | if (fn_ptr as *const u8).is_null() {} 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 = 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 --> $DIR/ptr_null_checks.rs:18:8
| |
LL | if (fn_ptr as *const ()) == std::ptr::null() {} 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 = 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 --> $DIR/ptr_null_checks.rs:20:8
| |
LL | if (fn_ptr as *mut ()) == std::ptr::null_mut() {} 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 = 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 --> $DIR/ptr_null_checks.rs:22:8
| |
LL | if (fn_ptr as *const ()) == (0 as *const ()) {} 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 = 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 --> $DIR/ptr_null_checks.rs:24:8
| |
LL | if <*const _>::is_null(fn_ptr as *const ()) {} 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 = 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 --> $DIR/ptr_null_checks.rs:26:8
| |
LL | if (fn_ptr as *mut fn() as *const fn() as *const ()).is_null() {} 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 = 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 --> $DIR/ptr_null_checks.rs:28:8
| |
LL | if (fn_ptr as *mut fn() as *const fn()).cast_mut().is_null() {} 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 = 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 --> $DIR/ptr_null_checks.rs:30:8
| |
LL | if ((fn_ptr as *mut fn()).cast() as *const fn()).cast_mut().is_null() {} 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 = 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 --> $DIR/ptr_null_checks.rs:32:8
| |
LL | if (fn_ptr as fn() as *const ()).is_null() {} 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 = 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 --> $DIR/ptr_null_checks.rs:34:8
| |
LL | if (c_fn as *const fn()).is_null() {} 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 = help: wrap the function pointer inside an `Option` and use `Option::is_none` to check for null pointer value