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.
11 #![feature(tool_lints)]
13 #![allow(clippy::trivially_copy_pass_by_ref)]
16 fn fn_val(i: i32) -> i32 { unimplemented!() }
17 fn fn_constref(i: &i32) -> i32 { unimplemented!() }
18 fn fn_mutref(i: &mut i32) { unimplemented!() }
19 fn fooi() -> i32 { unimplemented!() }
20 fn foob() -> bool { unimplemented!() }
22 #[allow(clippy::many_single_char_names)]
23 fn immutable_condition() {
24 // Should warn when all vars mentioned are immutable
27 println!("KO - y is immutable");
31 while y < 10 && x < 3 {
34 println!("KO - x and y immutable");
39 println!("KO - cond immutable");
43 while y < 10 && i < 3 {
45 println!("OK - i is mutable");
48 let mut mut_cond = false;
49 while !mut_cond || cond {
51 println!("OK - mut_cond is mutable");
55 println!("OK - Fn call results may vary");
59 println!("OK - Fn call results may vary");
66 println!("OK - a is mutable");
74 println!("OK - tup.0 gets mutated")
79 // Should warn when a (mutable) var is not used in while body
80 let (mut i, mut j) = (0, 0);
84 println!("KO - i not mentioned");
87 while i < 3 && j > 0 {
88 println!("KO - i and j not mentioned");
94 println!("KO - shadowed");
97 while i < 3 && j > 0 {
99 println!("OK - i in cond and mentioned");
103 fn used_immutable() {
108 println!("KO - const reference");
113 println!("KO - passed by value");
117 println!("OK - passed by mutable reference");
123 println!("OK - passed by mutable reference");
128 const B: bool = false;
132 println!("Constants are not linted");
136 println!("Constants are not linted");
140 println!("Constants are not linted");
146 fn maybe_i_mutate(i: &Cell<bool>) { unimplemented!() }
148 fn internally_mutable() {
149 let b = Cell::new(true);
151 while b.get() { // 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);
181 immutable_condition();
184 internally_mutable();
186 let mut c = Counter { count: 0 };