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