Skip to content

Commit

Permalink
Fix Performance/RedundantStringChars cop error in case of implicit …
Browse files Browse the repository at this point in the history
…receiver

```console
echo 'chars.size' | be rubocop  --only Performance/RedundantStringChars --stdin bug.rb -d

Inspecting 1 file
Scanning /bug.rb
An error occurred while Performance/RedundantStringChars cop was inspecting /bug.rb:1:0.
undefined method `begin_pos' for nil
/lib/rubocop/cop/performance/redundant_string_chars.rb:76:in `correction_range'
/lib/rubocop/cop/performance/redundant_string_chars.rb:62:in `block in on_send'
```
  • Loading branch information
viralpraxis committed Dec 11, 2024
1 parent 378299f commit ccb9241
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#478](https://github.com/rubocop/rubocop-performance/pull/478): Fix `Performance/RedundantStringChars` cop error in case of implicit receiver. ([@viralpraxis][])
2 changes: 1 addition & 1 deletion lib/rubocop/cop/performance/redundant_string_chars.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class RedundantStringChars < Base
RESTRICT_ON_SEND = %i[[] slice first last take length size empty?].freeze

def_node_matcher :redundant_chars_call?, <<~PATTERN
(send $(send _ :chars) $_ $...)
(send $(send !nil? :chars) $_ $...)
PATTERN

def on_send(node)
Expand Down
6 changes: 6 additions & 0 deletions spec/rubocop/cop/performance/redundant_string_chars_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,10 @@
str.chars.max
RUBY
end

it 'does not register an offense with implicit receiver' do
expect_no_offenses(<<~RUBY)
chars.size
RUBY
end
end

0 comments on commit ccb9241

Please sign in to comment.