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