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