1 // aux-build:proc_macro_derive.rs
3 #![feature(rustc_private)]
5 #![allow(clippy::blacklisted_name, clippy::eq_op)]
6 #![warn(clippy::used_underscore_binding)]
9 extern crate proc_macro_derive;
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)]
16 macro_rules! test_macro {
23 /// Tests that we lint if we use a binding with a single leading underscore
24 fn prefix_underscore(_foo: u32) -> u32 {
28 /// Tests that we lint if we use a `_`-variable defined outside within a macro expansion
29 fn in_macro_or_desugar(_foo: u32) {
31 assert_eq!(_foo, _foo);
36 // Struct for testing use of fields prefixed with an underscore
37 struct StructFieldTest {
38 _underscore_field: u32,
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;
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 {
53 /// Tests that we do not lint if the underscore is not a prefix
54 fn non_prefix_underscore(some_foo: u32) -> u32 {
58 /// Tests that we do not lint if we do not use the binding (simple case)
59 fn unused_underscore_simple(_foo: u32) -> u32 {
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 {
71 /// Test that we do not lint for multiple underscores
72 fn multiple_underscores(__foo: u32) -> u32 {
76 // Non-variable bindings with preceding underscore
84 /// Tests that we do not lint for non-variable bindings
88 let _e = match _EnumTest::_Value(_StructTest) {
89 _EnumTest::_Empty => 0,
90 _EnumTest::_Value(_st) => 1,
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() {
100 fn uses_i(_i: i32) {}
113 // tests of unused_underscore lint
114 let _ = prefix_underscore(foo);
115 in_macro_or_desugar(foo);
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);