Skip to content

Commit

Permalink
Merge pull request #366 from koic/fix_false_positives_for_performance…
Browse files Browse the repository at this point in the history
…_array_semi_infinite_range_slice

[Fix #365] Fix false positives for `Performance/ArraySemiInfiniteRangeSlice`
  • Loading branch information
koic authored Aug 22, 2023
2 parents 728c964 + ed5c108 commit 7d7de79
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 7d7de79

Please sign in to comment.