2015-12-10 14:54:43 -06:00
|
|
|
#![feature(plugin)]
|
|
|
|
#![plugin(clippy)]
|
2015-12-12 18:31:34 -06:00
|
|
|
#![deny(clippy)]
|
2015-12-10 14:54:43 -06:00
|
|
|
|
2015-12-17 15:52:30 -06:00
|
|
|
/// Test 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 {
|
|
|
|
_foo + 1 //~ ERROR used binding which is prefixed with an underscore
|
2015-12-10 14:54:43 -06:00
|
|
|
}
|
|
|
|
|
2015-12-17 15:52:30 -06:00
|
|
|
/// Test that we lint even if the use is within a macro expansion
|
2015-12-18 18:04:33 -06:00
|
|
|
fn in_macro(_foo: u32) {
|
2015-12-21 03:03:12 -06:00
|
|
|
println!("{}", _foo); //~ ERROR used binding which is prefixed with an underscore
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: This doesn't actually correctly test this. Need to find a #[derive(...)] which sets off
|
|
|
|
// the lint if the `in_attributes_expansion` test isn't there
|
|
|
|
/// Test that we do not lint for unused underscores in a MacroAttribute expansion
|
|
|
|
#[derive(Clone)]
|
|
|
|
struct MacroAttributesTest {
|
|
|
|
_foo: u32,
|
2015-12-10 14:54:43 -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,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Test that we lint the use of a struct field which is prefixed with an underscore
|
|
|
|
fn in_struct_field() {
|
|
|
|
let mut s = StructFieldTest { _underscore_field: 0 };
|
|
|
|
s._underscore_field += 1; //~ Error used binding which is prefixed with an underscore
|
|
|
|
}
|
|
|
|
|
2015-12-17 15:52:30 -06:00
|
|
|
/// Test 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
|
|
|
}
|
|
|
|
|
2015-12-18 18:04:33 -06:00
|
|
|
/// Test that we do not lint if we do not use the binding (simple case)
|
|
|
|
fn unused_underscore_simple(_foo: u32) -> u32 {
|
|
|
|
1
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Test 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-12 18:31:34 -06:00
|
|
|
1
|
2015-12-10 14:54:43 -06:00
|
|
|
}
|
2015-12-12 18:31:34 -06:00
|
|
|
|
2015-12-18 15:47:12 -06: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 {
|
|
|
|
_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-12 18:31:34 -06:00
|
|
|
fn main() {
|
|
|
|
let foo = 0u32;
|
2015-12-21 03:03:12 -06:00
|
|
|
let _ = MacroAttributesTest{_foo: 0};
|
2015-12-12 18:31:34 -06:00
|
|
|
// tests of unused_underscore lint
|
|
|
|
let _ = prefix_underscore(foo);
|
|
|
|
in_macro(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();
|
2015-12-12 18:31:34 -06:00
|
|
|
}
|