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

[Fix #12233] Make Style/SlicingWithRange aware of beginless range #12594

Conversation

koic
Copy link
Member

@koic koic commented Jan 5, 2024

Fixes #12233.

This PR makes Style/SlicingWithRange aware of redundant and beginless range.

Ruby 2.7 introduced beginless ranges. But, unlike the somewhat obscure -1 in ary[1..-1], the 0 in ary[0..42] is clear as a starting point. In fact, changing it to ary[..42] could potentially make it less readable. Therefore, ary[0..42] should respect the original programmer's intent. On the other hand, ary[nil..42] could be replaced with ary[..42]. Similarly, ary[1..nil] could be replaced with ary[1..].

Moreover, [0..-1] in ary[0..-1] is redundant and simply synonymous with ary. This PR has also made adjustments to detect this case. While this is a byproduct of supporting beginless and endless ranges, I think including this change in Style/SlicingWithRange cop may be suitable and not feel out of place.


Before submitting the PR make sure the following are checked:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.

…range

Fixes rubocop#12233.

This PR makes `Style/SlicingWithRange` aware of redundant and beginless range.

Ruby 2.7 introduced beginless ranges. But, unlike the somewhat obscure `-1` in
`ary[1..-1]`, the `0` in `ary[0..42]` is clear as a starting point.
In fact, changing it to `ary[..42]` could potentially make it less readable.
Therefore, `ary[0..42]` should respect the original programmer's intent.
On the other hand, `ary[nil..42]` could be replaced with `ary[..42]`.
Similarly, `ary[1..nil]` could be replaced with `ary[1..]`.

Moreover, `[0..-1]` in `ary[0..-1]` is redundant and simply synonymous with `ary`.
This PR has also made adjustments to detect this case.
While this is a byproduct of supporting beginless and endless ranges,
I think including this change in `Style/SlicingWithRange` cop may be suitable
and not feel out of place.
@bbatsov
Copy link
Collaborator

bbatsov commented Jan 11, 2024

I'm fine with the proposed changes, but I'd suggest to also add an entry to the style guide about this.

@bbatsov bbatsov merged commit 39484b8 into rubocop:master Jan 11, 2024
33 checks passed
@koic koic deleted the make_style_slicing_with_range_aware_of_beginless_range branch January 11, 2024 07:03
koic added a commit to koic/ruby-style-guide that referenced this pull request Jan 12, 2024
Follow up rubocop/rubocop#12594 (comment).

This PR adds "Slicing with Ranges" rule.
koic added a commit to koic/ruby-style-guide that referenced this pull request Jan 12, 2024
Follow up rubocop/rubocop#12594 (comment).

This PR adds "Slicing with Ranges" rule.
bbatsov pushed a commit to rubocop/ruby-style-guide that referenced this pull request Jan 13, 2024
Follow up rubocop/rubocop#12594 (comment).

This PR adds "Slicing with Ranges" rule.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve Style/SlicingWithRange cop
2 participants