about summary refs log tree commit diff
path: root/src/tools/clippy/tests/ui/borrow_as_ptr.rs
blob: ce248f157c6efadaa867ff99e7ad8c8bced3dc63 (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//@aux-build:proc_macros.rs
#![warn(clippy::borrow_as_ptr)]
#![allow(clippy::useless_vec)]

extern crate proc_macros;

fn a() -> i32 {
    0
}

#[clippy::msrv = "1.75"]
fn main() {
    let val = 1;
    let _p = &val as *const i32;
    //~^ borrow_as_ptr
    let _p = &0 as *const i32;
    let _p = &a() as *const i32;
    let vec = vec![1];
    let _p = &vec.len() as *const usize;

    let mut val_mut = 1;
    let _p_mut = &mut val_mut as *mut i32;
    //~^ borrow_as_ptr

    let mut x: [i32; 2] = [42, 43];
    let _raw = (&mut x[1] as *mut i32).wrapping_offset(-1);
    //~^ borrow_as_ptr
}

fn issue_13882() {
    let mut x: [i32; 2] = [42, 43];
    let _raw = (&mut x[1] as *mut i32).wrapping_offset(-1);
    //~^ borrow_as_ptr
}

fn implicit_cast() {
    let val = 1;
    let p: *const i32 = &val;
    //~^ borrow_as_ptr

    let mut val = 1;
    let p: *mut i32 = &mut val;
    //~^ borrow_as_ptr

    let mut val = 1;
    // Only lint the leftmost argument, the rightmost is ref to a temporary
    core::ptr::eq(&val, &1);
    //~^ borrow_as_ptr

    // Do not lint references to temporaries
    core::ptr::eq(&0i32, &1i32);
}

fn issue_15141() {
    let a = String::new();
    // Don't lint cast to dyn trait pointers
    let b = &a as *const dyn std::any::Any;
}

fn issue15389() {
    proc_macros::with_span! {
        span
        let var = 0u32;
        // Don't lint in proc-macros
        let _ = &var as *const u32;
    };
}