]> git.proxmox.com Git - rustc.git/blob - src/test/compile-fail/liveness-unused.rs
Imported Upstream version 1.9.0+dfsg1
[rustc.git] / src / test / compile-fail / liveness-unused.rs
1 // Copyright 2014 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 #![deny(unused_variables)]
12 #![deny(unused_assignments)]
13 #![allow(dead_code, non_camel_case_types, trivial_numeric_casts)]
14
15 use std::ops::AddAssign;
16
17 fn f1(x: isize) {
18 //~^ ERROR unused variable: `x`
19 }
20
21 fn f1b(x: &mut isize) {
22 //~^ ERROR unused variable: `x`
23 }
24
25 #[allow(unused_variables)]
26 fn f1c(x: isize) {}
27
28 fn f1d() {
29 let x: isize;
30 //~^ ERROR unused variable: `x`
31 }
32
33 fn f2() {
34 let x = 3;
35 //~^ ERROR unused variable: `x`
36 }
37
38 fn f3() {
39 let mut x = 3;
40 //~^ ERROR variable `x` is assigned to, but never used
41 x += 4;
42 //~^ ERROR value assigned to `x` is never read
43 }
44
45 fn f3b() {
46 let mut z = 3;
47 //~^ ERROR variable `z` is assigned to, but never used
48 loop {
49 z += 4;
50 }
51 }
52
53 #[allow(unused_variables)]
54 fn f3c() {
55 let mut z = 3;
56 loop { z += 4; }
57 }
58
59 #[allow(unused_variables)]
60 #[allow(unused_assignments)]
61 fn f3d() {
62 let mut x = 3;
63 x += 4;
64 }
65
66 fn f4() {
67 match Some(3) {
68 Some(i) => {
69 //~^ ERROR unused variable: `i`
70 }
71 None => {}
72 }
73 }
74
75 enum tri {
76 a(isize), b(isize), c(isize)
77 }
78
79 fn f4b() -> isize {
80 match tri::a(3) {
81 tri::a(i) | tri::b(i) | tri::c(i) => {
82 i
83 }
84 }
85 }
86
87 fn f5a() {
88 for x in 1..10 { }
89 //~^ ERROR unused variable: `x`
90 }
91
92 fn f5b() {
93 for (x, _) in [1, 2, 3].iter().enumerate() { }
94 //~^ ERROR unused variable: `x`
95 }
96
97 fn f5c() {
98 for (_, x) in [1, 2, 3].iter().enumerate() {
99 //~^ ERROR unused variable: `x`
100 continue;
101 drop(*x as i32); //~ WARNING unreachable statement
102 }
103 }
104
105 struct View<'a>(&'a mut [i32]);
106
107 impl<'a> AddAssign<i32> for View<'a> {
108 fn add_assign(&mut self, rhs: i32) {
109 for lhs in self.0.iter_mut() {
110 *lhs += rhs;
111 }
112 }
113 }
114
115 fn f6() {
116 let mut array = [1, 2, 3];
117 let mut v = View(&mut array);
118
119 // ensure an error shows up for x even if lhs of an overloaded add assign
120
121 let x;
122 //~^ ERROR variable `x` is assigned to, but never used
123
124 *({
125 x = 0; //~ ERROR value assigned to `x` is never read
126 &mut v
127 }) += 1;
128 }
129
130
131 struct MutRef<'a>(&'a mut i32);
132
133 impl<'a> AddAssign<i32> for MutRef<'a> {
134 fn add_assign(&mut self, rhs: i32) {
135 *self.0 += rhs;
136 }
137 }
138
139 fn f7() {
140 let mut a = 1;
141 {
142 // `b` does not trigger unused_variables
143 let mut b = MutRef(&mut a);
144 b += 1;
145 }
146 drop(a);
147 }
148
149 fn main() {
150 }