2023-08-24 14:32:12 -05:00
|
|
|
//@aux-build:proc_macro_derive.rs
|
2024-02-10 16:31:36 -06:00
|
|
|
#![feature(rustc_private)]
|
2018-07-28 10:34:52 -05:00
|
|
|
#![warn(clippy::used_underscore_binding)]
|
2022-10-06 02:44:38 -05:00
|
|
|
#![allow(clippy::disallowed_names, clippy::eq_op, clippy::uninlined_format_args)]
|
2016-02-22 08:42:24 -06:00
|
|
|
|
2019-04-20 02:33:13 -05:00
|
|
|
#[macro_use]
|
|
|
|
extern crate proc_macro_derive;
|
|
|
|
|
|
|
|
// This should not trigger the lint. There's underscore binding inside the external derive that
|
|
|
|
// would trigger the `used_underscore_binding` lint.
|
|
|
|
#[derive(DeriveSomething)]
|
|
|
|
struct Baz;
|
|
|
|
|
2016-06-15 09:27:56 -05:00
|
|
|
macro_rules! test_macro {
|
|
|
|
() => {{
|
|
|
|
let _foo = 42;
|
|
|
|
_foo + 1
|
2018-12-09 16:26:16 -06:00
|
|
|
}};
|
2016-06-15 09:27:56 -05:00
|
|
|
}
|
|
|
|
|
2019-01-30 19:15:29 -06:00
|
|
|
/// Tests that we lint if we use a binding with a single leading underscore
|
2015-12-18 18:04:33 -06:00
|
|
|
fn prefix_underscore(_foo: u32) -> u32 {
|
2017-02-08 07:58:07 -06:00
|
|
|
_foo + 1
|
2015-12-10 14:54:43 -06:00
|
|
|
}
|
|
|
|
|
2019-01-30 19:15:29 -06:00
|
|
|
/// Tests that we lint if we use a `_`-variable defined outside within a macro expansion
|
2019-05-11 22:40:05 -05:00
|
|
|
fn in_macro_or_desugar(_foo: u32) {
|
2016-06-15 09:27:56 -05:00
|
|
|
println!("{}", _foo);
|
|
|
|
assert_eq!(_foo, _foo);
|
2017-02-08 07:58:07 -06:00
|
|
|
|
2016-06-15 09:27:56 -05:00
|
|
|
test_macro!() + 1;
|
2015-12-21 03:03:12 -06:00
|
|
|
}
|
|
|
|
|
2015-12-18 18:29:22 -06:00
|
|
|
// Struct for testing use of fields prefixed with an underscore
|
|
|
|
struct StructFieldTest {
|
|
|
|
_underscore_field: u32,
|
|
|
|
}
|
|
|
|
|
2019-01-30 19:15:29 -06:00
|
|
|
/// Tests that we lint the use of a struct field which is prefixed with an underscore
|
2015-12-18 18:29:22 -06:00
|
|
|
fn in_struct_field() {
|
|
|
|
let mut s = StructFieldTest { _underscore_field: 0 };
|
2017-02-08 07:58:07 -06:00
|
|
|
s._underscore_field += 1;
|
2015-12-18 18:29:22 -06:00
|
|
|
}
|
|
|
|
|
2022-07-07 14:30:37 -05:00
|
|
|
/// Tests that we do not lint if the struct field is used in code created with derive.
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct UnderscoreInStruct {
|
|
|
|
_foo: u32,
|
|
|
|
}
|
|
|
|
|
2019-01-30 19:15:29 -06:00
|
|
|
/// Tests that we do not lint if the underscore is not a prefix
|
2015-12-12 18:31:34 -06:00
|
|
|
fn non_prefix_underscore(some_foo: u32) -> u32 {
|
|
|
|
some_foo + 1
|
2015-12-10 14:54:43 -06:00
|
|
|
}
|
|
|
|
|
2019-01-30 19:15:29 -06:00
|
|
|
/// Tests that we do not lint if we do not use the binding (simple case)
|
2015-12-18 18:04:33 -06:00
|
|
|
fn unused_underscore_simple(_foo: u32) -> u32 {
|
|
|
|
1
|
|
|
|
}
|
|
|
|
|
2019-01-30 19:15:29 -06:00
|
|
|
/// Tests that we do not lint if we do not use the binding (complex case). This checks for
|
2015-12-18 18:04:33 -06:00
|
|
|
/// compatibility with the built-in `unused_variables` lint.
|
|
|
|
fn unused_underscore_complex(mut _foo: u32) -> u32 {
|
|
|
|
_foo += 1;
|
|
|
|
_foo = 2;
|
2015-12-12 18:31:34 -06:00
|
|
|
1
|
2015-12-10 14:54:43 -06:00
|
|
|
}
|
2015-12-12 18:31:34 -06:00
|
|
|
|
2019-04-20 02:33:13 -05:00
|
|
|
/// Test that we do not lint for multiple underscores
|
2015-12-18 18:04:33 -06:00
|
|
|
fn multiple_underscores(__foo: u32) -> u32 {
|
|
|
|
__foo + 1
|
2015-12-18 15:47:12 -06:00
|
|
|
}
|
|
|
|
|
2015-12-17 15:52:30 -06:00
|
|
|
// Non-variable bindings with preceding underscore
|
|
|
|
fn _fn_test() {}
|
|
|
|
struct _StructTest;
|
|
|
|
enum _EnumTest {
|
2016-02-01 05:51:33 -06:00
|
|
|
_Empty,
|
2018-12-09 16:26:16 -06:00
|
|
|
_Value(_StructTest),
|
2015-12-17 15:52:30 -06:00
|
|
|
}
|
|
|
|
|
2019-01-30 19:15:29 -06:00
|
|
|
/// Tests that we do not lint for non-variable bindings
|
2015-12-17 15:52:30 -06:00
|
|
|
fn non_variables() {
|
|
|
|
_fn_test();
|
|
|
|
let _s = _StructTest;
|
2016-02-01 05:51:33 -06:00
|
|
|
let _e = match _EnumTest::_Value(_StructTest) {
|
|
|
|
_EnumTest::_Empty => 0,
|
|
|
|
_EnumTest::_Value(_st) => 1,
|
2015-12-17 15:52:30 -06:00
|
|
|
};
|
|
|
|
let f = _fn_test;
|
|
|
|
f();
|
|
|
|
}
|
2020-04-27 14:12:39 -05:00
|
|
|
|
|
|
|
// Tests that we do not lint if the binding comes from await desugaring,
|
|
|
|
// but we do lint the awaited expression. See issue 5360.
|
2020-04-26 17:12:51 -05:00
|
|
|
async fn await_desugaring() {
|
|
|
|
async fn foo() {}
|
2020-04-27 14:12:39 -05:00
|
|
|
fn uses_i(_i: i32) {}
|
|
|
|
|
2020-04-26 17:12:51 -05:00
|
|
|
foo().await;
|
2020-04-27 14:12:39 -05:00
|
|
|
({
|
|
|
|
let _i = 5;
|
|
|
|
uses_i(_i);
|
|
|
|
foo()
|
|
|
|
})
|
|
|
|
.await
|
2020-04-26 17:12:51 -05:00
|
|
|
}
|
2015-12-17 15:52:30 -06:00
|
|
|
|
2023-09-25 04:28:58 -05:00
|
|
|
struct PhantomField<T> {
|
|
|
|
_marker: std::marker::PhantomData<T>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> std::fmt::Debug for PhantomField<T> {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
|
|
|
f.debug_struct("PhantomField").field("_marker", &self._marker).finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct AllowedField {
|
|
|
|
#[allow(clippy::used_underscore_binding)]
|
|
|
|
_allowed: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ExpectedField {
|
|
|
|
#[expect(clippy::used_underscore_binding)]
|
|
|
|
_expected: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn lint_levels(allowed: AllowedField, expected: ExpectedField) {
|
|
|
|
let _ = allowed._allowed;
|
|
|
|
let _ = expected._expected;
|
|
|
|
}
|
|
|
|
|
2015-12-12 18:31:34 -06:00
|
|
|
fn main() {
|
|
|
|
let foo = 0u32;
|
|
|
|
// tests of unused_underscore lint
|
|
|
|
let _ = prefix_underscore(foo);
|
2019-05-11 22:40:05 -05:00
|
|
|
in_macro_or_desugar(foo);
|
2015-12-18 18:29:22 -06:00
|
|
|
in_struct_field();
|
2015-12-12 18:31:34 -06:00
|
|
|
// possible false positives
|
|
|
|
let _ = non_prefix_underscore(foo);
|
2015-12-18 18:04:33 -06:00
|
|
|
let _ = unused_underscore_simple(foo);
|
|
|
|
let _ = unused_underscore_complex(foo);
|
2015-12-18 15:47:12 -06:00
|
|
|
let _ = multiple_underscores(foo);
|
2015-12-17 15:52:30 -06:00
|
|
|
non_variables();
|
2020-04-26 17:12:51 -05:00
|
|
|
await_desugaring();
|
2015-12-12 18:31:34 -06:00
|
|
|
}
|