]> git.lizzy.rs Git - rust.git/blob - src/test/compile-fail/lint-unused-unsafe.rs
2bf784faf00eca40f9226bcdb4455d2699dcdf0c
[rust.git] / src / test / compile-fail / lint-unused-unsafe.rs
1 // Copyright 2013 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 // Exercise the unused_unsafe attribute in some positive and negative cases
12
13 #[allow(dead_code)];
14 #[deny(unused_unsafe)];
15 #[allow(deprecated_owned_vector)];
16
17 mod foo {
18     extern {
19         pub fn bar();
20     }
21 }
22
23 fn callback<T>(_f: || -> T) -> T { fail!() }
24 unsafe fn unsf() {}
25
26 fn bad1() { unsafe {} }                  //~ ERROR: unnecessary `unsafe` block
27 fn bad2() { unsafe { bad1() } }          //~ ERROR: unnecessary `unsafe` block
28 unsafe fn bad3() { unsafe {} }           //~ ERROR: unnecessary `unsafe` block
29 fn bad4() { unsafe { callback(||{}) } }  //~ ERROR: unnecessary `unsafe` block
30 unsafe fn bad5() { unsafe { unsf() } }   //~ ERROR: unnecessary `unsafe` block
31 fn bad6() {
32     unsafe {                             // don't put the warning here
33         unsafe {                         //~ ERROR: unnecessary `unsafe` block
34             unsf()
35         }
36     }
37 }
38 unsafe fn bad7() {
39     unsafe {                             //~ ERROR: unnecessary `unsafe` block
40         unsafe {                         //~ ERROR: unnecessary `unsafe` block
41             unsf()
42         }
43     }
44 }
45
46 unsafe fn good0() { unsf() }
47 fn good1() { unsafe { unsf() } }
48 fn good2() {
49     /* bug uncovered when implementing warning about unused unsafe blocks. Be
50        sure that when purity is inherited that the source of the unsafe-ness
51        is tracked correctly */
52     unsafe {
53         unsafe fn what() -> Vec<~str> { fail!() }
54
55         callback(|| {
56             what();
57         });
58     }
59 }
60
61 unsafe fn good3() { foo::bar() }
62 fn good4() { unsafe { foo::bar() } }
63
64 #[allow(unused_unsafe)] fn allowed() { unsafe {} }
65
66 fn main() {}