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