]> git.lizzy.rs Git - rust.git/blob - src/test/compile-fail/lint-dead-code-1.rs
b68a2241e1a39a9dc63d6d1fea0e4f86e386b729
[rust.git] / src / test / compile-fail / lint-dead-code-1.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 #![no_std]
12 #![allow(unused_variable)]
13 #![allow(non_camel_case_types)]
14 #![allow(visible_private_types)]
15 #![deny(dead_code)]
16 #![feature(lang_items)]
17
18 #![crate_type="lib"]
19
20 pub use foo2::Bar2;
21
22 #[lang="sized"]
23 pub trait Sized {}
24
25 mod foo {
26     pub struct Bar; //~ ERROR: code is never used
27 }
28
29 mod foo2 {
30     pub struct Bar2;
31 }
32
33 pub static pub_static: int = 0;
34 static priv_static: int = 0; //~ ERROR: code is never used
35 static used_static: int = 0;
36 pub static used_static2: int = used_static;
37 static USED_STATIC: int = 0;
38 static STATIC_USED_IN_ENUM_DISCRIMINANT: uint = 10;
39
40 pub type typ = *UsedStruct4;
41 pub struct PubStruct;
42 struct PrivStruct; //~ ERROR: code is never used
43 struct UsedStruct1 {
44     #[allow(dead_code)]
45     x: int
46 }
47 struct UsedStruct2(int);
48 struct UsedStruct3;
49 struct UsedStruct4;
50 // this struct is never used directly, but its method is, so we don't want
51 // to warn it
52 struct SemiUsedStruct;
53 impl SemiUsedStruct {
54     fn la_la_la() {}
55 }
56 struct StructUsedAsField;
57 struct StructUsedInEnum;
58 struct StructUsedInGeneric;
59 pub struct PubStruct2 {
60     #[allow(dead_code)]
61     struct_used_as_field: *StructUsedAsField
62 }
63
64 pub enum pub_enum { foo1, bar1 }
65 pub enum pub_enum2 { a(*StructUsedInEnum) }
66 pub enum pub_enum3 { Foo = STATIC_USED_IN_ENUM_DISCRIMINANT }
67 enum priv_enum { foo2, bar2 } //~ ERROR: code is never used
68 enum used_enum { foo3, bar3 }
69
70 fn f<T>() {}
71
72 pub fn pub_fn() {
73     used_fn();
74     let used_struct1 = UsedStruct1 { x: 1 };
75     let used_struct2 = UsedStruct2(1);
76     let used_struct3 = UsedStruct3;
77     let e = foo3;
78     SemiUsedStruct::la_la_la();
79
80     let i = 1;
81     match i {
82         USED_STATIC => (),
83         _ => ()
84     }
85     f::<StructUsedInGeneric>();
86 }
87 fn priv_fn() { //~ ERROR: code is never used
88     let unused_struct = PrivStruct;
89 }
90 fn used_fn() {}
91
92 fn foo() { //~ ERROR: code is never used
93     bar();
94     let unused_enum = foo2;
95 }
96
97 fn bar() { //~ ERROR: code is never used
98     foo();
99 }
100
101 // Code with #[allow(dead_code)] should be marked live (and thus anything it
102 // calls is marked live)
103 #[allow(dead_code)]
104 fn g() { h(); }
105 fn h() {}
106
107 // Similarly, lang items are live
108 #[lang="fail_"]
109 fn fail(_: *u8, _: *u8, _: uint) -> ! { loop {} }