Skip to content

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

@DannyBen

Description

@DannyBen

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions