Skip to content

Commit

Permalink
[Fix rubocop#365] Fix false positives for `Performance/ArraySemiInfin…
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
koic committed Aug 19, 2023
1 parent 728c964 commit ed5c108
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#365](https://github.com/rubocop/rubocop-performance/issues/365): Fix false positives for `Performance/ArraySemiInfiniteRangeSlice` when using `[]` with string literals. ([@koic][])
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class ArraySemiInfiniteRangeSlice < Base
RESTRICT_ON_SEND = SLICE_METHODS

def_node_matcher :endless_range_slice?, <<~PATTERN
(call $_ $%SLICE_METHODS $#endless_range?)
(call $!{str dstr xstr} $%SLICE_METHODS $#endless_range?)
PATTERN

def_node_matcher :endless_range?, <<~PATTERN
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,23 @@
array[-2..]
RUBY
end

it 'does not registers an offense when using `[]` with endless range for string literal' do
expect_no_offenses(<<~RUBY)
'str'[3..]
RUBY
end

it 'does not registers an offense when using `[]` with endless range for interpolated string literal' do
expect_no_offenses(<<~'RUBY')
"string#{interpolation}"[3..]
RUBY
end

it 'does not registers an offense when using `[]` with endless range for backquote string literal' do
expect_no_offenses(<<~RUBY)
`str`[3..]
RUBY
end
end
end

0 comments on commit ed5c108

Please sign in to comment.