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