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