about summary refs log tree commit diff
path: root/src/tools/clippy/tests/ui/pointer_format.rs
blob: 0621f966ad1111325e0954aa9034f4d4f91d5e6c (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
#![warn(clippy::pointer_format)]

use core::fmt::Debug;
use core::marker::PhantomData;

#[derive(Debug)]
struct ContainsPointerDeep {
    w: WithPointer,
}

struct ManualDebug {
    ptr: *const u8,
}

#[derive(Debug)]
struct WithPointer {
    len: usize,
    ptr: *const u8,
}

impl std::fmt::Debug for ManualDebug {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        f.write_str("ManualDebug")
    }
}

trait Foo {
    type Assoc: Foo + Debug;
}

#[derive(Debug)]
struct S<T: Foo + 'static>(&'static S<T::Assoc>, PhantomData<T>);

#[allow(unused)]
fn unbounded<T: Foo + Debug + 'static>(s: &S<T>) {
    format!("{s:?}");
}

fn main() {
    let m = &(main as fn());
    let g = &0;
    let o = &format!("{m:p}");
    //~^ pointer_format
    let _ = format!("{m:?}");
    //~^ pointer_format
    println!("{g:p}");
    //~^ pointer_format
    panic!("{o:p}");
    //~^ pointer_format
    let answer = 42;
    let x = &raw const answer;
    let arr = [0u8; 8];
    let with_ptr = WithPointer { len: 8, ptr: &arr as _ };
    let _ = format!("{x:?}");
    //~^ pointer_format
    print!("{with_ptr:?}");
    //~^ pointer_format
    let container = ContainsPointerDeep { w: with_ptr };
    print!("{container:?}");
    //~^ pointer_format

    let no_pointer = "foo";
    println!("{no_pointer:?}");
    let manual_debug = ManualDebug { ptr: &arr as _ };
    println!("{manual_debug:?}");
}