rust/tests/compile-fail/used_underscore_binding.rs

56 lines
1.3 KiB
Rust
Raw Normal View History

2015-12-10 21:54:43 +01:00
#![feature(plugin)]
#![plugin(clippy)]
2015-12-13 01:31:34 +01:00
#![deny(clippy)]
2015-12-10 21:54:43 +01:00
2015-12-17 22:52:30 +01:00
/// Test that we lint if we use a binding with a single leading underscore
2015-12-17 00:28:06 +01:00
fn prefix_underscore(_x: u32) -> u32 {
2015-12-13 01:31:34 +01:00
_x + 1 //~ ERROR used binding which is prefixed with an underscore
2015-12-10 21:54:43 +01:00
}
2015-12-17 22:52:30 +01:00
/// Test that we lint even if the use is within a macro expansion
2015-12-13 01:31:34 +01:00
fn in_macro(_x: u32) {
println!("{}", _x); //~ ERROR used binding which is prefixed with an underscore
2015-12-10 21:54:43 +01:00
}
2015-12-17 22:52:30 +01:00
/// Test that we do not lint if the underscore is not a prefix
2015-12-13 01:31:34 +01:00
fn non_prefix_underscore(some_foo: u32) -> u32 {
some_foo + 1
2015-12-10 21:54:43 +01:00
}
2015-12-17 22:52:30 +01:00
/// Test that we do not lint if we do not use the binding
2015-12-13 01:31:34 +01:00
fn unused_underscore(_foo: u32) -> u32 {
1
2015-12-10 21:54:43 +01:00
}
2015-12-13 01:31:34 +01:00
2015-12-17 22:52:30 +01:00
// Non-variable bindings with preceding underscore
fn _fn_test() {}
struct _StructTest;
enum _EnumTest {
_FieldA,
_FieldB(_StructTest)
}
/// Test that we do not lint for non-variable bindings
fn non_variables() {
_fn_test();
let _s = _StructTest;
let _e = match _EnumTest::_FieldB(_StructTest) {
_EnumTest::_FieldA => 0,
_EnumTest::_FieldB(_st) => 1,
};
let f = _fn_test;
f();
}
2015-12-13 01:31:34 +01:00
fn main() {
let foo = 0u32;
// tests of unused_underscore lint
let _ = prefix_underscore(foo);
in_macro(foo);
// possible false positives
let _ = non_prefix_underscore(foo);
let _ = unused_underscore(foo);
2015-12-17 22:52:30 +01:00
non_variables();
2015-12-13 01:31:34 +01:00
}