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

RedundantStringChars auto-correct #249

Closed
jaycetde opened this issue May 20, 2021 · 2 comments · Fixed by #250
Closed

RedundantStringChars auto-correct #249

jaycetde opened this issue May 20, 2021 · 2 comments · Fixed by #250
Labels
bug Something isn't working

Comments

@jaycetde
Copy link

str = ''

# Existing code:
str.chars.last(2)
  # => []

# Rubocop suggested and auto-corrected to:
str[-2..-1].chars
  # => NoMethodError (undefined method `chars' for nil:NilClass)
# Not equivalent to `str.chars.last(2)` because below returns `nil`
str[-2..-1]
  # => nil

Expected behavior

Do not set this cop as auto-correctable as it can cause breakage

Actual behavior

Auto-correct transforms into potentially broken code

Steps to reproduce the problem

NA

RuboCop version

$ [bundle exec] rubocop -V
1.14.0 (using Parser 3.0.1.1, rubocop-ast 1.5.0, running on ruby 2.5.7 x86_64-linux)
  - rubocop-performance 1.6.1
  - rubocop-rails 2.5.2
@koic koic added the bug Something isn't working label May 21, 2021
koic added a commit to koic/rubocop-performance that referenced this issue May 21, 2021
…ngChars`

Fixes rubocop#249.

This PR fixes a false positive for `Performance/RedundantStringChars` when
using `str.chars.last` and `str.chars.drop`.
tejasbubane added a commit to tejasbubane/rubocop-performance that referenced this issue May 21, 2021
koic added a commit to koic/rubocop-performance that referenced this issue May 22, 2021
…ngChars`

Fixes rubocop#249.

This PR fixes a false positive for `Performance/RedundantStringChars` when
using `str.chars.last` and `str.chars.drop`.
@koic koic closed this as completed in #250 May 23, 2021
koic added a commit that referenced this issue May 23, 2021
…ring_chars

[Fix #249] Fix a false positive for `Performance/RedundantStringChars`
@jjb
Copy link

jjb commented Nov 17, 2021

I experienced string.chars[0]string[0].chars

rubocop-performance 1.11.5 (held back by standardrb)

koic added a commit to koic/rubocop-performance that referenced this issue Nov 18, 2021
Follow up to rubocop#249 (comment).

This PR fixes an incorrect autocorrect for `Performance/RedundantStringChars`
when using `str.chars[0]`.
@koic
Copy link
Member

koic commented Nov 18, 2021

@jjb Thank you for the feedback. I've opened #273.

koic added a commit to koic/rubocop-performance that referenced this issue Nov 18, 2021
Follow up to rubocop#249 (comment).

This PR fixes an incorrect autocorrect for `Performance/RedundantStringChars`
when using `str.chars[0]`.
patrickm53 pushed a commit to patrickm53/performance-develop-rubyonrails that referenced this issue Sep 23, 2022
Follow up to rubocop/rubocop-performance#249 (comment).

This PR fixes an incorrect autocorrect for `Performance/RedundantStringChars`
when using `str.chars[0]`.
richardstewart0213 added a commit to richardstewart0213/performance-build-Performance-optimization-analysis- that referenced this issue Nov 4, 2022
Follow up to rubocop/rubocop-performance#249 (comment).

This PR fixes an incorrect autocorrect for `Performance/RedundantStringChars`
when using `str.chars[0]`.
koic added a commit to koic/rubocop-performance that referenced this issue Nov 21, 2022
…ngChars`

Fixes rubocop#313.

This PR fixes a false negative for `Performance/RedundantStringChars`
when using `str.chars.last` without argument.

Report rubocop#249 has `str.chars.last(2)` argument. rubocop#250 mistakenly changed
even without arguments `str.chars.last`.
Cute0110 added a commit to Cute0110/Rubocop-Performance that referenced this issue Sep 28, 2023
Follow up to rubocop/rubocop-performance#249 (comment).

This PR fixes an incorrect autocorrect for `Performance/RedundantStringChars`
when using `str.chars[0]`.
SerhiiMisiura added a commit to SerhiiMisiura/Rubocop-Performance that referenced this issue Oct 5, 2023
Follow up to rubocop/rubocop-performance#249 (comment).

This PR fixes an incorrect autocorrect for `Performance/RedundantStringChars`
when using `str.chars[0]`.
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
3 participants