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