Skip to content

Commit ee51953

Browse files
committed
Also add label with original type for function pointers
1 parent 4b3dadb commit ee51953

File tree

4 files changed

+40
-13
lines changed

4 files changed

+40
-13
lines changed

compiler/rustc_lint/messages.ftl

+1
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,7 @@ lint_path_statement_no_effect = path statement with no effect
449449
450450
lint_ptr_null_checks_fn_ptr = function pointers are not nullable, so checking them for null will always return false
451451
.help = wrap the function pointer inside an `Option` and use `Option::is_none` to check for null pointer value
452+
.label = expression has type `{$orig_ty}`
452453
453454
lint_ptr_null_checks_ref = references are not nullable, so checking them for null will always return false
454455
.label = expression has type `{$orig_ty}`

compiler/rustc_lint/src/lints.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,11 @@ pub struct ExpectationNote {
618618
pub enum PtrNullChecksDiag<'a> {
619619
#[diag(lint_ptr_null_checks_fn_ptr)]
620620
#[help(lint_help)]
621-
FnPtr,
621+
FnPtr {
622+
orig_ty: Ty<'a>,
623+
#[label]
624+
label: Span,
625+
},
622626
#[diag(lint_ptr_null_checks_ref)]
623627
Ref {
624628
orig_ty: Ty<'a>,

compiler/rustc_lint/src/ptr_nulls.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ fn incorrect_check<'a>(cx: &LateContext<'a>, expr: &Expr<'_>) -> Option<PtrNullC
6565

6666
let orig_ty = cx.typeck_results().expr_ty(expr);
6767
if orig_ty.is_fn() {
68-
Some(PtrNullChecksDiag::FnPtr)
68+
Some(PtrNullChecksDiag::FnPtr { orig_ty, label: expr.span })
6969
} else if orig_ty.is_ref() {
7070
Some(PtrNullChecksDiag::Ref { orig_ty, label: expr.span })
7171
} else {

tests/ui/lint/ptr_null_checks.stderr

+33-11
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ warning: function pointers are not nullable, so checking them for null will alwa
22
--> $DIR/ptr_null_checks.rs:14:8
33
|
44
LL | if (fn_ptr as *mut ()).is_null() {}
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5+
| ^------^^^^^^^^^^^^^^^^^^^^^^
6+
| |
7+
| expression has type `fn() {main}`
68
|
79
= help: wrap the function pointer inside an `Option` and use `Option::is_none` to check for null pointer value
810
= note: `#[warn(useless_ptr_null_checks)]` on by default
@@ -11,79 +13,99 @@ warning: function pointers are not nullable, so checking them for null will alwa
1113
--> $DIR/ptr_null_checks.rs:16:8
1214
|
1315
LL | if (fn_ptr as *const u8).is_null() {}
14-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
16+
| ^------^^^^^^^^^^^^^^^^^^^^^^^^
17+
| |
18+
| expression has type `fn() {main}`
1519
|
1620
= help: wrap the function pointer inside an `Option` and use `Option::is_none` to check for null pointer value
1721

1822
warning: function pointers are not nullable, so checking them for null will always return false
1923
--> $DIR/ptr_null_checks.rs:18:8
2024
|
2125
LL | if (fn_ptr as *const ()) == std::ptr::null() {}
22-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
26+
| ^------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
27+
| |
28+
| expression has type `fn() {main}`
2329
|
2430
= help: wrap the function pointer inside an `Option` and use `Option::is_none` to check for null pointer value
2531

2632
warning: function pointers are not nullable, so checking them for null will always return false
2733
--> $DIR/ptr_null_checks.rs:20:8
2834
|
2935
LL | if (fn_ptr as *mut ()) == std::ptr::null_mut() {}
30-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
36+
| ^------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
37+
| |
38+
| expression has type `fn() {main}`
3139
|
3240
= help: wrap the function pointer inside an `Option` and use `Option::is_none` to check for null pointer value
3341

3442
warning: function pointers are not nullable, so checking them for null will always return false
3543
--> $DIR/ptr_null_checks.rs:22:8
3644
|
3745
LL | if (fn_ptr as *const ()) == (0 as *const ()) {}
38-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
46+
| ^------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
47+
| |
48+
| expression has type `fn() {main}`
3949
|
4050
= help: wrap the function pointer inside an `Option` and use `Option::is_none` to check for null pointer value
4151

4252
warning: function pointers are not nullable, so checking them for null will always return false
4353
--> $DIR/ptr_null_checks.rs:24:8
4454
|
4555
LL | if <*const _>::is_null(fn_ptr as *const ()) {}
46-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
56+
| ^^^^^^^^^^^^^^^^^^^^------^^^^^^^^^^^^^^
57+
| |
58+
| expression has type `fn() {main}`
4759
|
4860
= help: wrap the function pointer inside an `Option` and use `Option::is_none` to check for null pointer value
4961

5062
warning: function pointers are not nullable, so checking them for null will always return false
5163
--> $DIR/ptr_null_checks.rs:26:8
5264
|
5365
LL | if (fn_ptr as *mut fn() as *const fn() as *const ()).is_null() {}
54-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66+
| ^------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
67+
| |
68+
| expression has type `fn() {main}`
5569
|
5670
= help: wrap the function pointer inside an `Option` and use `Option::is_none` to check for null pointer value
5771

5872
warning: function pointers are not nullable, so checking them for null will always return false
5973
--> $DIR/ptr_null_checks.rs:28:8
6074
|
6175
LL | if (fn_ptr as *mut fn() as *const fn()).cast_mut().is_null() {}
62-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
76+
| ^------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
77+
| |
78+
| expression has type `fn() {main}`
6379
|
6480
= help: wrap the function pointer inside an `Option` and use `Option::is_none` to check for null pointer value
6581

6682
warning: function pointers are not nullable, so checking them for null will always return false
6783
--> $DIR/ptr_null_checks.rs:30:8
6884
|
6985
LL | if ((fn_ptr as *mut fn()).cast() as *const fn()).cast_mut().is_null() {}
70-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
86+
| ^^------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
87+
| |
88+
| expression has type `fn() {main}`
7189
|
7290
= help: wrap the function pointer inside an `Option` and use `Option::is_none` to check for null pointer value
7391

7492
warning: function pointers are not nullable, so checking them for null will always return false
7593
--> $DIR/ptr_null_checks.rs:32:8
7694
|
7795
LL | if (fn_ptr as fn() as *const ()).is_null() {}
78-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
96+
| ^--------------^^^^^^^^^^^^^^^^^^^^^^^^
97+
| |
98+
| expression has type `fn()`
7999
|
80100
= help: wrap the function pointer inside an `Option` and use `Option::is_none` to check for null pointer value
81101

82102
warning: function pointers are not nullable, so checking them for null will always return false
83103
--> $DIR/ptr_null_checks.rs:34:8
84104
|
85105
LL | if (c_fn as *const fn()).is_null() {}
86-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
106+
| ^----^^^^^^^^^^^^^^^^^^^^^^^^^^
107+
| |
108+
| expression has type `extern "C" fn() {c_fn}`
87109
|
88110
= help: wrap the function pointer inside an `Option` and use `Option::is_none` to check for null pointer value
89111

0 commit comments

Comments
 (0)