]> git.lizzy.rs Git - rust.git/blob - src/test/ui/lint/lint-unsafe-code.rs
4ac02b51f62fec0e687cab1a9d75eeec6cac2c60
[rust.git] / src / test / 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 #[export_name = "bar"] fn bar() {} //~ ERROR: declaration of a function with `export_name`
35 #[export_name = "BAR"] static BAR: u32 = 5; //~ ERROR: declaration of a static with `export_name`
36
37 unsafe fn baz() {} //~ ERROR: declaration of an `unsafe` function
38 unsafe trait Foo {} //~ ERROR: declaration of an `unsafe` trait
39 unsafe impl Foo for Bar {} //~ ERROR: implementation of an `unsafe` trait
40
41 trait Baz {
42     unsafe fn baz(&self); //~ ERROR: declaration of an `unsafe` method
43     unsafe fn provided(&self) {} //~ ERROR: implementation of an `unsafe` method
44     unsafe fn provided_override(&self) {} //~ ERROR: implementation of an `unsafe` method
45 }
46
47 impl Baz for Bar {
48     unsafe fn baz(&self) {} //~ ERROR: implementation of an `unsafe` method
49     unsafe fn provided_override(&self) {} //~ ERROR: implementation of an `unsafe` method
50 }
51
52
53 #[allow(unsafe_code)]
54 trait A {
55     unsafe fn allowed_unsafe(&self);
56     unsafe fn allowed_unsafe_provided(&self) {}
57 }
58
59 #[allow(unsafe_code)]
60 impl Baz for Bar2 {
61     unsafe fn baz(&self) {}
62     unsafe fn provided_override(&self) {}
63 }
64
65 impl Baz for Bar3 {
66     #[allow(unsafe_code)]
67     unsafe fn baz(&self) {}
68     unsafe fn provided_override(&self) {} //~ ERROR: implementation of an `unsafe` method
69 }
70
71 #[allow(unsafe_code)]
72 unsafe trait B {
73     fn dummy(&self) {}
74 }
75
76 trait C {
77     #[allow(unsafe_code)]
78     unsafe fn baz(&self);
79     unsafe fn provided(&self) {} //~ ERROR: implementation of an `unsafe` method
80 }
81
82 impl C for Bar {
83     #[allow(unsafe_code)]
84     unsafe fn baz(&self) {}
85     unsafe fn provided(&self) {} //~ ERROR: implementation of an `unsafe` method
86 }
87
88 impl C for Bar2 {
89     unsafe fn baz(&self) {} //~ ERROR: implementation of an `unsafe` method
90 }
91
92 trait D {
93     #[allow(unsafe_code)]
94     unsafe fn unsafe_provided(&self) {}
95 }
96
97 impl D for Bar {}
98
99 fn main() {
100     unsafe {} //~ ERROR: usage of an `unsafe` block
101
102     unsafe_in_macro!()
103 }