Add new Style/WithIndexOffset cop#14993
Open
justindell wants to merge 1 commit into
Open
Conversation
56b75e0 to
b73a851
Compare
|
This pull request has been automatically marked as stale because it has not had any recent activity. It will be closed soon if no further activity occurs. Thank you for your contribution and understanding! |
Author
|
Any thoughts on this cop from anyone? |
|
This pull request has been automatically marked as stale because it has not had any recent activity. It will be closed soon if no further activity occurs. Thank you for your contribution and understanding! |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Adds a new
Style/WithIndexOffsetcop that detects offset arithmetic on the index variable insideeach_with_indexorwith_indexblocks.When every usage of the index variable in a block applies the same constant offset (e.g.
index + 1), the offset can be passed directly towith_indexinstead.Before
After
Details
The cop flags the following patterns when applied to the index block parameter:
index + N/N + index(integer literal addition)index - N(integer literal subtraction)index.succ/index.next(equivalent to+ 1)index.pred(equivalent to- 1)It only registers an offense when every reference to the index variable in the block body uses the same constant offset. Mixed usage (e.g. both
index + 1and bareindex) is not flagged, which keeps autocorrection safe.Supported call forms
each_with_index { |_, i| i + N }each.with_index(N)each.with_index { |_, i| i + N }each.with_index(N)each.with_index(0) { |_, i| i + N }each.with_index(N)each.with_index(5) { |_, i| i + N }Configuration
Test coverage
23 specs covering:
+ N,N +,- N,.succ,.next,.pred)each_with_index,each.with_index,each.with_index(0))&.)it-parameter blocks (Ruby 3.4+ itblocks)Both Parser and Prism engines pass. The cop also detected and corrected 5 instances in the RuboCop codebase itself.
The cop is safe to disable on a per-line basis for patterns like array subscript peek-ahead where the original form may be more readable.