about summary refs log tree commit diff
path: root/src/tools/clippy/tests/ui/future_not_send.rs
blob: 662ecb9c955de0327f24f06489cb56aeb0b4d996 (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
#![warn(clippy::future_not_send)]

use std::cell::Cell;
use std::future::Future;
use std::rc::Rc;
use std::sync::Arc;

async fn private_future(rc: Rc<[u8]>, cell: &Cell<usize>) -> bool {
    //~^ future_not_send

    async { true }.await
}

pub async fn public_future(rc: Rc<[u8]>) {
    //~^ future_not_send

    async { true }.await;
}

pub async fn public_send(arc: Arc<[u8]>) -> bool {
    async { false }.await
}

async fn private_future2(rc: Rc<[u8]>, cell: &Cell<usize>) -> bool {
    //~^ future_not_send

    true
}

pub async fn public_future2(rc: Rc<[u8]>) {}
//~^ future_not_send

pub async fn public_send2(arc: Arc<[u8]>) -> bool {
    false
}

struct Dummy {
    rc: Rc<[u8]>,
}

impl Dummy {
    async fn private_future(&self) -> usize {
        //~^ future_not_send

        async { true }.await;
        self.rc.len()
    }

    pub async fn public_future(&self) {
        //~^ future_not_send

        self.private_future().await;
    }

    #[allow(clippy::manual_async_fn)]
    pub fn public_send(&self) -> impl std::future::Future<Output = bool> {
        async { false }
    }
}

async fn generic_future<T>(t: T) -> T
//~^ future_not_send
where
    T: Send,
{
    let rt = &t;
    async { true }.await;
    let _ = rt;
    t
}

async fn maybe_send_generic_future<T>(t: T) -> T {
    async { true }.await;
    t
}

async fn maybe_send_generic_future2<F: Fn() -> Fut, Fut: Future>(f: F) {
    async { true }.await;
    let res = f();
    async { true }.await;
}

async fn generic_future_always_unsend<T>(_: Rc<T>) {
    //~^ future_not_send

    async { true }.await;
}

async fn generic_future_send<T>(t: T)
where
    T: Send,
{
    async { true }.await;
}

async fn unclear_future<T>(t: T) {}

fn main() {
    let rc = Rc::new([1, 2, 3]);
    private_future(rc.clone(), &Cell::new(42));
    public_future(rc.clone());
    let arc = Arc::new([4, 5, 6]);
    public_send(arc);
    generic_future(42);
    generic_future_send(42);

    let dummy = Dummy { rc };
    dummy.public_future();
    dummy.public_send();
}