]> git.proxmox.com Git - rustc.git/blame - src/test/compile-fail/liveness-unused.rs
Imported Upstream version 1.9.0+dfsg1
[rustc.git] / src / test / compile-fail / liveness-unused.rs
CommitLineData
1a4d82fc 1// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
223e47cc
LB
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
1a4d82fc
JJ
11#![deny(unused_variables)]
12#![deny(unused_assignments)]
c34b1796 13#![allow(dead_code, non_camel_case_types, trivial_numeric_casts)]
970d7e83 14
54a0048b
SL
15use std::ops::AddAssign;
16
1a4d82fc 17fn f1(x: isize) {
970d7e83 18 //~^ ERROR unused variable: `x`
223e47cc
LB
19}
20
1a4d82fc 21fn f1b(x: &mut isize) {
970d7e83 22 //~^ ERROR unused variable: `x`
223e47cc
LB
23}
24
1a4d82fc
JJ
25#[allow(unused_variables)]
26fn f1c(x: isize) {}
27
28fn f1d() {
29 let x: isize;
30 //~^ ERROR unused variable: `x`
31}
970d7e83 32
223e47cc 33fn f2() {
85aaf69f 34 let x = 3;
970d7e83 35 //~^ ERROR unused variable: `x`
223e47cc
LB
36}
37
38fn f3() {
85aaf69f 39 let mut x = 3;
970d7e83 40 //~^ ERROR variable `x` is assigned to, but never used
85aaf69f 41 x += 4;
970d7e83 42 //~^ ERROR value assigned to `x` is never read
223e47cc
LB
43}
44
45fn f3b() {
85aaf69f 46 let mut z = 3;
970d7e83 47 //~^ ERROR variable `z` is assigned to, but never used
223e47cc 48 loop {
85aaf69f 49 z += 4;
223e47cc
LB
50 }
51}
52
1a4d82fc 53#[allow(unused_variables)]
970d7e83 54fn f3c() {
85aaf69f
SL
55 let mut z = 3;
56 loop { z += 4; }
970d7e83
LB
57}
58
1a4d82fc
JJ
59#[allow(unused_variables)]
60#[allow(unused_assignments)]
970d7e83 61fn f3d() {
85aaf69f
SL
62 let mut x = 3;
63 x += 4;
970d7e83
LB
64}
65
223e47cc 66fn f4() {
85aaf69f 67 match Some(3) {
223e47cc 68 Some(i) => {
970d7e83 69 //~^ ERROR unused variable: `i`
223e47cc
LB
70 }
71 None => {}
72 }
73}
74
75enum tri {
1a4d82fc 76 a(isize), b(isize), c(isize)
223e47cc
LB
77}
78
1a4d82fc 79fn f4b() -> isize {
85aaf69f 80 match tri::a(3) {
1a4d82fc 81 tri::a(i) | tri::b(i) | tri::c(i) => {
223e47cc
LB
82 i
83 }
84 }
85}
86
1a4d82fc 87fn f5a() {
85aaf69f 88 for x in 1..10 { }
1a4d82fc
JJ
89 //~^ ERROR unused variable: `x`
90}
91
92fn f5b() {
85aaf69f 93 for (x, _) in [1, 2, 3].iter().enumerate() { }
1a4d82fc
JJ
94 //~^ ERROR unused variable: `x`
95}
96
97fn f5c() {
85aaf69f 98 for (_, x) in [1, 2, 3].iter().enumerate() {
1a4d82fc
JJ
99 //~^ ERROR unused variable: `x`
100 continue;
85aaf69f 101 drop(*x as i32); //~ WARNING unreachable statement
1a4d82fc
JJ
102 }
103}
104
54a0048b
SL
105struct View<'a>(&'a mut [i32]);
106
107impl<'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
115fn 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
131struct MutRef<'a>(&'a mut i32);
132
133impl<'a> AddAssign<i32> for MutRef<'a> {
134 fn add_assign(&mut self, rhs: i32) {
135 *self.0 += rhs;
136 }
137}
138
139fn 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
223e47cc 149fn main() {
223e47cc 150}