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

ArraySemiInfiniteRangeSlice detects and autocorrects on Strings with methods not available to String #365

Closed
aseroff opened this issue Aug 15, 2023 · 0 comments · Fixed by #366
Labels
bug Something isn't working

Comments

@aseroff
Copy link

aseroff commented Aug 15, 2023

Expected behavior

Based on the cop name, it shouldn't detect instances where the method [] with a semi-infinite range is called on Strings.

Actual behavior

C: [Correctable] Performance/ArraySemiInfiniteRangeSlice: Use drop instead of [] with semi-infinite range.

Which autocorrects "string"[3..] to string.drop(3), which causes undefined method 'drop' for String (NoMethodError)

Steps to reproduce the problem

Use [] with a semi-infinite range on a string, e.g. "string"[3..]

RuboCop version

$ [bundle exec] rubocop -V
1.56.0 (using Parser 3.2.2.3, rubocop-ast 1.29.0, running on ruby 3.1.4) [x86_64-darwin21]
  - rubocop-capybara 2.18.0
  - rubocop-minitest 0.31.0
  - rubocop-performance 1.19.0
  - rubocop-rails 2.20.2
  - rubocop-rake 0.6.0
  - rubocop-thread_safety 0.5.1
@koic koic transferred this issue from rubocop/rubocop Aug 15, 2023
@koic koic added the bug Something isn't working label Aug 16, 2023
koic added a commit to koic/rubocop-performance that referenced this issue Aug 19, 2023
…iteRangeSlice`

Fixes rubocop#365.

This PR fixes false positives for `Performance/ArraySemiInfiniteRangeSlice`
when using `[]` with string literals.
What this can do is accept string literals, as they are already marked as unsafe.
@koic koic closed this as completed in #366 Aug 22, 2023
koic added a commit that referenced this issue Aug 22, 2023
…_array_semi_infinite_range_slice

[Fix #365] Fix false positives for `Performance/ArraySemiInfiniteRangeSlice`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants