summaryrefslogtreecommitdiffstats
path: root/src/test/ui/lint/lint-unsafe-code.rs
blob: b72e4c3a9e7fb6f8ecce2d97128ccd6257c8c030 (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#![allow(unused_unsafe)]
#![allow(dead_code)]
#![deny(unsafe_code)]

struct Bar;
struct Bar2;
struct Bar3;

#[allow(unsafe_code)]
mod allowed_unsafe {
    fn allowed() { unsafe {} }
    unsafe fn also_allowed() {}
    unsafe trait AllowedUnsafe { }
    unsafe impl AllowedUnsafe for super::Bar {}
    #[no_mangle] fn allowed2() {}
    #[export_name = "foo"] fn allowed3() {}
}

macro_rules! unsafe_in_macro {
    () => {{
        #[no_mangle] fn foo() {} //~ ERROR: declaration of a `no_mangle` function
        #[no_mangle] static FOO: u32 = 5; //~ ERROR: declaration of a `no_mangle` static
        #[export_name = "bar"] fn bar() {}
        //~^ ERROR: declaration of a function with `export_name`
        #[export_name = "BAR"] static BAR: u32 = 5;
        //~^ ERROR: declaration of a static with `export_name`
        unsafe {} //~ ERROR: usage of an `unsafe` block
    }}
}

#[no_mangle] fn foo() {} //~ ERROR: declaration of a `no_mangle` function
#[no_mangle] static FOO: u32 = 5; //~ ERROR: declaration of a `no_mangle` static

trait AssocFnTrait {
    fn foo();
}

struct AssocFnFoo;

impl AssocFnFoo {
    #[no_mangle] fn foo() {} //~ ERROR: declaration of a `no_mangle` method
}

impl AssocFnTrait for AssocFnFoo {
    #[no_mangle] fn foo() {} //~ ERROR: declaration of a `no_mangle` method
}

#[export_name = "bar"] fn bar() {} //~ ERROR: declaration of a function with `export_name`
#[export_name = "BAR"] static BAR: u32 = 5; //~ ERROR: declaration of a static with `export_name`

#[link_section = ".example_section"] fn uwu() {} //~ ERROR: declaration of a function with `link_section`
#[link_section = ".example_section"] static UWU: u32 = 5; //~ ERROR: declaration of a static with `link_section`

struct AssocFnBar;

impl AssocFnBar {
    #[export_name = "bar"] fn bar() {} //~ ERROR: declaration of a method with `export_name`
}

impl AssocFnTrait for AssocFnBar {
    #[export_name = "bar"] fn foo() {} //~ ERROR: declaration of a method with `export_name`
}

unsafe fn baz() {} //~ ERROR: declaration of an `unsafe` function
unsafe trait Foo {} //~ ERROR: declaration of an `unsafe` trait
unsafe impl Foo for Bar {} //~ ERROR: implementation of an `unsafe` trait

trait Baz {
    unsafe fn baz(&self); //~ ERROR: declaration of an `unsafe` method
    unsafe fn provided(&self) {} //~ ERROR: implementation of an `unsafe` method
    unsafe fn provided_override(&self) {} //~ ERROR: implementation of an `unsafe` method
}

impl Baz for Bar {
    unsafe fn baz(&self) {} //~ ERROR: implementation of an `unsafe` method
    unsafe fn provided_override(&self) {} //~ ERROR: implementation of an `unsafe` method
}


#[allow(unsafe_code)]
trait A {
    unsafe fn allowed_unsafe(&self);
    unsafe fn allowed_unsafe_provided(&self) {}
}

#[allow(unsafe_code)]
impl Baz for Bar2 {
    unsafe fn baz(&self) {}
    unsafe fn provided_override(&self) {}
}

impl Baz for Bar3 {
    #[allow(unsafe_code)]
    unsafe fn baz(&self) {}
    unsafe fn provided_override(&self) {} //~ ERROR: implementation of an `unsafe` method
}

#[allow(unsafe_code)]
unsafe trait B {
    fn dummy(&self) {}
}

trait C {
    #[allow(unsafe_code)]
    unsafe fn baz(&self);
    unsafe fn provided(&self) {} //~ ERROR: implementation of an `unsafe` method
}

impl C for Bar {
    #[allow(unsafe_code)]
    unsafe fn baz(&self) {}
    unsafe fn provided(&self) {} //~ ERROR: implementation of an `unsafe` method
}

impl C for Bar2 {
    unsafe fn baz(&self) {} //~ ERROR: implementation of an `unsafe` method
}

trait D {
    #[allow(unsafe_code)]
    unsafe fn unsafe_provided(&self) {}
}

impl D for Bar {}

fn main() {
    unsafe {} //~ ERROR: usage of an `unsafe` block

    unsafe_in_macro!()
}