]> git.lizzy.rs Git - rust.git/blob - tests/ui/let_unit.rs
Auto merge of #3635 - matthiaskrgr:revert_random_state_3603, r=xfix
[rust.git] / tests / ui / let_unit.rs
1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
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.
9
10 #![warn(clippy::let_unit_value)]
11 #![allow(unused_variables)]
12
13 macro_rules! let_and_return {
14     ($n:expr) => {{
15         let ret = $n;
16     }};
17 }
18
19 fn main() {
20     let _x = println!("x");
21     let _y = 1; // this is fine
22     let _z = ((), 1); // this as well
23     if true {
24         let _a = ();
25     }
26
27     consume_units_with_for_loop(); // should be fine as well
28
29     let_and_return!(()) // should be fine
30 }
31
32 // Related to issue #1964
33 fn consume_units_with_for_loop() {
34     // `for_let_unit` lint should not be triggered by consuming them using for loop.
35     let v = vec![(), (), ()];
36     let mut count = 0;
37     for _ in v {
38         count += 1;
39     }
40     assert_eq!(count, 3);
41
42     // Same for consuming from some other Iterator<Item = ()>.
43     let (tx, rx) = ::std::sync::mpsc::channel();
44     tx.send(()).unwrap();
45     drop(tx);
46
47     count = 0;
48     for _ in rx.iter() {
49         count += 1;
50     }
51     assert_eq!(count, 1);
52 }
53
54 #[derive(Copy, Clone)]
55 pub struct ContainsUnit(()); // should be fine