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