about summary refs log tree commit diff
path: root/tests/ui/lint/int_to_ptr.rs
blob: 7f60da47b85d8e70fec1bbf389e8115d07ae7355 (plain)
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
// Checks for the `integer_to_pointer_transmutes` lint

//@ check-pass
//@ run-rustfix

#![allow(unused_unsafe)]
#![allow(dead_code)]

unsafe fn should_lint(a: usize) {
    let _ptr: *const u8 = unsafe { std::mem::transmute::<usize, *const u8>(a) };
    //~^ WARN transmuting an integer to a pointer
    let _ptr: *mut u8 = unsafe { std::mem::transmute::<usize, *mut u8>(a) };
    //~^ WARN transmuting an integer to a pointer
    let _ref: &'static u8 = unsafe { std::mem::transmute::<usize, &'static u8>(a) };
    //~^ WARN transmuting an integer to a pointer
    let _ref: &'static mut u8 = unsafe { std::mem::transmute::<usize, &'static mut u8>(a) };
    //~^ WARN transmuting an integer to a pointer

    let _ptr = unsafe { std::mem::transmute::<usize, *const u8>(42usize) };
    //~^ WARN transmuting an integer to a pointer
    let _ptr = unsafe { std::mem::transmute::<usize, *const u8>(a + a) };
    //~^ WARN transmuting an integer to a pointer
}

const unsafe fn should_lintin_const(a: usize) {
    let _ptr: *const u8 = unsafe { std::mem::transmute::<usize, *const u8>(a) };
    //~^ WARN transmuting an integer to a pointer
    let _ptr: *mut u8 = unsafe { std::mem::transmute::<usize, *mut u8>(a) };
    //~^ WARN transmuting an integer to a pointer
    let _ref: &'static u8 = unsafe { std::mem::transmute::<usize, &'static u8>(a) };
    //~^ WARN transmuting an integer to a pointer
    let _ref: &'static mut u8 = unsafe { std::mem::transmute::<usize, &'static mut u8>(a) };
    //~^ WARN transmuting an integer to a pointer

    let _ptr = unsafe { std::mem::transmute::<usize, *const u8>(42usize) };
    //~^ WARN transmuting an integer to a pointer
    let _ptr = unsafe { std::mem::transmute::<usize, *const u8>(a + a) };
    //~^ WARN transmuting an integer to a pointer
}

unsafe fn should_not_lint(a: usize) {
    let _ptr = unsafe { std::mem::transmute::<usize, *const u8>(0usize) }; // linted by other lints
    let _ptr = unsafe { std::mem::transmute::<usize, *const ()>(a) }; // inner type is a ZST
    let _ptr = unsafe { std::mem::transmute::<usize, fn()>(a) }; // omit fn-ptr for now
}

fn main() {}