about summary refs log tree commit diff
path: root/src/tools/clippy/tests/ui/debug_assert_with_mut_call.rs
blob: 74d59b7593fa7320d02836a37b9f4bfa59650869 (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
178
179
180
181
182
183
184
185
186
187
188
189
190
#![feature(custom_inner_attributes)]
#![rustfmt::skip]
#![warn(clippy::debug_assert_with_mut_call)]
#![allow(clippy::redundant_closure_call, clippy::get_first)]


struct S;

impl S {
    fn bool_self_ref(&self) -> bool { false }
    fn bool_self_mut(&mut self) -> bool { false }
    fn bool_self_ref_arg_ref(&self, _: &u32) -> bool { false }
    fn bool_self_ref_arg_mut(&self, _: &mut u32) -> bool { false }
    fn bool_self_mut_arg_ref(&mut self, _: &u32) -> bool { false }
    fn bool_self_mut_arg_mut(&mut self, _: &mut u32) -> bool { false }

    fn u32_self_ref(&self) -> u32 { 0 }
    fn u32_self_mut(&mut self) -> u32 { 0 }
    fn u32_self_ref_arg_ref(&self, _: &u32) -> u32 { 0 }
    fn u32_self_ref_arg_mut(&self, _: &mut u32) -> u32 { 0 }
    fn u32_self_mut_arg_ref(&mut self, _: &u32) -> u32 { 0 }
    fn u32_self_mut_arg_mut(&mut self, _: &mut u32) -> u32 { 0 }
}

fn bool_ref(_: &u32) -> bool { false }
fn bool_mut(_: &mut u32) -> bool { false }
fn u32_ref(_: &u32) -> u32 { 0 }
fn u32_mut(_: &mut u32) -> u32 { 0 }

fn func_non_mutable() {
    debug_assert!(bool_ref(&3));
    debug_assert!(!bool_ref(&3));

    debug_assert_eq!(0, u32_ref(&3));
    debug_assert_eq!(u32_ref(&3), 0);

    debug_assert_ne!(1, u32_ref(&3));
    debug_assert_ne!(u32_ref(&3), 1);
}

fn func_mutable() {
    debug_assert!(bool_mut(&mut 3));
    //~^ debug_assert_with_mut_call


    debug_assert!(!bool_mut(&mut 3));
    //~^ debug_assert_with_mut_call


    debug_assert_eq!(0, u32_mut(&mut 3));
    //~^ debug_assert_with_mut_call

    debug_assert_eq!(u32_mut(&mut 3), 0);
    //~^ debug_assert_with_mut_call


    debug_assert_ne!(1, u32_mut(&mut 3));
    //~^ debug_assert_with_mut_call

    debug_assert_ne!(u32_mut(&mut 3), 1);
    //~^ debug_assert_with_mut_call

}

fn method_non_mutable() {
    debug_assert!(S.bool_self_ref());
    debug_assert!(S.bool_self_ref_arg_ref(&3));

    debug_assert_eq!(S.u32_self_ref(), 0);
    debug_assert_eq!(S.u32_self_ref_arg_ref(&3), 0);

    debug_assert_ne!(S.u32_self_ref(), 1);
    debug_assert_ne!(S.u32_self_ref_arg_ref(&3), 1);
}

fn method_mutable() {
    debug_assert!(S.bool_self_mut());
    //~^ debug_assert_with_mut_call

    debug_assert!(!S.bool_self_mut());
    //~^ debug_assert_with_mut_call

    debug_assert!(S.bool_self_ref_arg_mut(&mut 3));
    //~^ debug_assert_with_mut_call

    debug_assert!(S.bool_self_mut_arg_ref(&3));
    //~^ debug_assert_with_mut_call

    debug_assert!(S.bool_self_mut_arg_mut(&mut 3));
    //~^ debug_assert_with_mut_call


    debug_assert_eq!(S.u32_self_mut(), 0);
    //~^ debug_assert_with_mut_call

    debug_assert_eq!(S.u32_self_mut_arg_ref(&3), 0);
    //~^ debug_assert_with_mut_call

    debug_assert_eq!(S.u32_self_ref_arg_mut(&mut 3), 0);
    //~^ debug_assert_with_mut_call

    debug_assert_eq!(S.u32_self_mut_arg_mut(&mut 3), 0);
    //~^ debug_assert_with_mut_call


    debug_assert_ne!(S.u32_self_mut(), 1);
    //~^ debug_assert_with_mut_call

    debug_assert_ne!(S.u32_self_mut_arg_ref(&3), 1);
    //~^ debug_assert_with_mut_call

    debug_assert_ne!(S.u32_self_ref_arg_mut(&mut 3), 1);
    //~^ debug_assert_with_mut_call

    debug_assert_ne!(S.u32_self_mut_arg_mut(&mut 3), 1);
    //~^ debug_assert_with_mut_call

}

fn misc() {
    // with variable
    let mut v: Vec<u32> = vec![1, 2, 3, 4];
    debug_assert_eq!(v.get(0), Some(&1));
    debug_assert_ne!(v[0], 2);
    debug_assert_eq!(v.pop(), Some(1));
    //~^ debug_assert_with_mut_call

    debug_assert_ne!(Some(3), v.pop());
    //~^ debug_assert_with_mut_call


    let a = &mut 3;
    debug_assert!(bool_mut(a));
    //~^ debug_assert_with_mut_call


    // nested
    debug_assert!(!(bool_ref(&u32_mut(&mut 3))));
    //~^ debug_assert_with_mut_call


    // chained
    debug_assert_eq!(v.pop().unwrap(), 3);
    //~^ debug_assert_with_mut_call


    // format args
    debug_assert!(bool_ref(&3), "w/o format");
    debug_assert!(bool_mut(&mut 3), "w/o format");
    //~^ debug_assert_with_mut_call

    debug_assert!(bool_ref(&3), "{} format", "w/");
    debug_assert!(bool_mut(&mut 3), "{} format", "w/");
    //~^ debug_assert_with_mut_call


    // sub block
    let mut x = 42_u32;
    debug_assert!({
        bool_mut(&mut x);
        //~^ debug_assert_with_mut_call

        x > 10
    });

    // closures
    debug_assert!((|| {
        let mut x = 42;
        bool_mut(&mut x);
        //~^ debug_assert_with_mut_call

        x > 10
    })());
}

async fn debug_await() {
    debug_assert!(async {
        true
    }.await);
}

fn main() {
    func_non_mutable();
    func_mutable();
    method_non_mutable();
    method_mutable();

    misc();
    debug_await();
}