Skip to content

Commit

Permalink
Also add label with original type for function pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
Urgau committed Aug 3, 2023
1 parent 4b3dadb commit ee51953
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 13 deletions.
1 change: 1 addition & 0 deletions compiler/rustc_lint/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -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}`
Expand Down
6 changes: 5 additions & 1 deletion compiler/rustc_lint/src/lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_lint/src/ptr_nulls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
44 changes: 33 additions & 11 deletions tests/ui/lint/ptr_null_checks.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -11,79 +13,99 @@ 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

warning: function pointers are not nullable, so checking them for null will always return false
--> $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

warning: function pointers are not nullable, so checking them for null will always return false
--> $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

warning: function pointers are not nullable, so checking them for null will always return false
--> $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

warning: function pointers are not nullable, so checking them for null will always return false
--> $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

warning: function pointers are not nullable, so checking them for null will always return false
--> $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

warning: function pointers are not nullable, so checking them for null will always return false
--> $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

warning: function pointers are not nullable, so checking them for null will always return false
--> $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

warning: function pointers are not nullable, so checking them for null will always return false
--> $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

warning: function pointers are not nullable, so checking them for null will always return false
--> $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

Expand Down

0 comments on commit ee51953

Please sign in to comment.