2023-07-27 11:40:22 +00:00
|
|
|
//@no-rustfix
|
|
|
|
|
2018-12-09 22:26:16 +00:00
|
|
|
fn fn_val(i: i32) -> i32 {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
fn fn_constref(i: &i32) -> i32 {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
fn fn_mutref(i: &mut i32) {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
fn fooi() -> i32 {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
fn foob() -> bool {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
2018-02-25 17:25:31 +00:00
|
|
|
|
|
|
|
fn immutable_condition() {
|
2018-07-25 18:02:52 +00:00
|
|
|
// Should warn when all vars mentioned are immutable
|
2018-02-25 17:25:31 +00:00
|
|
|
let y = 0;
|
|
|
|
while y < 10 {
|
|
|
|
println!("KO - y is immutable");
|
|
|
|
}
|
|
|
|
|
|
|
|
let x = 0;
|
|
|
|
while y < 10 && x < 3 {
|
2018-03-01 22:23:41 +00:00
|
|
|
let mut k = 1;
|
|
|
|
k += 2;
|
2018-02-25 17:25:31 +00:00
|
|
|
println!("KO - x and y immutable");
|
|
|
|
}
|
|
|
|
|
|
|
|
let cond = false;
|
|
|
|
while !cond {
|
|
|
|
println!("KO - cond immutable");
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut i = 0;
|
|
|
|
while y < 10 && i < 3 {
|
|
|
|
i += 1;
|
|
|
|
println!("OK - i is mutable");
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut mut_cond = false;
|
|
|
|
while !mut_cond || cond {
|
|
|
|
mut_cond = true;
|
|
|
|
println!("OK - mut_cond is mutable");
|
|
|
|
}
|
|
|
|
|
2018-03-01 22:23:41 +00:00
|
|
|
while fooi() < x {
|
|
|
|
println!("OK - Fn call results may vary");
|
|
|
|
}
|
|
|
|
|
|
|
|
while foob() {
|
2018-02-25 17:25:31 +00:00
|
|
|
println!("OK - Fn call results may vary");
|
|
|
|
}
|
|
|
|
|
2018-03-25 15:23:31 +00:00
|
|
|
let mut a = 0;
|
|
|
|
let mut c = move || {
|
|
|
|
while a < 5 {
|
|
|
|
a += 1;
|
|
|
|
println!("OK - a is mutable");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
c();
|
2018-04-03 14:41:30 +00:00
|
|
|
|
|
|
|
let mut tup = (0, 0);
|
|
|
|
while tup.0 < 5 {
|
|
|
|
tup.0 += 1;
|
|
|
|
println!("OK - tup.0 gets mutated")
|
|
|
|
}
|
2018-02-25 17:25:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn unused_var() {
|
|
|
|
// Should warn when a (mutable) var is not used in while body
|
|
|
|
let (mut i, mut j) = (0, 0);
|
|
|
|
|
|
|
|
while i < 3 {
|
|
|
|
j = 3;
|
2018-07-25 18:02:52 +00:00
|
|
|
println!("KO - i not mentioned");
|
2018-02-25 17:25:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
while i < 3 && j > 0 {
|
2018-07-25 18:02:52 +00:00
|
|
|
println!("KO - i and j not mentioned");
|
2018-02-25 17:25:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
while i < 3 {
|
|
|
|
let mut i = 5;
|
|
|
|
fn_mutref(&mut i);
|
|
|
|
println!("KO - shadowed");
|
|
|
|
}
|
|
|
|
|
|
|
|
while i < 3 && j > 0 {
|
|
|
|
i = 5;
|
2018-07-25 18:02:52 +00:00
|
|
|
println!("OK - i in cond and mentioned");
|
2018-02-25 17:25:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn used_immutable() {
|
|
|
|
let mut i = 0;
|
|
|
|
|
|
|
|
while i < 3 {
|
|
|
|
fn_constref(&i);
|
|
|
|
println!("KO - const reference");
|
|
|
|
}
|
|
|
|
|
|
|
|
while i < 3 {
|
|
|
|
fn_val(i);
|
|
|
|
println!("KO - passed by value");
|
|
|
|
}
|
|
|
|
|
|
|
|
while i < 3 {
|
|
|
|
println!("OK - passed by mutable reference");
|
2018-03-01 21:00:43 +00:00
|
|
|
fn_mutref(&mut i)
|
2018-02-25 17:25:31 +00:00
|
|
|
}
|
2018-03-01 22:23:41 +00:00
|
|
|
|
|
|
|
while i < 3 {
|
|
|
|
fn_mutref(&mut i);
|
|
|
|
println!("OK - passed by mutable reference");
|
|
|
|
}
|
2018-02-25 17:25:31 +00:00
|
|
|
}
|
|
|
|
|
2018-03-07 17:24:36 +00:00
|
|
|
const N: i32 = 5;
|
|
|
|
const B: bool = false;
|
|
|
|
|
|
|
|
fn consts() {
|
|
|
|
while false {
|
|
|
|
println!("Constants are not linted");
|
|
|
|
}
|
|
|
|
|
|
|
|
while B {
|
|
|
|
println!("Constants are not linted");
|
|
|
|
}
|
|
|
|
|
|
|
|
while N > 0 {
|
|
|
|
println!("Constants are not linted");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-06 17:27:11 +00:00
|
|
|
use std::cell::Cell;
|
|
|
|
|
2018-12-09 22:26:16 +00:00
|
|
|
fn maybe_i_mutate(i: &Cell<bool>) {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
2018-03-06 17:27:11 +00:00
|
|
|
|
|
|
|
fn internally_mutable() {
|
|
|
|
let b = Cell::new(true);
|
|
|
|
|
2018-12-09 22:26:16 +00:00
|
|
|
while b.get() {
|
|
|
|
// b cannot be silently coerced to `bool`
|
2018-03-06 17:27:11 +00:00
|
|
|
maybe_i_mutate(&b);
|
|
|
|
println!("OK - Method call within condition");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-26 18:37:34 +00:00
|
|
|
struct Counter {
|
|
|
|
count: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Counter {
|
|
|
|
fn inc(&mut self) {
|
|
|
|
self.count += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn inc_n(&mut self, n: usize) {
|
|
|
|
while self.count < n {
|
|
|
|
self.inc();
|
|
|
|
}
|
|
|
|
println!("OK - self borrowed mutably");
|
|
|
|
}
|
|
|
|
|
|
|
|
fn print_n(&self, n: usize) {
|
|
|
|
while self.count < n {
|
|
|
|
println!("KO - {} is not mutated", self.count);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-25 06:46:25 +00:00
|
|
|
fn while_loop_with_break_and_return() {
|
|
|
|
let y = 0;
|
|
|
|
while y < 10 {
|
|
|
|
if y == 0 {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
println!("KO - loop contains break");
|
|
|
|
}
|
|
|
|
|
|
|
|
while y < 10 {
|
|
|
|
if y == 0 {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
println!("KO - loop contains return");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-06 09:51:22 +00:00
|
|
|
fn immutable_condition_false_positive(mut n: u64) -> u32 {
|
|
|
|
let mut count = 0;
|
|
|
|
while {
|
|
|
|
n >>= 1;
|
|
|
|
n != 0
|
|
|
|
} {
|
|
|
|
count += 1;
|
|
|
|
}
|
|
|
|
count
|
|
|
|
}
|
|
|
|
|
2018-02-25 17:25:31 +00:00
|
|
|
fn main() {
|
|
|
|
immutable_condition();
|
|
|
|
unused_var();
|
|
|
|
used_immutable();
|
2018-03-06 17:27:11 +00:00
|
|
|
internally_mutable();
|
2021-05-06 09:51:22 +00:00
|
|
|
immutable_condition_false_positive(5);
|
2018-03-26 18:37:34 +00:00
|
|
|
|
|
|
|
let mut c = Counter { count: 0 };
|
|
|
|
c.inc_n(5);
|
|
|
|
c.print_n(2);
|
2019-10-25 06:46:25 +00:00
|
|
|
|
|
|
|
while_loop_with_break_and_return();
|
2018-02-25 17:25:31 +00:00
|
|
|
}
|