-
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
New Lint: Iter nth zero #4966
New Lint: Iter nth zero #4966
Conversation
315667e
to
feaf179
Compare
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.
@krishna-veerareddy is correct with his review, that this will lint on every method called nth
.
For lints like this, we already have infrastructure. You can just add ["nth", ..]
below this statements:
rust-clippy/clippy_lints/src/methods/mod.rs
Lines 1194 to 1195 in c807fbc
["nth", "iter"] => lint_iter_nth(cx, expr, arg_lists[1], false), | |
["nth", "iter_mut"] => lint_iter_nth(cx, expr, arg_lists[1], true), |
and implement a function, that checks for the lint. To verify that this function is from the Iterator
trait, you can add a check like this:
rust-clippy/clippy_lints/src/methods/mod.rs
Lines 2105 to 2106 in c807fbc
// lint if caller of skip is an Iterator | |
if match_trait_method(cx, expr, &paths::ITERATOR) { |
So steps to more easily implement the lint and addressing all review comments:
- add the
declare_clippy_lint
to `methods/mod.rs - add
["nth", ..]
to the above mentioned match statement - add a new module for this lint in
methods
and implement your check function, with the above mentionedIterator
check and the check for the second (1
) argument for constant0
.
feaf179
to
e059583
Compare
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.
LGTM overall
e059583
to
d42cc8c
Compare
I made the requested changes, however after rebasing on master sometime yesterday I could not compile
This happens in ~900 other places as well. |
I have replicated the issue even when cloning a new repo from rust-lang:rust-clippy. Here's my rustc version:
I'm only pointing this out because I can't build locally in order ensure all the tests pass |
@bradsherman It's because there is a difference between nightly and master rustc. Clippy is built for master, not nightly. You can use |
d42cc8c
to
9c9e1dd
Compare
@JohnTitor thanks, that fixed it. Sorry I missed that in the docs. |
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.
Thanks! LGTM, just a NIT with the documentation
9c9e1dd
to
ce9385e
Compare
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.
Thanks! r=me with CI green
ce9385e
to
6d22fbe
Compare
- Encourage iter.next() rather than iter.nth(0), which is less readable
6d22fbe
to
ab5ff03
Compare
@bors r+ Thanks! |
📌 Commit ab5ff03 has been approved by |
New Lint: Iter nth zero Check for the use of `iter.nth(0)` and encourage `iter.next()` instead as it is more readable changelog: add new lint when `iter.nth(0)` is used Fixes #4957
☀️ Test successful - checks-travis, status-appveyor |
thanks all for helping with my first open source contribution! 😃🎉 |
Check for the use of
iter.nth(0)
and encourageiter.next()
instead as it is more readablechangelog: add new lint when
iter.nth(0)
is usedFixes #4957