1 // aux-build:proc_macro_derive.rs
2 #![feature(rustc_private)]
4 #![warn(clippy::used_underscore_binding)]
5 #![allow(clippy::disallowed_names, clippy::eq_op, clippy::uninlined_format_args)]
8 extern crate proc_macro_derive;
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)]
15 macro_rules! test_macro {
22 /// Tests that we lint if we use a binding with a single leading underscore
23 fn prefix_underscore(_foo: u32) -> u32 {
27 /// Tests that we lint if we use a `_`-variable defined outside within a macro expansion
28 fn in_macro_or_desugar(_foo: u32) {
30 assert_eq!(_foo, _foo);
35 // Struct for testing use of fields prefixed with an underscore
36 struct StructFieldTest {
37 _underscore_field: u32,
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;
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 {
52 /// Tests that we do not lint if the underscore is not a prefix
53 fn non_prefix_underscore(some_foo: u32) -> u32 {
57 /// Tests that we do not lint if we do not use the binding (simple case)
58 fn unused_underscore_simple(_foo: u32) -> u32 {
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 {
70 /// Test that we do not lint for multiple underscores
71 fn multiple_underscores(__foo: u32) -> u32 {
75 // Non-variable bindings with preceding underscore
83 /// Tests that we do not lint for non-variable bindings
87 let _e = match _EnumTest::_Value(_StructTest) {
88 _EnumTest::_Empty => 0,
89 _EnumTest::_Value(_st) => 1,
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() {
112 // tests of unused_underscore lint
113 let _ = prefix_underscore(foo);
114 in_macro_or_desugar(foo);
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);