Skip to content

Commit ef3413d

Browse files
committed
Add more tests for useless_ptr_null_checks lint
1 parent 84c5372 commit ef3413d

File tree

2 files changed

+50
-17
lines changed

2 files changed

+50
-17
lines changed

tests/ui/lint/ptr_null_checks.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
// check-pass
22

3+
extern "C" fn c_fn() {}
4+
fn static_i32() -> &'static i32 { &1 }
5+
36
fn main() {
47
let fn_ptr = main;
58

@@ -20,6 +23,8 @@ fn main() {
2023
//~^ WARN function pointers are not nullable
2124
if (fn_ptr as fn() as *const ()).is_null() {}
2225
//~^ WARN function pointers are not nullable
26+
if (c_fn as *const fn()).is_null() {}
27+
//~^ WARN function pointers are not nullable
2328

2429
// ---------------- References ------------------
2530
if (&mut 8 as *mut i32).is_null() {}
@@ -39,6 +44,10 @@ fn main() {
3944
//~^ WARN references are not nullable
4045
if (&mut [1, 2] as *mut i32) == std::ptr::null_mut() {}
4146
//~^ WARN references are not nullable
47+
if (static_i32() as *const i32).is_null() {}
48+
//~^ WARN references are not nullable
49+
if (&*{ static_i32() } as *const i32).is_null() {}
50+
//~^ WARN references are not nullable
4251

4352
// ----------------------------------------------
4453
const ZPTR: *const () = 0 as *const _;

tests/ui/lint/ptr_null_checks.stderr

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
warning: function pointers are not nullable, so checking them for null will always return false
2-
--> $DIR/ptr_null_checks.rs:7:8
2+
--> $DIR/ptr_null_checks.rs:10:8
33
|
44
LL | if (fn_ptr as *mut ()).is_null() {}
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -8,124 +8,148 @@ LL | if (fn_ptr as *mut ()).is_null() {}
88
= note: `#[warn(useless_ptr_null_checks)]` on by default
99

1010
warning: function pointers are not nullable, so checking them for null will always return false
11-
--> $DIR/ptr_null_checks.rs:9:8
11+
--> $DIR/ptr_null_checks.rs:12:8
1212
|
1313
LL | if (fn_ptr as *const u8).is_null() {}
1414
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1515
|
1616
= help: wrap the function pointer inside an `Option` and use `Option::is_none` to check for null pointer value
1717

1818
warning: function pointers are not nullable, so checking them for null will always return false
19-
--> $DIR/ptr_null_checks.rs:11:8
19+
--> $DIR/ptr_null_checks.rs:14:8
2020
|
2121
LL | if (fn_ptr as *const ()) == std::ptr::null() {}
2222
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2323
|
2424
= help: wrap the function pointer inside an `Option` and use `Option::is_none` to check for null pointer value
2525

2626
warning: function pointers are not nullable, so checking them for null will always return false
27-
--> $DIR/ptr_null_checks.rs:13:8
27+
--> $DIR/ptr_null_checks.rs:16:8
2828
|
2929
LL | if (fn_ptr as *mut ()) == std::ptr::null_mut() {}
3030
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3131
|
3232
= help: wrap the function pointer inside an `Option` and use `Option::is_none` to check for null pointer value
3333

3434
warning: function pointers are not nullable, so checking them for null will always return false
35-
--> $DIR/ptr_null_checks.rs:15:8
35+
--> $DIR/ptr_null_checks.rs:18:8
3636
|
3737
LL | if (fn_ptr as *const ()) == (0 as *const ()) {}
3838
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3939
|
4040
= help: wrap the function pointer inside an `Option` and use `Option::is_none` to check for null pointer value
4141

4242
warning: function pointers are not nullable, so checking them for null will always return false
43-
--> $DIR/ptr_null_checks.rs:17:8
43+
--> $DIR/ptr_null_checks.rs:20:8
4444
|
4545
LL | if <*const _>::is_null(fn_ptr as *const ()) {}
4646
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4747
|
4848
= help: wrap the function pointer inside an `Option` and use `Option::is_none` to check for null pointer value
4949

5050
warning: function pointers are not nullable, so checking them for null will always return false
51-
--> $DIR/ptr_null_checks.rs:19:8
51+
--> $DIR/ptr_null_checks.rs:22:8
5252
|
5353
LL | if (fn_ptr as *mut fn() as *const fn() as *const ()).is_null() {}
5454
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5555
|
5656
= help: wrap the function pointer inside an `Option` and use `Option::is_none` to check for null pointer value
5757

5858
warning: function pointers are not nullable, so checking them for null will always return false
59-
--> $DIR/ptr_null_checks.rs:21:8
59+
--> $DIR/ptr_null_checks.rs:24:8
6060
|
6161
LL | if (fn_ptr as fn() as *const ()).is_null() {}
6262
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6363
|
6464
= help: wrap the function pointer inside an `Option` and use `Option::is_none` to check for null pointer value
6565

66+
warning: function pointers are not nullable, so checking them for null will always return false
67+
--> $DIR/ptr_null_checks.rs:26:8
68+
|
69+
LL | if (c_fn as *const fn()).is_null() {}
70+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
71+
|
72+
= help: wrap the function pointer inside an `Option` and use `Option::is_none` to check for null pointer value
73+
6674
warning: references are not nullable, so checking them for null will always return false
67-
--> $DIR/ptr_null_checks.rs:25:8
75+
--> $DIR/ptr_null_checks.rs:30:8
6876
|
6977
LL | if (&mut 8 as *mut i32).is_null() {}
7078
| ^------^^^^^^^^^^^^^^^^^^^^^^^
7179
| |
7280
| expression has type `&mut i32`
7381

7482
warning: references are not nullable, so checking them for null will always return false
75-
--> $DIR/ptr_null_checks.rs:27:8
83+
--> $DIR/ptr_null_checks.rs:32:8
7684
|
7785
LL | if (&8 as *const i32).is_null() {}
7886
| ^--^^^^^^^^^^^^^^^^^^^^^^^^^
7987
| |
8088
| expression has type `&i32`
8189

8290
warning: references are not nullable, so checking them for null will always return false
83-
--> $DIR/ptr_null_checks.rs:29:8
91+
--> $DIR/ptr_null_checks.rs:34:8
8492
|
8593
LL | if (&8 as *const i32) == std::ptr::null() {}
8694
| ^--^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
8795
| |
8896
| expression has type `&i32`
8997

9098
warning: references are not nullable, so checking them for null will always return false
91-
--> $DIR/ptr_null_checks.rs:32:8
99+
--> $DIR/ptr_null_checks.rs:37:8
92100
|
93101
LL | if (ref_num as *const i32) == std::ptr::null() {}
94102
| ^-------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
95103
| |
96104
| expression has type `&i32`
97105

98106
warning: references are not nullable, so checking them for null will always return false
99-
--> $DIR/ptr_null_checks.rs:34:8
107+
--> $DIR/ptr_null_checks.rs:39:8
100108
|
101109
LL | if (b"\0" as *const u8).is_null() {}
102110
| ^-----^^^^^^^^^^^^^^^^^^^^^^^^
103111
| |
104112
| expression has type `&[u8; 1]`
105113

106114
warning: references are not nullable, so checking them for null will always return false
107-
--> $DIR/ptr_null_checks.rs:36:8
115+
--> $DIR/ptr_null_checks.rs:41:8
108116
|
109117
LL | if ("aa" as *const str).is_null() {}
110118
| ^----^^^^^^^^^^^^^^^^^^^^^^^^^
111119
| |
112120
| expression has type `&str`
113121

114122
warning: references are not nullable, so checking them for null will always return false
115-
--> $DIR/ptr_null_checks.rs:38:8
123+
--> $DIR/ptr_null_checks.rs:43:8
116124
|
117125
LL | if (&[1, 2] as *const i32).is_null() {}
118126
| ^-------^^^^^^^^^^^^^^^^^^^^^^^^^
119127
| |
120128
| expression has type `&[i32; 2]`
121129

122130
warning: references are not nullable, so checking them for null will always return false
123-
--> $DIR/ptr_null_checks.rs:40:8
131+
--> $DIR/ptr_null_checks.rs:45:8
124132
|
125133
LL | if (&mut [1, 2] as *mut i32) == std::ptr::null_mut() {}
126134
| ^-----------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
127135
| |
128136
| expression has type `&mut [i32; 2]`
129137

130-
warning: 16 warnings emitted
138+
warning: references are not nullable, so checking them for null will always return false
139+
--> $DIR/ptr_null_checks.rs:47:8
140+
|
141+
LL | if (static_i32() as *const i32).is_null() {}
142+
| ^------------^^^^^^^^^^^^^^^^^^^^^^^^^
143+
| |
144+
| expression has type `&i32`
145+
146+
warning: references are not nullable, so checking them for null will always return false
147+
--> $DIR/ptr_null_checks.rs:49:8
148+
|
149+
LL | if (&*{ static_i32() } as *const i32).is_null() {}
150+
| ^------------------^^^^^^^^^^^^^^^^^^^^^^^^^
151+
| |
152+
| expression has type `&i32`
153+
154+
warning: 19 warnings emitted
131155

0 commit comments

Comments
 (0)