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