-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add used_underscore_binding lint #499
Merged
Manishearth
merged 14 commits into
rust-lang:master
from
devonhollowood:underscore_binding
Dec 19, 2015
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
6482840
Add tests
devonhollowood 9de308e
Add used_underscore_binding lint
devonhollowood 43b96d5
Run update_lints.py
devonhollowood 6091112
Update tests
devonhollowood aeb5a0e
Reduce false positives
devonhollowood 92fba6b
Make clippy tests compatible with new lint
devonhollowood b24e3ae
Add wiki docs, in line with #492
devonhollowood 6960bf2
Make ExprField follow single-underscore rules
devonhollowood e620a1d
Make suggested changes
devonhollowood 3533d3a
Add more tests
devonhollowood 02cb24d
Remove local variable check
devonhollowood c8d78a7
Test that we do not lint for multiple underscores
devonhollowood 98d21f9
Make compatible with `unused_variables` lint
devonhollowood bd82c08
Add test for struct fields
devonhollowood File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
#![feature(plugin)] | ||
#![plugin(clippy)] | ||
#![deny(clippy)] | ||
|
||
/// Test that we lint if we use a binding with a single leading underscore | ||
fn prefix_underscore(_foo: u32) -> u32 { | ||
_foo + 1 //~ ERROR used binding which is prefixed with an underscore | ||
} | ||
|
||
/// Test that we lint even if the use is within a macro expansion | ||
fn in_macro(_foo: u32) { | ||
println!("{}", _foo); //~ ERROR used binding which is prefixed with an underscore | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. \o/ |
||
} | ||
|
||
// 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 | ||
} | ||
|
||
/// Test that we do not lint if the underscore is not a prefix | ||
fn non_prefix_underscore(some_foo: u32) -> u32 { | ||
some_foo + 1 | ||
} | ||
|
||
/// 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; | ||
1 | ||
} | ||
|
||
///Test that we do not lint for multiple underscores | ||
fn multiple_underscores(__foo: u32) -> u32 { | ||
__foo + 1 | ||
} | ||
|
||
// 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(); | ||
} | ||
|
||
fn main() { | ||
let foo = 0u32; | ||
// tests of unused_underscore lint | ||
let _ = prefix_underscore(foo); | ||
in_macro(foo); | ||
in_struct_field(); | ||
// possible false positives | ||
let _ = non_prefix_underscore(foo); | ||
let _ = unused_underscore_simple(foo); | ||
let _ = unused_underscore_complex(foo); | ||
let _ = multiple_underscores(foo); | ||
non_variables(); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh I like this way of calculating is_used; smart 😄 !