1 #![allow(clippy::blocks_in_if_conditions)]
3 fn fn_val(i: i32) -> i32 {
6 fn fn_constref(i: &i32) -> i32 {
9 fn fn_mutref(i: &mut i32) {
19 #[allow(clippy::many_single_char_names)]
20 fn immutable_condition() {
21 // Should warn when all vars mentioned are immutable
24 println!("KO - y is immutable");
28 while y < 10 && x < 3 {
31 println!("KO - x and y immutable");
36 println!("KO - cond immutable");
40 while y < 10 && i < 3 {
42 println!("OK - i is mutable");
45 let mut mut_cond = false;
46 while !mut_cond || cond {
48 println!("OK - mut_cond is mutable");
52 println!("OK - Fn call results may vary");
56 println!("OK - Fn call results may vary");
63 println!("OK - a is mutable");
71 println!("OK - tup.0 gets mutated")
76 // Should warn when a (mutable) var is not used in while body
77 let (mut i, mut j) = (0, 0);
81 println!("KO - i not mentioned");
84 while i < 3 && j > 0 {
85 println!("KO - i and j not mentioned");
91 println!("KO - shadowed");
94 while i < 3 && j > 0 {
96 println!("OK - i in cond and mentioned");
100 fn used_immutable() {
105 println!("KO - const reference");
110 println!("KO - passed by value");
114 println!("OK - passed by mutable reference");
120 println!("OK - passed by mutable reference");
125 const B: bool = false;
129 println!("Constants are not linted");
133 println!("Constants are not linted");
137 println!("Constants are not linted");
143 fn maybe_i_mutate(i: &Cell<bool>) {
147 fn internally_mutable() {
148 let b = Cell::new(true);
151 // b cannot be silently coerced to `bool`
153 println!("OK - Method call within condition");
166 fn inc_n(&mut self, n: usize) {
167 while self.count < n {
170 println!("OK - self borrowed mutably");
173 fn print_n(&self, n: usize) {
174 while self.count < n {
175 println!("KO - {} is not mutated", self.count);
180 fn while_loop_with_break_and_return() {
186 println!("KO - loop contains break");
193 println!("KO - loop contains return");
197 fn immutable_condition_false_positive(mut n: u64) -> u32 {
209 immutable_condition();
212 internally_mutable();
213 immutable_condition_false_positive(5);
215 let mut c = Counter { count: 0 };
219 while_loop_with_break_and_return();