]> git.lizzy.rs Git - rust.git/blob - tests/ui/let_unit.rs
Merge pull request #3265 from mikerite/fix-export
[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
11 #![feature(tool_lints)]
12
13
14 #![warn(clippy::let_unit_value)]
15 #![allow(unused_variables)]
16
17 macro_rules! let_and_return {
18     ($n:expr) => {{
19         let ret = $n;
20     }}
21 }
22
23 fn main() {
24     let _x = println!("x");
25     let _y = 1;   // this is fine
26     let _z = ((), 1);  // this as well
27     if true {
28         let _a = ();
29     }
30
31     consume_units_with_for_loop(); // should be fine as well
32
33     let_and_return!(()) // should be fine
34 }
35
36 // Related to issue #1964
37 fn consume_units_with_for_loop() {
38     // `for_let_unit` lint should not be triggered by consuming them using for loop.
39     let v = vec![(), (), ()];
40     let mut count = 0;
41     for _ in v {
42         count += 1;
43     }
44     assert_eq!(count, 3);
45
46     // Same for consuming from some other Iterator<Item = ()>.
47     let (tx, rx) = ::std::sync::mpsc::channel();
48     tx.send(()).unwrap();
49     drop(tx);
50
51     count = 0;
52     for _ in rx.iter() {
53         count += 1;
54     }
55     assert_eq!(count, 1);
56 }
57
58 #[derive(Copy, Clone)]
59 pub struct ContainsUnit(()); // should be fine