]> git.lizzy.rs Git - rust.git/blob - src/test/ui/lint/lint-unsafe-code.rs
Move parse-fail tests to UI
[rust.git] / src / test / ui / lint / lint-unsafe-code.rs
1 // Copyright 2013-2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 #![allow(unused_unsafe)]
12 #![allow(dead_code)]
13 #![deny(unsafe_code)]
14
15 struct Bar;
16 struct Bar2;
17 struct Bar3;
18
19 #[allow(unsafe_code)]
20 mod allowed_unsafe {
21     fn allowed() { unsafe {} }
22     unsafe fn also_allowed() {}
23     unsafe trait AllowedUnsafe { }
24     unsafe impl AllowedUnsafe for super::Bar {}
25 }
26
27 macro_rules! unsafe_in_macro {
28     () => {
29         unsafe {} //~ ERROR: usage of an `unsafe` block
30     }
31 }
32
33 unsafe fn baz() {} //~ ERROR: declaration of an `unsafe` function
34 unsafe trait Foo {} //~ ERROR: declaration of an `unsafe` trait
35 unsafe impl Foo for Bar {} //~ ERROR: implementation of an `unsafe` trait
36
37 trait Baz {
38     unsafe fn baz(&self); //~ ERROR: declaration of an `unsafe` method
39     unsafe fn provided(&self) {} //~ ERROR: implementation of an `unsafe` method
40     unsafe fn provided_override(&self) {} //~ ERROR: implementation of an `unsafe` method
41 }
42
43 impl Baz for Bar {
44     unsafe fn baz(&self) {} //~ ERROR: implementation of an `unsafe` method
45     unsafe fn provided_override(&self) {} //~ ERROR: implementation of an `unsafe` method
46 }
47
48
49 #[allow(unsafe_code)]
50 trait A {
51     unsafe fn allowed_unsafe(&self);
52     unsafe fn allowed_unsafe_provided(&self) {}
53 }
54
55 #[allow(unsafe_code)]
56 impl Baz for Bar2 {
57     unsafe fn baz(&self) {}
58     unsafe fn provided_override(&self) {}
59 }
60
61 impl Baz for Bar3 {
62     #[allow(unsafe_code)]
63     unsafe fn baz(&self) {}
64     unsafe fn provided_override(&self) {} //~ ERROR: implementation of an `unsafe` method
65 }
66
67 #[allow(unsafe_code)]
68 unsafe trait B {
69     fn dummy(&self) {}
70 }
71
72 trait C {
73     #[allow(unsafe_code)]
74     unsafe fn baz(&self);
75     unsafe fn provided(&self) {} //~ ERROR: implementation of an `unsafe` method
76 }
77
78 impl C for Bar {
79     #[allow(unsafe_code)]
80     unsafe fn baz(&self) {}
81     unsafe fn provided(&self) {} //~ ERROR: implementation of an `unsafe` method
82 }
83
84 impl C for Bar2 {
85     unsafe fn baz(&self) {} //~ ERROR: implementation of an `unsafe` method
86 }
87
88 trait D {
89     #[allow(unsafe_code)]
90     unsafe fn unsafe_provided(&self) {}
91 }
92
93 impl D for Bar {}
94
95 fn main() {
96     unsafe {} //~ ERROR: usage of an `unsafe` block
97
98     unsafe_in_macro!()
99 }