]> git.lizzy.rs Git - rust.git/blob - tests/ui/unit_arg.rs
Split up tests for unit arg expressions
[rust.git] / tests / ui / unit_arg.rs
1 #![warn(clippy::unit_arg)]
2 #![allow(
3     clippy::no_effect,
4     unused_must_use,
5     unused_variables,
6     clippy::unused_unit,
7     clippy::unnecessary_wraps,
8     clippy::or_fun_call,
9     clippy::needless_question_mark
10 )]
11
12 use std::fmt::Debug;
13
14 fn foo<T: Debug>(t: T) {
15     println!("{:?}", t);
16 }
17
18 fn foo3<T1: Debug, T2: Debug, T3: Debug>(t1: T1, t2: T2, t3: T3) {
19     println!("{:?}, {:?}, {:?}", t1, t2, t3);
20 }
21
22 struct Bar;
23
24 impl Bar {
25     fn bar<T: Debug>(&self, t: T) {
26         println!("{:?}", t);
27     }
28 }
29
30 fn bad() {
31     foo({
32         1;
33     });
34     foo(foo(1));
35     foo({
36         foo(1);
37         foo(2);
38     });
39     let b = Bar;
40     b.bar({
41         1;
42     });
43     taking_multiple_units(foo(0), foo(1));
44     taking_multiple_units(foo(0), {
45         foo(1);
46         foo(2);
47     });
48     taking_multiple_units(
49         {
50             foo(0);
51             foo(1);
52         },
53         {
54             foo(2);
55             foo(3);
56         },
57     );
58     // here Some(foo(2)) isn't the top level statement expression, wrap the suggestion in a block
59     None.or(Some(foo(2)));
60     // in this case, the suggestion can be inlined, no need for a surrounding block
61     // foo(()); foo(()) instead of { foo(()); foo(()) }
62     foo(foo(()));
63 }
64
65 fn ok() {
66     foo(());
67     foo(1);
68     foo({ 1 });
69     foo3("a", 3, vec![3]);
70     let b = Bar;
71     b.bar({ 1 });
72     b.bar(());
73     question_mark();
74     let named_unit_arg = ();
75     foo(named_unit_arg);
76 }
77
78 fn question_mark() -> Result<(), ()> {
79     Ok(Ok(())?)?;
80     Ok(Ok(()))??;
81     Ok(())
82 }
83
84 #[allow(dead_code)]
85 mod issue_2945 {
86     fn unit_fn() -> Result<(), i32> {
87         Ok(())
88     }
89
90     fn fallible() -> Result<(), i32> {
91         Ok(unit_fn()?)
92     }
93 }
94
95 #[allow(dead_code)]
96 fn returning_expr() -> Option<()> {
97     Some(foo(1))
98 }
99
100 fn taking_multiple_units(a: (), b: ()) {}
101
102 fn main() {
103     bad();
104     ok();
105 }