]> git.lizzy.rs Git - rust.git/blob - src/test/ui/liveness/liveness-unused.rs
Rollup merge of #55247 - peterjoel:peterjoel-prim-char-doc-example, r=joshtriplett
[rust.git] / src / test / ui / liveness / 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 #![warn(unused)]
12 #![deny(unused_variables)]
13 #![deny(unused_assignments)]
14 #![allow(dead_code, non_camel_case_types, trivial_numeric_casts)]
15
16 use std::ops::AddAssign;
17
18 fn f1(x: isize) {
19     //~^ ERROR unused variable: `x`
20 }
21
22 fn f1b(x: &mut isize) {
23     //~^ ERROR unused variable: `x`
24 }
25
26 #[allow(unused_variables)]
27 fn f1c(x: isize) {}
28
29 fn f1d() {
30     let x: isize;
31     //~^ ERROR unused variable: `x`
32 }
33
34 fn f2() {
35     let x = 3;
36     //~^ ERROR unused variable: `x`
37 }
38
39 fn f3() {
40     let mut x = 3;
41     //~^ ERROR variable `x` is assigned to, but never used
42     x += 4;
43     //~^ ERROR value assigned to `x` is never read
44 }
45
46 fn f3b() {
47     let mut z = 3;
48     //~^ ERROR variable `z` is assigned to, but never used
49     loop {
50         z += 4;
51     }
52 }
53
54 #[allow(unused_variables)]
55 fn f3c() {
56     let mut z = 3;
57     loop { z += 4; }
58 }
59
60 #[allow(unused_variables)]
61 #[allow(unused_assignments)]
62 fn f3d() {
63     let mut x = 3;
64     x += 4;
65 }
66
67 fn f4() {
68     match Some(3) {
69       Some(i) => {
70         //~^ ERROR unused variable: `i`
71       }
72       None => {}
73     }
74 }
75
76 enum tri {
77     a(isize), b(isize), c(isize)
78 }
79
80 fn f4b() -> isize {
81     match tri::a(3) {
82       tri::a(i) | tri::b(i) | tri::c(i) => {
83         i
84       }
85     }
86 }
87
88 fn f5a() {
89     for x in 1..10 { }
90     //~^ ERROR unused variable: `x`
91 }
92
93 fn f5b() {
94     for (x, _) in [1, 2, 3].iter().enumerate() { }
95     //~^ ERROR unused variable: `x`
96 }
97
98 fn f5c() {
99     for (_, x) in [1, 2, 3].iter().enumerate() {
100     //~^ ERROR unused variable: `x`
101         continue;
102         drop(*x as i32); //~ WARNING unreachable statement
103     }
104 }
105
106 struct View<'a>(&'a mut [i32]);
107
108 impl<'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
116 fn 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
132 struct MutRef<'a>(&'a mut i32);
133
134 impl<'a> AddAssign<i32> for MutRef<'a> {
135     fn add_assign(&mut self, rhs: i32) {
136         *self.0 += rhs;
137     }
138 }
139
140 fn 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
150 fn main() {
151 }