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