]> git.lizzy.rs Git - rust.git/blob - tests/ui/lint/lint-unsafe-code.rs
Auto merge of #106884 - clubby789:fieldless-enum-debug, r=michaelwoerister
[rust.git] / tests / ui / lint / lint-unsafe-code.rs
1 #![allow(unused_unsafe)]
2 #![allow(dead_code)]
3 #![deny(unsafe_code)]
4
5 struct Bar;
6 struct Bar2;
7 struct Bar3;
8
9 #[allow(unsafe_code)]
10 mod allowed_unsafe {
11     fn allowed() { unsafe {} }
12     unsafe fn also_allowed() {}
13     unsafe trait AllowedUnsafe { }
14     unsafe impl AllowedUnsafe for super::Bar {}
15     #[no_mangle] fn allowed2() {}
16     #[export_name = "foo"] fn allowed3() {}
17 }
18
19 macro_rules! unsafe_in_macro {
20     () => {{
21         #[no_mangle] fn foo() {} //~ ERROR: declaration of a `no_mangle` function
22         #[no_mangle] static FOO: u32 = 5; //~ ERROR: declaration of a `no_mangle` static
23         #[export_name = "bar"] fn bar() {}
24         //~^ ERROR: declaration of a function with `export_name`
25         #[export_name = "BAR"] static BAR: u32 = 5;
26         //~^ ERROR: declaration of a static with `export_name`
27         unsafe {} //~ ERROR: usage of an `unsafe` block
28     }}
29 }
30
31 #[no_mangle] fn foo() {} //~ ERROR: declaration of a `no_mangle` function
32 #[no_mangle] static FOO: u32 = 5; //~ ERROR: declaration of a `no_mangle` static
33
34 trait AssocFnTrait {
35     fn foo();
36 }
37
38 struct AssocFnFoo;
39
40 impl AssocFnFoo {
41     #[no_mangle] fn foo() {} //~ ERROR: declaration of a `no_mangle` method
42 }
43
44 impl AssocFnTrait for AssocFnFoo {
45     #[no_mangle] fn foo() {} //~ ERROR: declaration of a `no_mangle` method
46 }
47
48 #[export_name = "bar"] fn bar() {} //~ ERROR: declaration of a function with `export_name`
49 #[export_name = "BAR"] static BAR: u32 = 5; //~ ERROR: declaration of a static with `export_name`
50
51 #[link_section = ".example_section"] fn uwu() {} //~ ERROR: declaration of a function with `link_section`
52 #[link_section = ".example_section"] static UWU: u32 = 5; //~ ERROR: declaration of a static with `link_section`
53
54 struct AssocFnBar;
55
56 impl AssocFnBar {
57     #[export_name = "bar"] fn bar() {} //~ ERROR: declaration of a method with `export_name`
58 }
59
60 impl AssocFnTrait for AssocFnBar {
61     #[export_name = "bar"] fn foo() {} //~ ERROR: declaration of a method with `export_name`
62 }
63
64 unsafe fn baz() {} //~ ERROR: declaration of an `unsafe` function
65 unsafe trait Foo {} //~ ERROR: declaration of an `unsafe` trait
66 unsafe impl Foo for Bar {} //~ ERROR: implementation of an `unsafe` trait
67
68 trait Baz {
69     unsafe fn baz(&self); //~ ERROR: declaration of an `unsafe` method
70     unsafe fn provided(&self) {} //~ ERROR: implementation of an `unsafe` method
71     unsafe fn provided_override(&self) {} //~ ERROR: implementation of an `unsafe` method
72 }
73
74 impl Baz for Bar {
75     unsafe fn baz(&self) {} //~ ERROR: implementation of an `unsafe` method
76     unsafe fn provided_override(&self) {} //~ ERROR: implementation of an `unsafe` method
77 }
78
79
80 #[allow(unsafe_code)]
81 trait A {
82     unsafe fn allowed_unsafe(&self);
83     unsafe fn allowed_unsafe_provided(&self) {}
84 }
85
86 #[allow(unsafe_code)]
87 impl Baz for Bar2 {
88     unsafe fn baz(&self) {}
89     unsafe fn provided_override(&self) {}
90 }
91
92 impl Baz for Bar3 {
93     #[allow(unsafe_code)]
94     unsafe fn baz(&self) {}
95     unsafe fn provided_override(&self) {} //~ ERROR: implementation of an `unsafe` method
96 }
97
98 #[allow(unsafe_code)]
99 unsafe trait B {
100     fn dummy(&self) {}
101 }
102
103 trait C {
104     #[allow(unsafe_code)]
105     unsafe fn baz(&self);
106     unsafe fn provided(&self) {} //~ ERROR: implementation of an `unsafe` method
107 }
108
109 impl C for Bar {
110     #[allow(unsafe_code)]
111     unsafe fn baz(&self) {}
112     unsafe fn provided(&self) {} //~ ERROR: implementation of an `unsafe` method
113 }
114
115 impl C for Bar2 {
116     unsafe fn baz(&self) {} //~ ERROR: implementation of an `unsafe` method
117 }
118
119 trait D {
120     #[allow(unsafe_code)]
121     unsafe fn unsafe_provided(&self) {}
122 }
123
124 impl D for Bar {}
125
126 fn main() {
127     unsafe {} //~ ERROR: usage of an `unsafe` block
128
129     unsafe_in_macro!()
130 }