]> git.lizzy.rs Git - rust.git/blob - src/test/ui/lint/lint-unsafe-code.rs
Adjust `#[no_mangle]`-related checks and lints for `impl` items
[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 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 struct AssocFnBar;
52
53 impl AssocFnBar {
54     #[export_name = "bar"] fn bar() {} //~ ERROR: declaration of a method with `export_name`
55 }
56
57 impl AssocFnTrait for AssocFnBar {
58     #[export_name = "bar"] fn foo() {} //~ ERROR: declaration of a method with `export_name`
59 }
60
61 unsafe fn baz() {} //~ ERROR: declaration of an `unsafe` function
62 unsafe trait Foo {} //~ ERROR: declaration of an `unsafe` trait
63 unsafe impl Foo for Bar {} //~ ERROR: implementation of an `unsafe` trait
64
65 trait Baz {
66     unsafe fn baz(&self); //~ ERROR: declaration of an `unsafe` method
67     unsafe fn provided(&self) {} //~ ERROR: implementation of an `unsafe` method
68     unsafe fn provided_override(&self) {} //~ ERROR: implementation of an `unsafe` method
69 }
70
71 impl Baz for Bar {
72     unsafe fn baz(&self) {} //~ ERROR: implementation of an `unsafe` method
73     unsafe fn provided_override(&self) {} //~ ERROR: implementation of an `unsafe` method
74 }
75
76
77 #[allow(unsafe_code)]
78 trait A {
79     unsafe fn allowed_unsafe(&self);
80     unsafe fn allowed_unsafe_provided(&self) {}
81 }
82
83 #[allow(unsafe_code)]
84 impl Baz for Bar2 {
85     unsafe fn baz(&self) {}
86     unsafe fn provided_override(&self) {}
87 }
88
89 impl Baz for Bar3 {
90     #[allow(unsafe_code)]
91     unsafe fn baz(&self) {}
92     unsafe fn provided_override(&self) {} //~ ERROR: implementation of an `unsafe` method
93 }
94
95 #[allow(unsafe_code)]
96 unsafe trait B {
97     fn dummy(&self) {}
98 }
99
100 trait C {
101     #[allow(unsafe_code)]
102     unsafe fn baz(&self);
103     unsafe fn provided(&self) {} //~ ERROR: implementation of an `unsafe` method
104 }
105
106 impl C for Bar {
107     #[allow(unsafe_code)]
108     unsafe fn baz(&self) {}
109     unsafe fn provided(&self) {} //~ ERROR: implementation of an `unsafe` method
110 }
111
112 impl C for Bar2 {
113     unsafe fn baz(&self) {} //~ ERROR: implementation of an `unsafe` method
114 }
115
116 trait D {
117     #[allow(unsafe_code)]
118     unsafe fn unsafe_provided(&self) {}
119 }
120
121 impl D for Bar {}
122
123 fn main() {
124     unsafe {} //~ ERROR: usage of an `unsafe` block
125
126     unsafe_in_macro!()
127 }