about summary refs log tree commit diff
path: root/src/tools/clippy/tests/ui/unused_unit.rs
blob: 31e980f2655c61fd5122a2424d02f0a4bb159cb8 (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
//@revisions: edition2021 edition2024
//@[edition2021] edition:2021
//@[edition2024] edition:2024

// The output for humans should just highlight the whole span without showing
// the suggested replacement, but we also want to test that suggested
// replacement only removes one set of parentheses, rather than naïvely
// stripping away any starting or ending parenthesis characters—hence this
// test of the JSON error format.

#![feature(custom_inner_attributes)]
#![feature(closure_lifetime_binder)]
#![rustfmt::skip]

#![deny(clippy::unused_unit)]
#![allow(dead_code)]
#![allow(clippy::from_over_into)]

struct Unitter;
impl Unitter {
    #[allow(clippy::no_effect)]
    pub fn get_unit<F: Fn() -> (), G>(&self, f: F, _g: G) -> ()
    //~^ unused_unit
    //~| unused_unit
    where G: Fn() -> () {
    //~^ unused_unit
        let _y: &dyn Fn() -> () = &f;
        //~^ unused_unit
        (); // this should not lint, as it's not in return type position
    }
}

impl Into<()> for Unitter {
    #[rustfmt::skip]
    fn into(self) -> () {
    //~^ unused_unit
        ()
        //~^ unused_unit
    }
}

trait Trait {
    fn redundant<F: FnOnce() -> (), G, H>(&self, _f: F, _g: G, _h: H)
    //~^ unused_unit
    where
        G: FnMut() -> (),
        //~^ unused_unit
        H: Fn() -> ();
        //~^ unused_unit
}

impl Trait for Unitter {
    fn redundant<F: FnOnce() -> (), G, H>(&self, _f: F, _g: G, _h: H)
    //~^ unused_unit
    where
        G: FnMut() -> (),
        //~^ unused_unit
        H: Fn() -> () {}
        //~^ unused_unit
}

fn return_unit() -> () { () }
//~^ unused_unit
//~| unused_unit

#[allow(clippy::needless_return)]
#[allow(clippy::never_loop)]
#[allow(clippy::unit_cmp)]
fn main() {
    let u = Unitter;
    assert_eq!(u.get_unit(|| {}, return_unit), u.into());
    return_unit();
    loop {
        break();
        //~^ unused_unit
    }
    return();
    //~^ unused_unit
}

// https://github.com/rust-lang/rust-clippy/issues/4076
fn foo() {
    macro_rules! foo {
        (recv($r:expr) -> $res:pat => $body:expr) => {
            $body
        }
    }

    foo! {
        recv(rx) -> _x => ()
    }
}

#[rustfmt::skip]
fn test()->(){}
//~^ unused_unit

#[rustfmt::skip]
fn test2() ->(){}
//~^ unused_unit

#[rustfmt::skip]
fn test3()-> (){}
//~^ unused_unit

fn macro_expr() {
    macro_rules! e {
        () => (());
    }
    e!()
}

mod issue9748 {
    fn main() {
        let _ = for<'a> |_: &'a u32| -> () {};
    }
}

mod issue9949 {
    fn main() {
        #[doc = "documentation"]
        ()
    }
}

mod issue14577 {
    trait Unit {}
    impl Unit for () {}

    #[allow(dependency_on_unit_never_type_fallback)]
    fn bar() -> () {
        //~[edition2021]^ unused_unit
        panic!()
    }

    struct UnitStruct;
    impl UnitStruct {
        fn apply<F: for<'c> Fn(&'c mut Self)>(&mut self, f: F) {
            todo!()
        }
    }
}

mod pr14962 {
    #[allow(unused_parens)]
    type UnusedParensButNoUnit = Box<dyn (Fn())>;
}


mod issue15035 {

    trait Convert<T> {
        fn from(value: T) -> Self;
    }

    impl Convert<u64> for () {
        fn from(_value: u64) -> Self {}
    }

    fn handle<T: Convert<u64>>(value: u64) -> T {
        Convert::from(value)
    }

    pub fn f() -> Option<bool> {
        let result: Result<bool, u64> = Err(42);
        // the `-> ()` is required for the inference of `handle`'s return type
        result.map_err(|err| -> () { handle(err) }).ok()
    }
}