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