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