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