]> git.proxmox.com Git - rustc.git/blame - src/test/ui/lint/lint-unsafe-code.rs
New upstream version 1.52.0~beta.3+dfsg1
[rustc.git] / src / test / ui / lint / lint-unsafe-code.rs
CommitLineData
c34b1796
AL
1#![allow(unused_unsafe)]
2#![allow(dead_code)]
3#![deny(unsafe_code)]
4
c34b1796
AL
5struct Bar;
6struct Bar2;
7struct Bar3;
8
9#[allow(unsafe_code)]
10mod allowed_unsafe {
c34b1796
AL
11 fn allowed() { unsafe {} }
12 unsafe fn also_allowed() {}
9346a6ac 13 unsafe trait AllowedUnsafe { }
c34b1796 14 unsafe impl AllowedUnsafe for super::Bar {}
6a06907d
XL
15 #[no_mangle] fn allowed2() {}
16 #[export_name = "foo"] fn allowed3() {}
c34b1796
AL
17}
18
19macro_rules! unsafe_in_macro {
6a06907d
XL
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`
c34b1796 27 unsafe {} //~ ERROR: usage of an `unsafe` block
6a06907d 28 }}
c34b1796
AL
29}
30
6a06907d
XL
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
c34b1796 37unsafe fn baz() {} //~ ERROR: declaration of an `unsafe` function
9346a6ac 38unsafe trait Foo {} //~ ERROR: declaration of an `unsafe` trait
c34b1796
AL
39unsafe impl Foo for Bar {} //~ ERROR: implementation of an `unsafe` trait
40
41trait 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
47impl 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)]
54trait A {
55 unsafe fn allowed_unsafe(&self);
56 unsafe fn allowed_unsafe_provided(&self) {}
57}
58
59#[allow(unsafe_code)]
60impl Baz for Bar2 {
61 unsafe fn baz(&self) {}
62 unsafe fn provided_override(&self) {}
63}
64
65impl 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)]
72unsafe trait B {
73 fn dummy(&self) {}
74}
75
76trait C {
77 #[allow(unsafe_code)]
78 unsafe fn baz(&self);
79 unsafe fn provided(&self) {} //~ ERROR: implementation of an `unsafe` method
80}
81
82impl 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
88impl C for Bar2 {
89 unsafe fn baz(&self) {} //~ ERROR: implementation of an `unsafe` method
90}
91
92trait D {
93 #[allow(unsafe_code)]
94 unsafe fn unsafe_provided(&self) {}
95}
96
97impl D for Bar {}
98
99fn main() {
100 unsafe {} //~ ERROR: usage of an `unsafe` block
101
102 unsafe_in_macro!()
103}