about summary refs log tree commit diff
path: root/src/tools/clippy/tests/ui/unit_arg.rs
blob: 4208efad6774d7858ec1d1d0f5bb18828b53187e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//@aux-build: proc_macros.rs
//@no-rustfix: overlapping suggestions
#![warn(clippy::unit_arg)]
#![allow(unused_must_use, unused_variables)]
#![allow(
    clippy::let_unit_value,
    clippy::needless_question_mark,
    clippy::never_loop,
    clippy::no_effect,
    clippy::or_fun_call,
    clippy::self_named_constructors,
    clippy::uninlined_format_args,
    clippy::unnecessary_wraps,
    clippy::unused_unit
)]

extern crate proc_macros;

use proc_macros::with_span;
use std::fmt::Debug;

fn foo<T: Debug>(t: T) {
    println!("{:?}", t);
}

fn foo3<T1: Debug, T2: Debug, T3: Debug>(t1: T1, t2: T2, t3: T3) {
    println!("{:?}, {:?}, {:?}", t1, t2, t3);
}

struct Bar;

impl Bar {
    fn bar<T: Debug>(&self, t: T) {
        println!("{:?}", t);
    }
}

fn baz<T: Debug>(t: T) {
    foo(t);
}

trait Tr {
    type Args;
    fn do_it(args: Self::Args);
}

struct A;
impl Tr for A {
    type Args = ();
    fn do_it(_: Self::Args) {}
}

struct B;
impl Tr for B {
    type Args = <A as Tr>::Args;

    fn do_it(args: Self::Args) {
        A::do_it(args)
    }
}

fn bad() {
    foo({
        //~^ unit_arg
        1;
    });
    foo(foo(1));
    //~^ unit_arg
    foo({
        //~^ unit_arg
        foo(1);
        foo(2);
    });
    let b = Bar;
    b.bar({
        //~^ unit_arg
        1;
    });
    taking_multiple_units(foo(0), foo(1));
    //~^ unit_arg
    taking_multiple_units(foo(0), {
        //~^ unit_arg
        foo(1);
        foo(2);
    });
    taking_multiple_units(
        //~^ unit_arg
        {
            foo(0);
            foo(1);
        },
        {
            foo(2);
            foo(3);
        },
    );
    // here Some(foo(2)) isn't the top level statement expression, wrap the suggestion in a block
    None.or(Some(foo(2)));
    //~^ unit_arg
    // in this case, the suggestion can be inlined, no need for a surrounding block
    // foo(()); foo(()) instead of { foo(()); foo(()) }
    foo(foo(()));
    //~^ unit_arg
}

fn ok() {
    foo(());
    foo(1);
    foo({ 1 });
    foo3("a", 3, vec![3]);
    let b = Bar;
    b.bar({ 1 });
    b.bar(());
    question_mark();
    let named_unit_arg = ();
    foo(named_unit_arg);
    baz(());
    B::do_it(());
}

fn question_mark() -> Result<(), ()> {
    Ok(Ok(())?)?;
    Ok(Ok(()))??;
    Ok(())
}

#[allow(dead_code)]
mod issue_2945 {
    fn unit_fn() -> Result<(), i32> {
        Ok(())
    }

    fn fallible() -> Result<(), i32> {
        Ok(unit_fn()?)
    }
}

#[allow(dead_code)]
fn returning_expr() -> Option<()> {
    Some(foo(1))
    //~^ unit_arg
}

fn taking_multiple_units(a: (), b: ()) {}

fn proc_macro() {
    with_span!(span taking_multiple_units(unsafe { (); }, 'x: loop { break 'x (); }));
}

fn main() {
    bad();
    ok();
}

fn issue14857() {
    let fn_take_unit = |_: ()| {};
    fn some_other_fn(_: &i32) {}

    macro_rules! mac {
        (def) => {
            Default::default()
        };
        (func $f:expr) => {
            $f()
        };
        (nonempty_block $e:expr) => {{
            some_other_fn(&$e);
            $e
        }};
    }
    fn_take_unit(mac!(def));
    //~^ unit_arg
    fn_take_unit(mac!(func Default::default));
    //~^ unit_arg
    fn_take_unit(mac!(nonempty_block Default::default()));
    //~^ unit_arg
}