Skip to content

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

Closed
@aseroff

Description

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

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