Skip to content
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_out_of_bounds #11396

Merged
merged 3 commits into from Aug 30, 2023
Merged

new lint: iter_out_of_bounds #11396

merged 3 commits into from Aug 30, 2023

Conversation

y21
Copy link
Member

@y21 y21 commented Aug 24, 2023

Closes #11345

The original idea in the linked issue seemed to be just about arrays afaict, but I extended this to catch some other iterator sources such as iter::once or iter::empty.

I'm not entirely sure if this name makes a lot of sense now that it's not just about arrays anymore (specifically, not sure if you can call .take(1) on an iter::Empty to be "out of bounds"?).

changelog: [iter_out_of_bounds]: new lint

@rustbot
Copy link
Collaborator

rustbot commented Aug 24, 2023

r? @Jarcho

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Aug 24, 2023
clippy_lints/src/methods/iter_out_of_bounds.rs Outdated Show resolved Hide resolved
clippy_lints/src/methods/iter_out_of_bounds.rs Outdated Show resolved Hide resolved
clippy_lints/src/methods/iter_out_of_bounds.rs Outdated Show resolved Hide resolved
clippy_lints/src/methods/mod.rs Outdated Show resolved Hide resolved
tests/ui/iter_out_of_bounds.rs Outdated Show resolved Hide resolved
&& let Some(skipped) = expr_as_u128(cx, arg)
&& skipped > len
{
span_lint_and_note(cx, ITER_OUT_OF_BOUNDS, expr.span, message, None, note);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This really should have a MaybeIncorrect suggestion added to it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what the suggestion should be or if it makes sense to have one. At least for something like [1,2,3].iter().take(4) it could probably suggest removing the .take() call as that'll still yield the same items with or without it, but I don't know what the user could have meant with [1,2,3].iter().skip(4). Should it suggest and empty iterator? 🤔

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was only considering take here. Given the suggestion isn't going to have a lot of value (decent chance of being the wrong fix), I guess it's fine to not have one.

Copy link
Contributor

@Jarcho Jarcho left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should have some more negative tests added. e.g. [1, 2, 3].iter().take(2)

@Jarcho
Copy link
Contributor

Jarcho commented Aug 30, 2023

Thank you. @bors r+

@bors
Copy link
Collaborator

bors commented Aug 30, 2023

📌 Commit 33cc140 has been approved by Jarcho

It is now in the queue for this repository.

@bors
Copy link
Collaborator

bors commented Aug 30, 2023

⌛ Testing commit 33cc140 with merge 3da21b0...

@bors
Copy link
Collaborator

bors commented Aug 30, 2023

☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test
Approved by: Jarcho
Pushing 3da21b0 to master...

@bors bors merged commit 3da21b0 into rust-lang:master Aug 30, 2023
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Lint when .take(x) or .skip(x) is out of bounds for an array
5 participants