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