Skip to content

Commit 4b3dadb

Browse files
committed
Also lint on cast/cast_mut and ptr::from_mut/ptr::from_ref
1 parent 0b9529c commit 4b3dadb

File tree

4 files changed

+121
-36
lines changed

4 files changed

+121
-36
lines changed

compiler/rustc_lint/src/ptr_nulls.rs

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,25 +31,45 @@ declare_lint! {
3131

3232
declare_lint_pass!(PtrNullChecks => [USELESS_PTR_NULL_CHECKS]);
3333

34-
fn incorrect_check<'a>(cx: &LateContext<'a>, expr: &Expr<'_>) -> Option<PtrNullChecksDiag<'a>> {
35-
let mut expr = expr.peel_blocks();
34+
/// This function detects and returns the original expression from a series of consecutive casts,
35+
/// ie. `(my_fn as *const _ as *mut _).cast_mut()` would return the expression for `my_fn`.
36+
fn ptr_cast_chain<'a>(cx: &'a LateContext<'_>, mut e: &'a Expr<'a>) -> Option<&'a Expr<'a>> {
3637
let mut had_at_least_one_cast = false;
37-
while let ExprKind::Cast(cast_expr, cast_ty) = expr.kind
38-
&& let TyKind::Ptr(_) = cast_ty.kind {
39-
expr = cast_expr.peel_blocks();
40-
had_at_least_one_cast = true;
38+
loop {
39+
e = e.peel_blocks();
40+
e = if let ExprKind::Cast(expr, t) = e.kind
41+
&& let TyKind::Ptr(_) = t.kind {
42+
had_at_least_one_cast = true;
43+
expr
44+
} else if let ExprKind::MethodCall(_, expr, [], _) = e.kind
45+
&& let Some(def_id) = cx.typeck_results().type_dependent_def_id(e.hir_id)
46+
&& matches!(cx.tcx.get_diagnostic_name(def_id), Some(sym::ptr_cast | sym::ptr_cast_mut)) {
47+
had_at_least_one_cast = true;
48+
expr
49+
} else if let ExprKind::Call(path, [arg]) = e.kind
50+
&& let ExprKind::Path(ref qpath) = path.kind
51+
&& let Some(def_id) = cx.qpath_res(qpath, path.hir_id).opt_def_id()
52+
&& matches!(cx.tcx.get_diagnostic_name(def_id), Some(sym::ptr_from_ref | sym::ptr_from_mut)) {
53+
had_at_least_one_cast = true;
54+
arg
55+
} else if had_at_least_one_cast {
56+
return Some(e);
57+
} else {
58+
return None;
59+
};
4160
}
42-
if !had_at_least_one_cast {
43-
None
61+
}
62+
63+
fn incorrect_check<'a>(cx: &LateContext<'a>, expr: &Expr<'_>) -> Option<PtrNullChecksDiag<'a>> {
64+
let expr = ptr_cast_chain(cx, expr)?;
65+
66+
let orig_ty = cx.typeck_results().expr_ty(expr);
67+
if orig_ty.is_fn() {
68+
Some(PtrNullChecksDiag::FnPtr)
69+
} else if orig_ty.is_ref() {
70+
Some(PtrNullChecksDiag::Ref { orig_ty, label: expr.span })
4471
} else {
45-
let orig_ty = cx.typeck_results().expr_ty(expr);
46-
if orig_ty.is_fn() {
47-
Some(PtrNullChecksDiag::FnPtr)
48-
} else if orig_ty.is_ref() {
49-
Some(PtrNullChecksDiag::Ref { orig_ty, label: expr.span })
50-
} else {
51-
None
52-
}
72+
None
5373
}
5474
}
5575

tests/ui/consts/ptr_is_null.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// check-pass
33

44
#![feature(const_ptr_is_null)]
5+
#![allow(useless_ptr_null_checks)]
56

67
const FOO: &usize = &42;
78

tests/ui/lint/ptr_null_checks.rs

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

3+
#![feature(ptr_from_ref)]
4+
5+
use std::ptr;
6+
37
extern "C" fn c_fn() {}
48
fn static_i32() -> &'static i32 { &1 }
59

@@ -21,6 +25,10 @@ fn main() {
2125
//~^ WARN function pointers are not nullable
2226
if (fn_ptr as *mut fn() as *const fn() as *const ()).is_null() {}
2327
//~^ WARN function pointers are not nullable
28+
if (fn_ptr as *mut fn() as *const fn()).cast_mut().is_null() {}
29+
//~^ WARN function pointers are not nullable
30+
if ((fn_ptr as *mut fn()).cast() as *const fn()).cast_mut().is_null() {}
31+
//~^ WARN function pointers are not nullable
2432
if (fn_ptr as fn() as *const ()).is_null() {}
2533
//~^ WARN function pointers are not nullable
2634
if (c_fn as *const fn()).is_null() {}
@@ -29,8 +37,16 @@ fn main() {
2937
// ---------------- References ------------------
3038
if (&mut 8 as *mut i32).is_null() {}
3139
//~^ WARN references are not nullable
40+
if ptr::from_mut(&mut 8).is_null() {}
41+
//~^ WARN references are not nullable
3242
if (&8 as *const i32).is_null() {}
3343
//~^ WARN references are not nullable
44+
if ptr::from_ref(&8).is_null() {}
45+
//~^ WARN references are not nullable
46+
if ptr::from_ref(&8).cast_mut().is_null() {}
47+
//~^ WARN references are not nullable
48+
if (ptr::from_ref(&8).cast_mut() as *mut i32).is_null() {}
49+
//~^ WARN references are not nullable
3450
if (&8 as *const i32) == std::ptr::null() {}
3551
//~^ WARN references are not nullable
3652
let ref_num = &8;

tests/ui/lint/ptr_null_checks.stderr

Lines changed: 68 additions & 20 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:10:8
2+
--> $DIR/ptr_null_checks.rs:14:8
33
|
44
LL | if (fn_ptr as *mut ()).is_null() {}
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -8,148 +8,196 @@ 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:12:8
11+
--> $DIR/ptr_null_checks.rs:16: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:14:8
19+
--> $DIR/ptr_null_checks.rs:18: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:16:8
27+
--> $DIR/ptr_null_checks.rs:20: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:18:8
35+
--> $DIR/ptr_null_checks.rs:22: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:20:8
43+
--> $DIR/ptr_null_checks.rs:24: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:22:8
51+
--> $DIR/ptr_null_checks.rs:26: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:24:8
59+
--> $DIR/ptr_null_checks.rs:28:8
60+
|
61+
LL | if (fn_ptr as *mut fn() as *const fn()).cast_mut().is_null() {}
62+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
63+
|
64+
= help: wrap the function pointer inside an `Option` and use `Option::is_none` to check for null pointer value
65+
66+
warning: function pointers are not nullable, so checking them for null will always return false
67+
--> $DIR/ptr_null_checks.rs:30:8
68+
|
69+
LL | if ((fn_ptr as *mut fn()).cast() as *const fn()).cast_mut().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+
74+
warning: function pointers are not nullable, so checking them for null will always return false
75+
--> $DIR/ptr_null_checks.rs:32:8
6076
|
6177
LL | if (fn_ptr as fn() as *const ()).is_null() {}
6278
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
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
67-
--> $DIR/ptr_null_checks.rs:26:8
83+
--> $DIR/ptr_null_checks.rs:34:8
6884
|
6985
LL | if (c_fn as *const fn()).is_null() {}
7086
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
7187
|
7288
= help: wrap the function pointer inside an `Option` and use `Option::is_none` to check for null pointer value
7389

7490
warning: references are not nullable, so checking them for null will always return false
75-
--> $DIR/ptr_null_checks.rs:30:8
91+
--> $DIR/ptr_null_checks.rs:38:8
7692
|
7793
LL | if (&mut 8 as *mut i32).is_null() {}
7894
| ^------^^^^^^^^^^^^^^^^^^^^^^^
7995
| |
8096
| expression has type `&mut i32`
8197

8298
warning: references are not nullable, so checking them for null will always return false
83-
--> $DIR/ptr_null_checks.rs:32:8
99+
--> $DIR/ptr_null_checks.rs:40:8
100+
|
101+
LL | if ptr::from_mut(&mut 8).is_null() {}
102+
| ^^^^^^^^^^^^^^------^^^^^^^^^^^
103+
| |
104+
| expression has type `&mut i32`
105+
106+
warning: references are not nullable, so checking them for null will always return false
107+
--> $DIR/ptr_null_checks.rs:42:8
84108
|
85109
LL | if (&8 as *const i32).is_null() {}
86110
| ^--^^^^^^^^^^^^^^^^^^^^^^^^^
87111
| |
88112
| expression has type `&i32`
89113

90114
warning: references are not nullable, so checking them for null will always return false
91-
--> $DIR/ptr_null_checks.rs:34:8
115+
--> $DIR/ptr_null_checks.rs:44:8
116+
|
117+
LL | if ptr::from_ref(&8).is_null() {}
118+
| ^^^^^^^^^^^^^^--^^^^^^^^^^^
119+
| |
120+
| expression has type `&i32`
121+
122+
warning: references are not nullable, so checking them for null will always return false
123+
--> $DIR/ptr_null_checks.rs:46:8
124+
|
125+
LL | if ptr::from_ref(&8).cast_mut().is_null() {}
126+
| ^^^^^^^^^^^^^^--^^^^^^^^^^^^^^^^^^^^^^
127+
| |
128+
| expression has type `&i32`
129+
130+
warning: references are not nullable, so checking them for null will always return false
131+
--> $DIR/ptr_null_checks.rs:48:8
132+
|
133+
LL | if (ptr::from_ref(&8).cast_mut() as *mut i32).is_null() {}
134+
| ^^^^^^^^^^^^^^^--^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
135+
| |
136+
| expression has type `&i32`
137+
138+
warning: references are not nullable, so checking them for null will always return false
139+
--> $DIR/ptr_null_checks.rs:50:8
92140
|
93141
LL | if (&8 as *const i32) == std::ptr::null() {}
94142
| ^--^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
95143
| |
96144
| expression has type `&i32`
97145

98146
warning: references are not nullable, so checking them for null will always return false
99-
--> $DIR/ptr_null_checks.rs:37:8
147+
--> $DIR/ptr_null_checks.rs:53:8
100148
|
101149
LL | if (ref_num as *const i32) == std::ptr::null() {}
102150
| ^-------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
103151
| |
104152
| expression has type `&i32`
105153

106154
warning: references are not nullable, so checking them for null will always return false
107-
--> $DIR/ptr_null_checks.rs:39:8
155+
--> $DIR/ptr_null_checks.rs:55:8
108156
|
109157
LL | if (b"\0" as *const u8).is_null() {}
110158
| ^-----^^^^^^^^^^^^^^^^^^^^^^^^
111159
| |
112160
| expression has type `&[u8; 1]`
113161

114162
warning: references are not nullable, so checking them for null will always return false
115-
--> $DIR/ptr_null_checks.rs:41:8
163+
--> $DIR/ptr_null_checks.rs:57:8
116164
|
117165
LL | if ("aa" as *const str).is_null() {}
118166
| ^----^^^^^^^^^^^^^^^^^^^^^^^^^
119167
| |
120168
| expression has type `&str`
121169

122170
warning: references are not nullable, so checking them for null will always return false
123-
--> $DIR/ptr_null_checks.rs:43:8
171+
--> $DIR/ptr_null_checks.rs:59:8
124172
|
125173
LL | if (&[1, 2] as *const i32).is_null() {}
126174
| ^-------^^^^^^^^^^^^^^^^^^^^^^^^^
127175
| |
128176
| expression has type `&[i32; 2]`
129177

130178
warning: references are not nullable, so checking them for null will always return false
131-
--> $DIR/ptr_null_checks.rs:45:8
179+
--> $DIR/ptr_null_checks.rs:61:8
132180
|
133181
LL | if (&mut [1, 2] as *mut i32) == std::ptr::null_mut() {}
134182
| ^-----------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
135183
| |
136184
| expression has type `&mut [i32; 2]`
137185

138186
warning: references are not nullable, so checking them for null will always return false
139-
--> $DIR/ptr_null_checks.rs:47:8
187+
--> $DIR/ptr_null_checks.rs:63:8
140188
|
141189
LL | if (static_i32() as *const i32).is_null() {}
142190
| ^------------^^^^^^^^^^^^^^^^^^^^^^^^^
143191
| |
144192
| expression has type `&i32`
145193

146194
warning: references are not nullable, so checking them for null will always return false
147-
--> $DIR/ptr_null_checks.rs:49:8
195+
--> $DIR/ptr_null_checks.rs:65:8
148196
|
149197
LL | if (&*{ static_i32() } as *const i32).is_null() {}
150198
| ^------------------^^^^^^^^^^^^^^^^^^^^^^^^^
151199
| |
152200
| expression has type `&i32`
153201

154-
warning: 19 warnings emitted
202+
warning: 25 warnings emitted
155203

0 commit comments

Comments
 (0)