File: ptr_null_checks.rs

package info (click to toggle)
rustc 1.85.0%2Bdfsg3-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid, trixie
  • size: 893,396 kB
  • sloc: xml: 158,127; python: 35,830; javascript: 19,497; cpp: 19,002; sh: 17,245; ansic: 13,127; asm: 4,376; makefile: 1,051; perl: 29; lisp: 29; ruby: 19; sql: 11
file content (80 lines) | stat: -rw-r--r-- 3,178 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//@ check-pass

use std::ptr;

extern "C" fn c_fn() {}
fn static_i32() -> &'static i32 { &1 }

fn main() {
    let fn_ptr = main;

    // ------------- Function pointers ---------------
    if (fn_ptr as *mut ()).is_null() {}
    //~^ WARN function pointers are not nullable
    if (fn_ptr as *const u8).is_null() {}
    //~^ WARN function pointers are not nullable
    if (fn_ptr as *const ()) == std::ptr::null() {}
    //~^ WARN function pointers are not nullable
    if (fn_ptr as *mut ()) == std::ptr::null_mut() {}
    //~^ WARN function pointers are not nullable
    if (fn_ptr as *const ()) == (0 as *const ()) {}
    //~^ WARN function pointers are not nullable
    if <*const _>::is_null(fn_ptr as *const ()) {}
    //~^ WARN function pointers are not nullable
    if (fn_ptr as *mut fn() as *const fn() as *const ()).is_null() {}
    //~^ WARN function pointers are not nullable
    if (fn_ptr as *mut fn() as *const fn()).cast_mut().is_null() {}
    //~^ WARN function pointers are not nullable
    if ((fn_ptr as *mut fn()).cast() as *const fn()).cast_mut().is_null() {}
    //~^ WARN function pointers are not nullable
    if (fn_ptr as fn() as *const ()).is_null() {}
    //~^ WARN function pointers are not nullable
    if (c_fn as *const fn()).is_null() {}
    //~^ WARN function pointers are not nullable

    // ---------------- References ------------------
    if (&mut 8 as *mut i32).is_null() {}
    //~^ WARN references are not nullable
    if ptr::from_mut(&mut 8).is_null() {}
    //~^ WARN call is never null
    if (&8 as *const i32).is_null() {}
    //~^ WARN references are not nullable
    if ptr::from_ref(&8).is_null() {}
    //~^ WARN call is never null
    if ptr::from_ref(&8).cast_mut().is_null() {}
    //~^ WARN call is never null
    if (ptr::from_ref(&8).cast_mut() as *mut i32).is_null() {}
    //~^ WARN call is never null
    if (&8 as *const i32) == std::ptr::null() {}
    //~^ WARN references are not nullable
    let ref_num = &8;
    if (ref_num as *const i32) == std::ptr::null() {}
    //~^ WARN references are not nullable
    if (b"\0" as *const u8).is_null() {}
    //~^ WARN references are not nullable
    if ("aa" as *const str).is_null() {}
    //~^ WARN references are not nullable
    if (&[1, 2] as *const i32).is_null() {}
    //~^ WARN references are not nullable
    if (&mut [1, 2] as *mut i32) == std::ptr::null_mut() {}
    //~^ WARN references are not nullable
    if (static_i32() as *const i32).is_null() {}
    //~^ WARN references are not nullable
    if (&*{ static_i32() } as *const i32).is_null() {}
    //~^ WARN references are not nullable

    // ---------------- Functions -------------------
    if ptr::NonNull::new(&mut 8).unwrap().as_ptr().is_null() {}
    //~^ WARN call is never null
    if ptr::NonNull::<u8>::dangling().as_ptr().is_null() {}
    //~^ WARN call is never null

    // ----------------------------------------------
    const ZPTR: *const () = 0 as *const _;
    const NOT_ZPTR: *const () = 1 as *const _;

    // unlike the uplifted clippy::fn_null_check lint we do
    // not lint on them
    if (fn_ptr as *const ()) == ZPTR {}
    if (fn_ptr as *const ()) == NOT_ZPTR {}
}