]> git.lizzy.rs Git - rust.git/blob - tests/ui/unit_arg.rs
Auto merge of #85538 - r00ster91:iterrepeat, r=Mark-Simulacrum
[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 baz<T: Debug>(t: T) {
31     foo(t);
32 }
33
34 trait Tr {
35     type Args;
36     fn do_it(args: Self::Args);
37 }
38
39 struct A;
40 impl Tr for A {
41     type Args = ();
42     fn do_it(_: Self::Args) {}
43 }
44
45 struct B;
46 impl Tr for B {
47     type Args = <A as Tr>::Args;
48
49     fn do_it(args: Self::Args) {
50         A::do_it(args)
51     }
52 }
53
54 fn bad() {
55     foo({
56         1;
57     });
58     foo(foo(1));
59     foo({
60         foo(1);
61         foo(2);
62     });
63     let b = Bar;
64     b.bar({
65         1;
66     });
67     taking_multiple_units(foo(0), foo(1));
68     taking_multiple_units(foo(0), {
69         foo(1);
70         foo(2);
71     });
72     taking_multiple_units(
73         {
74             foo(0);
75             foo(1);
76         },
77         {
78             foo(2);
79             foo(3);
80         },
81     );
82     // here Some(foo(2)) isn't the top level statement expression, wrap the suggestion in a block
83     None.or(Some(foo(2)));
84     // in this case, the suggestion can be inlined, no need for a surrounding block
85     // foo(()); foo(()) instead of { foo(()); foo(()) }
86     foo(foo(()));
87 }
88
89 fn ok() {
90     foo(());
91     foo(1);
92     foo({ 1 });
93     foo3("a", 3, vec![3]);
94     let b = Bar;
95     b.bar({ 1 });
96     b.bar(());
97     question_mark();
98     let named_unit_arg = ();
99     foo(named_unit_arg);
100     baz(());
101     B::do_it(());
102 }
103
104 fn question_mark() -> Result<(), ()> {
105     Ok(Ok(())?)?;
106     Ok(Ok(()))??;
107     Ok(())
108 }
109
110 #[allow(dead_code)]
111 mod issue_2945 {
112     fn unit_fn() -> Result<(), i32> {
113         Ok(())
114     }
115
116     fn fallible() -> Result<(), i32> {
117         Ok(unit_fn()?)
118     }
119 }
120
121 #[allow(dead_code)]
122 fn returning_expr() -> Option<()> {
123     Some(foo(1))
124 }
125
126 fn taking_multiple_units(a: (), b: ()) {}
127
128 fn main() {
129     bad();
130     ok();
131 }