summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/tests/ui/rc_clone_in_vec_init/weak.rs
blob: 693c9b553c562fdeaf28629f2ae269b08f7e68bd (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
#![warn(clippy::rc_clone_in_vec_init)]
use std::rc::{Rc, Weak as UnSyncWeak};
use std::sync::{Arc, Mutex, Weak as SyncWeak};

fn main() {}

fn should_warn_simple_case() {
    let v = vec![SyncWeak::<u32>::new(); 2];
    let v2 = vec![UnSyncWeak::<u32>::new(); 2];

    let v = vec![Rc::downgrade(&Rc::new("x".to_string())); 2];
    let v = vec![Arc::downgrade(&Arc::new("x".to_string())); 2];
}

fn should_warn_simple_case_with_big_indentation() {
    if true {
        let k = 1;
        dbg!(k);
        if true {
            let v = vec![Arc::downgrade(&Arc::new("x".to_string())); 2];
            let v2 = vec![Rc::downgrade(&Rc::new("x".to_string())); 2];
        }
    }
}

fn should_warn_complex_case() {
    let v = vec![
        Arc::downgrade(&Arc::new(Mutex::new({
            let x = 1;
            dbg!(x);
            x
        })));
        2
    ];

    let v1 = vec![
        Rc::downgrade(&Rc::new(Mutex::new({
            let x = 1;
            dbg!(x);
            x
        })));
        2
    ];
}

fn should_not_warn_custom_weak() {
    #[derive(Clone)]
    struct Weak;

    impl Weak {
        fn new() -> Self {
            Weak
        }
    }

    let v = vec![Weak::new(); 2];
}

fn should_not_warn_vec_from_elem_but_not_weak() {
    let v = vec![String::new(); 2];
    let v1 = vec![1; 2];
    let v2 = vec![
        Box::new(Arc::downgrade(&Arc::new({
            let y = 3;
            dbg!(y);
            y
        })));
        2
    ];
    let v3 = vec![
        Box::new(Rc::downgrade(&Rc::new({
            let y = 3;
            dbg!(y);
            y
        })));
        2
    ];
}

fn should_not_warn_vec_macro_but_not_from_elem() {
    let v = vec![Arc::downgrade(&Arc::new("x".to_string()))];
    let v = vec![Rc::downgrade(&Rc::new("x".to_string()))];
}