blob: 7aebbf4981ee855fbb8cce0b4d512675b9778edf (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
#![allow(unused)]
fn main() {}
fn mut_range_bound_upper() {
let mut m = 4;
for i in 0..m {
m = 5;
//~^ ERROR: attempt to mutate range bound within loop
//~| NOTE: the range of the loop is unchanged
}
}
fn mut_range_bound_lower() {
let mut m = 4;
for i in m..10 {
m *= 2;
//~^ ERROR: attempt to mutate range bound within loop
//~| NOTE: the range of the loop is unchanged
}
}
fn mut_range_bound_both() {
let mut m = 4;
let mut n = 6;
for i in m..n {
m = 5;
//~^ ERROR: attempt to mutate range bound within loop
//~| NOTE: the range of the loop is unchanged
n = 7;
//~^ ERROR: attempt to mutate range bound within loop
//~| NOTE: the range of the loop is unchanged
}
}
fn mut_range_bound_no_mutation() {
let mut m = 4;
for i in 0..m {
continue;
} // no warning
}
fn mut_borrow_range_bound() {
let mut m = 4;
for i in 0..m {
let n = &mut m;
//~^ ERROR: attempt to mutate range bound within loop
//~| NOTE: the range of the loop is unchanged
*n += 1;
}
}
fn immut_borrow_range_bound() {
let mut m = 4;
for i in 0..m {
let n = &m;
}
}
fn immut_range_bound() {
let m = 4;
for i in 0..m {
continue;
} // no warning
}
fn mut_range_bound_break() {
let mut m = 4;
for i in 0..m {
if m == 4 {
m = 5; // no warning because of immediate break
break;
}
}
}
fn mut_range_bound_no_immediate_break() {
let mut m = 4;
for i in 0..m {
// warning because it is not immediately followed by break
m = 2;
//~^ ERROR: attempt to mutate range bound within loop
//~| NOTE: the range of the loop is unchanged
if m == 4 {
break;
}
}
let mut n = 3;
for i in n..10 {
if n == 4 {
// FIXME: warning because it is not immediately followed by break
n = 1;
//~^ ERROR: attempt to mutate range bound within loop
//~| NOTE: the range of the loop is unchanged
let _ = 2;
break;
}
}
}
|