]> git.lizzy.rs Git - rust.git/blob - src/test/ui/lint/lint-dead-code-4.rs
Move parse-fail tests to UI
[rust.git] / src / test / ui / lint / lint-dead-code-4.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 #![allow(unused_variables)]
12 #![allow(non_camel_case_types)]
13 #![deny(dead_code)]
14
15 struct Foo {
16     x: usize,
17     b: bool, //~ ERROR: field is never used
18 }
19
20 fn field_read(f: Foo) -> usize {
21     f.x.pow(2)
22 }
23
24 enum XYZ {
25     X, //~ ERROR variant is never constructed
26     Y { //~ ERROR variant is never constructed
27         a: String,
28         b: i32,
29         c: i32,
30     },
31     Z
32 }
33
34 enum ABC { //~ ERROR enum is never used
35     A,
36     B {
37         a: String,
38         b: i32,
39         c: i32,
40     },
41     C
42 }
43
44 // ensure struct variants get warning for their fields
45 enum IJK {
46     I, //~ ERROR variant is never constructed
47     J {
48         a: String,
49         b: i32, //~ ERROR field is never used
50         c: i32, //~ ERROR field is never used
51     },
52     K //~ ERROR variant is never constructed
53
54 }
55
56 fn struct_variant_partial_use(b: IJK) -> String {
57     match b {
58         IJK::J { a, b: _, .. } => a,
59         _ => "".to_string()
60     }
61 }
62
63 fn field_match_in_patterns(b: XYZ) -> String {
64     match b {
65         XYZ::Y { a, b: _, .. } => a,
66         _ => "".to_string()
67     }
68 }
69
70 struct Bar {
71     x: usize, //~ ERROR: field is never used
72     b: bool,
73     c: bool, //~ ERROR: field is never used
74     _guard: ()
75 }
76
77 #[repr(C)]
78 struct Baz {
79     x: u32,
80 }
81
82 fn field_match_in_let(f: Bar) -> bool {
83     let Bar { b, c: _, .. } = f;
84     b
85 }
86
87 fn main() {
88     field_read(Foo { x: 1, b: false });
89     field_match_in_patterns(XYZ::Z);
90     struct_variant_partial_use(IJK::J { a: "".into(), b: 1, c: -1 });
91     field_match_in_let(Bar { x: 42, b: true, c: false, _guard: () });
92     let _ = Baz { x: 0 };
93 }