1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
10 #![allow(clippy::trivially_copy_pass_by_ref)]
12 fn fn_val(i: i32) -> i32 {
15 fn fn_constref(i: &i32) -> i32 {
18 fn fn_mutref(i: &mut i32) {
28 #[allow(clippy::many_single_char_names)]
29 fn immutable_condition() {
30 // Should warn when all vars mentioned are immutable
33 println!("KO - y is immutable");
37 while y < 10 && x < 3 {
40 println!("KO - x and y immutable");
45 println!("KO - cond immutable");
49 while y < 10 && i < 3 {
51 println!("OK - i is mutable");
54 let mut mut_cond = false;
55 while !mut_cond || cond {
57 println!("OK - mut_cond is mutable");
61 println!("OK - Fn call results may vary");
65 println!("OK - Fn call results may vary");
72 println!("OK - a is mutable");
80 println!("OK - tup.0 gets mutated")
85 // Should warn when a (mutable) var is not used in while body
86 let (mut i, mut j) = (0, 0);
90 println!("KO - i not mentioned");
93 while i < 3 && j > 0 {
94 println!("KO - i and j not mentioned");
100 println!("KO - shadowed");
103 while i < 3 && j > 0 {
105 println!("OK - i in cond and mentioned");
109 fn used_immutable() {
114 println!("KO - const reference");
119 println!("KO - passed by value");
123 println!("OK - passed by mutable reference");
129 println!("OK - passed by mutable reference");
134 const B: bool = false;
138 println!("Constants are not linted");
142 println!("Constants are not linted");
146 println!("Constants are not linted");
152 fn maybe_i_mutate(i: &Cell<bool>) {
156 fn internally_mutable() {
157 let b = Cell::new(true);
160 // b cannot be silently coerced to `bool`
162 println!("OK - Method call within condition");
175 fn inc_n(&mut self, n: usize) {
176 while self.count < n {
179 println!("OK - self borrowed mutably");
182 fn print_n(&self, n: usize) {
183 while self.count < n {
184 println!("KO - {} is not mutated", self.count);
190 immutable_condition();
193 internally_mutable();
195 let mut c = Counter { count: 0 };