]> git.lizzy.rs Git - rust.git/blob - tests/ui/used_underscore_binding.rs
Move MSRV tests into the lint specific test files
[rust.git] / tests / ui / used_underscore_binding.rs
1 // aux-build:proc_macro_derive.rs
2 #![feature(rustc_private)]
3 #![warn(clippy::all)]
4 #![warn(clippy::used_underscore_binding)]
5 #![allow(clippy::disallowed_names, clippy::eq_op, clippy::uninlined_format_args)]
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 struct field is used in code created with derive.
47 #[derive(Clone, Debug)]
48 pub struct UnderscoreInStruct {
49     _foo: u32,
50 }
51
52 /// Tests that we do not lint if the underscore is not a prefix
53 fn non_prefix_underscore(some_foo: u32) -> u32 {
54     some_foo + 1
55 }
56
57 /// Tests that we do not lint if we do not use the binding (simple case)
58 fn unused_underscore_simple(_foo: u32) -> u32 {
59     1
60 }
61
62 /// Tests that we do not lint if we do not use the binding (complex case). This checks for
63 /// compatibility with the built-in `unused_variables` lint.
64 fn unused_underscore_complex(mut _foo: u32) -> u32 {
65     _foo += 1;
66     _foo = 2;
67     1
68 }
69
70 /// Test that we do not lint for multiple underscores
71 fn multiple_underscores(__foo: u32) -> u32 {
72     __foo + 1
73 }
74
75 // Non-variable bindings with preceding underscore
76 fn _fn_test() {}
77 struct _StructTest;
78 enum _EnumTest {
79     _Empty,
80     _Value(_StructTest),
81 }
82
83 /// Tests that we do not lint for non-variable bindings
84 fn non_variables() {
85     _fn_test();
86     let _s = _StructTest;
87     let _e = match _EnumTest::_Value(_StructTest) {
88         _EnumTest::_Empty => 0,
89         _EnumTest::_Value(_st) => 1,
90     };
91     let f = _fn_test;
92     f();
93 }
94
95 // Tests that we do not lint if the binding comes from await desugaring,
96 // but we do lint the awaited expression. See issue 5360.
97 async fn await_desugaring() {
98     async fn foo() {}
99     fn uses_i(_i: i32) {}
100
101     foo().await;
102     ({
103         let _i = 5;
104         uses_i(_i);
105         foo()
106     })
107     .await
108 }
109
110 fn main() {
111     let foo = 0u32;
112     // tests of unused_underscore lint
113     let _ = prefix_underscore(foo);
114     in_macro_or_desugar(foo);
115     in_struct_field();
116     // possible false positives
117     let _ = non_prefix_underscore(foo);
118     let _ = unused_underscore_simple(foo);
119     let _ = unused_underscore_complex(foo);
120     let _ = multiple_underscores(foo);
121     non_variables();
122     await_desugaring();
123 }