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