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