about summary refs log tree commit diff
path: root/src/tools/clippy/tests/ui/redundant_test_prefix_noautofix.rs
blob: 6ad5d011d8b71217661425aa02cae205d4746735 (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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
//@no-rustfix: name conflicts

#![allow(dead_code)]
#![warn(clippy::redundant_test_prefix)]

fn main() {
    // Normal function, no redundant prefix.
}

fn f1() {
    // Normal function, no redundant prefix.
}

fn test_f2() {
    // Has prefix, but no `#[test]` attribute, ignore.
}

#[test]
fn test_f3() {
    //~^ redundant_test_prefix

    // Has prefix, has `#[test]` attribute. Not within a `#[cfg(test)]`.
    // No collision with other functions, should emit warning.
}

#[cfg(test)]
#[test]
fn test_f4() {
    //~^ redundant_test_prefix

    // Has prefix, has `#[test]` attribute, within a `#[cfg(test)]`.
    // No collision with other functions, should emit warning.
}

fn f5() {}

#[cfg(test)]
#[test]
fn test_f5() {
    //~^ redundant_test_prefix

    // Has prefix, has `#[test]` attribute, within a `#[cfg(test)]`.
    // Collision with existing function.
}

mod m1 {
    pub fn f6() {}
    pub fn f7() {}
}

#[cfg(test)]
#[test]
fn test_f6() {
    //~^ redundant_test_prefix

    use m1::f6;

    f6();
    // Has prefix, has `#[test]` attribute, within a `#[cfg(test)]`.
    // No collision, but has a function call that will result in recursion.
}

#[cfg(test)]
#[test]
fn test_f8() {
    //~^ redundant_test_prefix

    use m1::f7;

    f7();
    // Has prefix, has `#[test]` attribute, within a `#[cfg(test)]`.
    // No collision, has function call, but it will not result in recursion.
}

// Although there's no direct call of `f` in the test, name collision still exists,
// since all `m3` functions are imported and then `map` is used to call `f`.
mod m2 {
    mod m3 {
        pub fn f(_: i32) -> i32 {
            0
        }
    }

    use m3::*;

    #[cfg(test)]
    #[test]
    fn test_f() {
        //~^ redundant_test_prefix
        let a = Some(3);
        let _ = a.map(f);
    }
}

mod m3 {
    fn test_m3_1() {
        // Has prefix, but no `#[test]` attribute, ignore.
    }

    #[test]
    fn test_m3_2() {
        //~^ redundant_test_prefix

        // Has prefix, has `#[test]` attribute. Not within a `#[cfg(test)]`.
        // No collision with other functions, should emit warning.
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_foo() {
        //~^ redundant_test_prefix
    }

    #[test]
    fn test_foo_with_call() {
        //~^ redundant_test_prefix

        main();
    }

    #[test]
    fn test_f1() {
        //~^ redundant_test_prefix
    }

    #[test]
    fn test_f2() {
        //~^ redundant_test_prefix
    }

    #[test]
    fn test_f3() {
        //~^ redundant_test_prefix
    }

    #[test]
    fn test_f4() {
        //~^ redundant_test_prefix
    }

    #[test]
    fn test_f5() {
        //~^ redundant_test_prefix
    }

    #[test]
    fn test_f6() {
        //~^ redundant_test_prefix
    }

    #[test]
    fn test_1() {
        //~^ redundant_test_prefix

        // `1` is invalid function name, so suggestion to rename is emitted
    }

    #[test]
    fn test_const() {
        //~^ redundant_test_prefix

        // `const` is reserved keyword, so suggestion to rename is emitted
    }

    #[test]
    fn test_async() {
        //~^ redundant_test_prefix

        // `async` is reserved keyword, so suggestion to rename is emitted
    }

    #[test]
    fn test_yield() {
        //~^ redundant_test_prefix

        // `yield` is reserved keyword for future use, so suggestion to rename is emitted
    }

    #[test]
    fn test_() {
        //~^ redundant_test_prefix

        // `` is invalid function name, so suggestion to rename is emitted
    }
}

mod tests_no_annotations {
    use super::*;

    #[test]
    fn test_foo() {
        //~^ redundant_test_prefix
    }

    #[test]
    fn test_foo_with_call() {
        //~^ redundant_test_prefix

        main();
    }

    #[test]
    fn test_f1() {
        //~^ redundant_test_prefix
    }

    #[test]
    fn test_f2() {
        //~^ redundant_test_prefix
    }

    #[test]
    fn test_f3() {
        //~^ redundant_test_prefix
    }

    #[test]
    fn test_f4() {
        //~^ redundant_test_prefix
    }

    #[test]
    fn test_f5() {
        //~^ redundant_test_prefix
    }

    #[test]
    fn test_f6() {
        //~^ redundant_test_prefix
    }

    #[test]
    fn test_1() {
        //~^ redundant_test_prefix

        // `1` is invalid function name, so suggestion to rename is emitted
    }

    #[test]
    fn test_const() {
        //~^ redundant_test_prefix

        // `const` is reserved keyword, so suggestion to rename is emitted
    }

    #[test]
    fn test_async() {
        //~^ redundant_test_prefix

        // `async` is reserved keyword, so suggestion to rename is emitted
    }

    #[test]
    fn test_yield() {
        //~^ redundant_test_prefix

        // `yield` is reserved keyword for future use, so suggestion to rename is emitted
    }

    #[test]
    fn test_() {
        //~^ redundant_test_prefix

        // `` is invalid function name, so suggestion to rename is emitted
    }
}

// This test is inspired by real test in `clippy_utils/src/sugg.rs`.
// The `is_in_test_function()` checks whether any identifier within a given node's parents is
// marked with `#[test]` attribute. Thus flagging false positives when nested functions are
// prefixed with `test_`. Therefore `is_test_function()` has been defined in `clippy_utils`,
// allowing to select only functions that are immediately marked with `#[test]` annotation.
//
// This test case ensures that for such nested functions no error is emitted.
#[test]
fn not_op() {
    fn test_not(foo: bool) {
        assert!(foo);
    }

    // Use helper function
    test_not(true);
    test_not(false);
}