]> git.lizzy.rs Git - rust.git/blob - tests/ui/used_underscore_binding.rs
Stabilize tool lints
[rust.git] / tests / ui / used_underscore_binding.rs
1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
9
10
11
12
13 #![warn(clippy::all)]
14
15 #![allow(clippy::blacklisted_name)]
16 #![warn(clippy::used_underscore_binding)]
17
18 macro_rules! test_macro {
19     () => {{
20         let _foo = 42;
21         _foo + 1
22     }}
23 }
24
25 /// Test that we lint if we use a binding with a single leading underscore
26 fn prefix_underscore(_foo: u32) -> u32 {
27     _foo + 1
28 }
29
30 /// Test that we lint if we use a `_`-variable defined outside within a macro expansion
31 fn in_macro(_foo: u32) {
32     println!("{}", _foo);
33     assert_eq!(_foo, _foo);
34
35     test_macro!() + 1;
36 }
37
38 // Struct for testing use of fields prefixed with an underscore
39 struct StructFieldTest {
40     _underscore_field: u32,
41 }
42
43 /// Test that we lint the use of a struct field which is prefixed with an underscore
44 fn in_struct_field() {
45     let mut s = StructFieldTest { _underscore_field: 0 };
46     s._underscore_field += 1;
47 }
48
49 /// Test that we do not lint if the underscore is not a prefix
50 fn non_prefix_underscore(some_foo: u32) -> u32 {
51     some_foo + 1
52 }
53
54 /// Test that we do not lint if we do not use the binding (simple case)
55 fn unused_underscore_simple(_foo: u32) -> u32 {
56     1
57 }
58
59 /// Test that we do not lint if we do not use the binding (complex case). This checks for
60 /// compatibility with the built-in `unused_variables` lint.
61 fn unused_underscore_complex(mut _foo: u32) -> u32 {
62     _foo += 1;
63     _foo = 2;
64     1
65 }
66
67 ///Test that we do not lint for multiple underscores
68 fn multiple_underscores(__foo: u32) -> u32 {
69     __foo + 1
70 }
71
72 // Non-variable bindings with preceding underscore
73 fn _fn_test() {}
74 struct _StructTest;
75 enum _EnumTest {
76     _Empty,
77     _Value(_StructTest)
78 }
79
80 /// Test that we do not lint for non-variable bindings
81 fn non_variables() {
82     _fn_test();
83     let _s = _StructTest;
84     let _e = match _EnumTest::_Value(_StructTest) {
85         _EnumTest::_Empty => 0,
86         _EnumTest::_Value(_st) => 1,
87     };
88     let f = _fn_test;
89     f();
90 }
91
92 fn main() {
93     let foo = 0u32;
94     // tests of unused_underscore lint
95     let _ = prefix_underscore(foo);
96     in_macro(foo);
97     in_struct_field();
98     // possible false positives
99     let _ = non_prefix_underscore(foo);
100     let _ = unused_underscore_simple(foo);
101     let _ = unused_underscore_complex(foo);
102     let _ = multiple_underscores(foo);
103     non_variables();
104 }