rust/tests/ui/used_underscore_binding.rs

92 lines
2.2 KiB
Rust
Raw Normal View History

2018-07-28 23:34:52 +08:00
#![warn(clippy::all)]
#![allow(clippy::blacklisted_name)]
#![warn(clippy::used_underscore_binding)]
2016-02-22 22:42:24 +08:00
2016-06-15 22:27:56 +08:00
macro_rules! test_macro {
() => {{
let _foo = 42;
_foo + 1
2018-12-10 06:26:16 +08:00
}};
2016-06-15 22:27:56 +08:00
}
2019-01-31 09:15:29 +08:00
/// Tests that we lint if we use a binding with a single leading underscore
fn prefix_underscore(_foo: u32) -> u32 {
2017-02-08 21:58:07 +08:00
_foo + 1
2015-12-11 04:54:43 +08:00
}
2019-01-31 09:15:29 +08:00
/// Tests that we lint if we use a `_`-variable defined outside within a macro expansion
fn in_macro(_foo: u32) {
2016-06-15 22:27:56 +08:00
println!("{}", _foo);
assert_eq!(_foo, _foo);
2017-02-08 21:58:07 +08:00
2016-06-15 22:27:56 +08:00
test_macro!() + 1;
}
2015-12-19 08:29:22 +08:00
// Struct for testing use of fields prefixed with an underscore
struct StructFieldTest {
_underscore_field: u32,
}
2019-01-31 09:15:29 +08:00
/// Tests that we lint the use of a struct field which is prefixed with an underscore
2015-12-19 08:29:22 +08:00
fn in_struct_field() {
let mut s = StructFieldTest { _underscore_field: 0 };
2017-02-08 21:58:07 +08:00
s._underscore_field += 1;
2015-12-19 08:29:22 +08:00
}
2019-01-31 09:15:29 +08:00
/// Tests that we do not lint if the underscore is not a prefix
2015-12-13 08:31:34 +08:00
fn non_prefix_underscore(some_foo: u32) -> u32 {
some_foo + 1
2015-12-11 04:54:43 +08:00
}
2019-01-31 09:15:29 +08:00
/// Tests that we do not lint if we do not use the binding (simple case)
fn unused_underscore_simple(_foo: u32) -> u32 {
1
}
2019-01-31 09:15:29 +08:00
/// Tests that we do not lint if we do not use the binding (complex case). This checks for
/// compatibility with the built-in `unused_variables` lint.
fn unused_underscore_complex(mut _foo: u32) -> u32 {
_foo += 1;
_foo = 2;
2015-12-13 08:31:34 +08:00
1
2015-12-11 04:54:43 +08:00
}
2015-12-13 08:31:34 +08:00
///Test that we do not lint for multiple underscores
fn multiple_underscores(__foo: u32) -> u32 {
__foo + 1
}
2015-12-18 05:52:30 +08:00
// Non-variable bindings with preceding underscore
fn _fn_test() {}
struct _StructTest;
enum _EnumTest {
2016-02-01 19:51:33 +08:00
_Empty,
2018-12-10 06:26:16 +08:00
_Value(_StructTest),
2015-12-18 05:52:30 +08:00
}
2019-01-31 09:15:29 +08:00
/// Tests that we do not lint for non-variable bindings
2015-12-18 05:52:30 +08:00
fn non_variables() {
_fn_test();
let _s = _StructTest;
2016-02-01 19:51:33 +08:00
let _e = match _EnumTest::_Value(_StructTest) {
_EnumTest::_Empty => 0,
_EnumTest::_Value(_st) => 1,
2015-12-18 05:52:30 +08:00
};
let f = _fn_test;
f();
}
2015-12-13 08:31:34 +08:00
fn main() {
let foo = 0u32;
// tests of unused_underscore lint
let _ = prefix_underscore(foo);
in_macro(foo);
2015-12-19 08:29:22 +08:00
in_struct_field();
2015-12-13 08:31:34 +08:00
// possible false positives
let _ = non_prefix_underscore(foo);
let _ = unused_underscore_simple(foo);
let _ = unused_underscore_complex(foo);
let _ = multiple_underscores(foo);
2015-12-18 05:52:30 +08:00
non_variables();
2015-12-13 08:31:34 +08:00
}