-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New lint: slice.iter().next() can be replaced with slice.get() #5572
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
Comments
Hello, I'm still new to open source development and I'm interested in working on this issue. What I understand from reading the description:
Is this correct? If yes, I can start working on this. Thank you! |
@esamudera Yep. You might want to handle things that For Here's a sample test case: fn main() {
let s = [1, 2, 3];
let v = vec![1, 2, 3];
let o = Some(5);
s.iter().next();
// Should be replaced by s.first()
s.into_iter().next();
// Probaby can rely on the default `array_into_iter`/`clippy::into_iter_on_ref` lint
s[1..].iter().next();
// Should be replaced by s.get(1)
s[1..].into_iter().next();
// Probaby can rely on the default `clippy::into_iter_on_ref` lint
v[1..].iter().next();
// Should be replaced by v.get(1)
v[1..].into_iter().next();
// Probaby can rely on the default `clippy::into_iter_on_ref` lint
v.iter().next();
// Should be replaced by v.first()
v.into_iter().next();
// Tricky one—I don't think there's any other (short) way to say this (take ownership of first value)
// So this should probably not get any warnings
o.iter().next();
// Replace with o.as_ref()
o.into_iter().next();
// Replace with o
} |
Okay, thank you for the additional contexts, I will start by making a new clippy_lint struct and see where it goes from there :) |
Hi, guys! I think this idea should be split into two lints.
Also the second one can be improved with handling such cases like |
Thanks @alex-700, I didn't know that https://github.com/rust-lang/rust-clippy/blob/master/clippy_lints/src/methods/mod.rs#L1352 exists, and made a new lint struct checking method call expressions and its parents from scratch. I'll use that module instead. For now I'm trying to implement lint for vector slice with I'm still very new to this codebase, is this issue an urgent one? Because I need some time to study the code and learn what |
New lint: iter_next_slice Hello, this is a work-in-progress PR for issue: #5572 I have implemented lint to replace `iter().next()` for `slice[index..]` and `array` with `get(index)` and `get(0)` respectively. However since I made a lot of changes, I would like to request some feedback before continuing so that I could fix mistakes. Thank you! --- changelog: - implement iter next slice lint and test - modify needless_continues, for_loop_over_options_result UI tests since they have `iter().next()`
New lint: iter_next_slice Hello, this is a work-in-progress PR for issue: #5572 I have implemented lint to replace `iter().next()` for `slice[index..]` and `array` with `get(index)` and `get(0)` respectively. However since I made a lot of changes, I would like to request some feedback before continuing so that I could fix mistakes. Thank you! --- changelog: implement `iter_next_slice` lint and test, and modify `needless_continues`, `for_loop_over_options_result` UI tests since they have `iter().next()`
Hi, is this issue already resolved? If so, it would be better to close this. |
Closing in favor of #5597. |
As I saw in this code review, someone wrote:
That should be replaced with:
In general,
slice[i..].iter().next()
can be replaced withslice.get(i)
, or0
if there is no slicing done.The text was updated successfully, but these errors were encountered: