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 $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