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

Performance/RedundantStringChars: Does not detect string.chars.last #313

Closed
DannyBen opened this issue Nov 19, 2022 · 0 comments · Fixed by #315
Closed

Performance/RedundantStringChars: Does not detect string.chars.last #313

DannyBen opened this issue Nov 19, 2022 · 0 comments · Fixed by #315
Labels
bug Something isn't working

Comments

@DannyBen
Copy link

Expected behavior

Expected the cop to report string.chars.last and autocorrect it to string[-1].

Actual behavior

The cop only corrects string.chars.first and does not report string.chars.last as an offense.

Steps to reproduce the problem

Create this rubocop.yml:

require: rubocop-performance

AllCops:
  NewCops: enable
  TargetRubyVersion: 3.0

Create this test.rb file:

# frozen_string_literal: true

string = 'AZ'

p string.chars.first
p string.chars.last

Run rubocop or rubocop -A. The autocorrected output will be:

# frozen_string_literal: true

string = 'AZ'

p string[0]
p string.chars.last

RuboCop version

$ rubocop -V
1.39.0 (using Parser 3.1.2.1, rubocop-ast 1.23.0, running on ruby 3.1.0) [x86_64-linux]
  - rubocop-performance 1.15.1
@koic koic added the bug Something isn't working label Nov 21, 2022
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`.
@koic koic closed this as completed in #315 Dec 2, 2022
koic added a commit that referenced this issue Dec 2, 2022
…e_redundant_string_chars

[Fix #313] Fix a false negative for `Performance/RedundantStringChars`
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