2017-08-30 17:07:25 +00:00
|
|
|
#![allow(unused)]
|
|
|
|
|
2021-09-08 14:31:47 +00:00
|
|
|
fn main() {}
|
2017-08-15 16:41:59 +00:00
|
|
|
|
|
|
|
fn mut_range_bound_upper() {
|
|
|
|
let mut m = 4;
|
2018-12-09 22:26:16 +00:00
|
|
|
for i in 0..m {
|
|
|
|
m = 5;
|
2023-08-24 19:32:12 +00:00
|
|
|
//~^ ERROR: attempt to mutate range bound within loop
|
|
|
|
//~| NOTE: the range of the loop is unchanged
|
|
|
|
}
|
2017-08-15 16:41:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn mut_range_bound_lower() {
|
|
|
|
let mut m = 4;
|
2018-12-09 22:26:16 +00:00
|
|
|
for i in m..10 {
|
|
|
|
m *= 2;
|
2023-08-24 19:32:12 +00:00
|
|
|
//~^ ERROR: attempt to mutate range bound within loop
|
|
|
|
//~| NOTE: the range of the loop is unchanged
|
|
|
|
}
|
2017-08-15 16:41:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn mut_range_bound_both() {
|
|
|
|
let mut m = 4;
|
|
|
|
let mut n = 6;
|
2018-12-09 22:26:16 +00:00
|
|
|
for i in m..n {
|
|
|
|
m = 5;
|
2023-08-24 19:32:12 +00:00
|
|
|
//~^ ERROR: attempt to mutate range bound within loop
|
|
|
|
//~| NOTE: the range of the loop is unchanged
|
2018-12-09 22:26:16 +00:00
|
|
|
n = 7;
|
2023-08-24 19:32:12 +00:00
|
|
|
//~^ ERROR: attempt to mutate range bound within loop
|
|
|
|
//~| NOTE: the range of the loop is unchanged
|
|
|
|
}
|
2017-09-19 00:10:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn mut_range_bound_no_mutation() {
|
|
|
|
let mut m = 4;
|
2018-12-09 22:26:16 +00:00
|
|
|
for i in 0..m {
|
|
|
|
continue;
|
|
|
|
} // no warning
|
2017-08-15 16:41:59 +00:00
|
|
|
}
|
|
|
|
|
2017-09-19 00:10:33 +00:00
|
|
|
fn mut_borrow_range_bound() {
|
|
|
|
let mut m = 4;
|
|
|
|
for i in 0..m {
|
2023-08-24 19:32:12 +00:00
|
|
|
let n = &mut m;
|
|
|
|
//~^ ERROR: attempt to mutate range bound within loop
|
|
|
|
//~| NOTE: the range of the loop is unchanged
|
2017-09-26 01:32:05 +00:00
|
|
|
*n += 1;
|
2017-09-25 06:00:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn immut_borrow_range_bound() {
|
|
|
|
let mut m = 4;
|
|
|
|
for i in 0..m {
|
2023-08-24 19:32:12 +00:00
|
|
|
let n = &m;
|
2017-09-19 00:10:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-15 16:41:59 +00:00
|
|
|
fn immut_range_bound() {
|
|
|
|
let m = 4;
|
2018-12-09 22:26:16 +00:00
|
|
|
for i in 0..m {
|
|
|
|
continue;
|
|
|
|
} // no warning
|
2017-08-15 16:41:59 +00:00
|
|
|
}
|
2021-09-08 14:31:47 +00:00
|
|
|
|
|
|
|
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 {
|
2023-08-24 19:32:12 +00:00
|
|
|
// 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
|
2021-09-08 14:31:47 +00:00
|
|
|
if m == 4 {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut n = 3;
|
|
|
|
for i in n..10 {
|
|
|
|
if n == 4 {
|
2023-08-24 19:32:12 +00:00
|
|
|
// 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
|
2021-09-08 14:31:47 +00:00
|
|
|
let _ = 2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|