]> git.lizzy.rs Git - rust.git/blob - tests/compile-fail/used_underscore_binding.rs
281d92c46df8114048e22c7efb173ae33ff2f887
[rust.git] / tests / compile-fail / used_underscore_binding.rs
1 #![feature(plugin)]
2 #![plugin(clippy)]
3 #![deny(clippy)]
4
5 /// Test that we lint if we use a binding with a single leading underscore
6 fn prefix_underscore(_foo: u32) -> u32 {
7     _foo + 1 //~ ERROR used binding which is prefixed with an underscore
8 }
9
10 /// Test that we lint even if the use is within a macro expansion
11 fn in_macro(_foo: u32) {
12     println!("{}", _foo); //~ ERROR used binding which is prefixed with an underscore
13 }
14
15 // Struct for testing use of fields prefixed with an underscore
16 struct StructFieldTest {
17     _underscore_field: u32,
18 }
19
20 /// Test that we lint the use of a struct field which is prefixed with an underscore
21 fn in_struct_field() {
22     let mut s = StructFieldTest { _underscore_field: 0 };
23     s._underscore_field += 1; //~ Error used binding which is prefixed with an underscore
24 }
25
26 /// Test that we do not lint if the underscore is not a prefix
27 fn non_prefix_underscore(some_foo: u32) -> u32 {
28     some_foo + 1
29 }
30
31 /// Test that we do not lint if we do not use the binding (simple case)
32 fn unused_underscore_simple(_foo: u32) -> u32 {
33     1
34 }
35
36 /// Test that we do not lint if we do not use the binding (complex case). This checks for
37 /// compatibility with the built-in `unused_variables` lint.
38 fn unused_underscore_complex(mut _foo: u32) -> u32 {
39     _foo += 1;
40     _foo = 2;
41     1
42 }
43
44 ///Test that we do not lint for multiple underscores
45 fn multiple_underscores(__foo: u32) -> u32 {
46     __foo + 1
47 }
48
49 // Non-variable bindings with preceding underscore
50 fn _fn_test() {}
51 struct _StructTest;
52 enum _EnumTest {
53     _Empty,
54     _Value(_StructTest)
55 }
56
57 /// Test that we do not lint for non-variable bindings
58 fn non_variables() {
59     _fn_test();
60     let _s = _StructTest;
61     let _e = match _EnumTest::_Value(_StructTest) {
62         _EnumTest::_Empty => 0,
63         _EnumTest::_Value(_st) => 1,
64     };
65     let f = _fn_test;
66     f();
67 }
68
69 fn main() {
70     let foo = 0u32;
71     // tests of unused_underscore lint
72     let _ = prefix_underscore(foo);
73     in_macro(foo);
74     in_struct_field();
75     // possible false positives
76     let _ = non_prefix_underscore(foo);
77     let _ = unused_underscore_simple(foo);
78     let _ = unused_underscore_complex(foo);
79     let _ = multiple_underscores(foo);
80     non_variables();
81 }