Skip to content

Commit

Permalink
[Fix #12461] Make Style/StringChars aware of safe navigation operator
Browse files Browse the repository at this point in the history
Fixes #12461.

This PR makes `Style/StringChars` aware of safe navigation operator.
  • Loading branch information
koic authored and bbatsov committed Dec 4, 2023
1 parent 720d05d commit a41cc46
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
@@ -0,0 +1 @@
* [#12461](https://github.com/rubocop/rubocop/issues/12461): Make `Style/StringChars` aware of safe navigation operator. ([@koic][])
1 change: 1 addition & 0 deletions lib/rubocop/cop/style/string_chars.rb
Expand Up @@ -35,6 +35,7 @@ def on_send(node)
corrector.replace(range, 'chars')
end
end
alias on_csend on_send
end
end
end
Expand Down
11 changes: 11 additions & 0 deletions spec/rubocop/cop/style/string_chars_spec.rb
Expand Up @@ -45,6 +45,17 @@
RUBY
end

it 'registers and corrects an offense when using safe navigation `split(//)` call' do
expect_offense(<<~RUBY)
string&.split(//)
^^^^^^^^^ Use `chars` instead of `split(//)`.
RUBY

expect_correction(<<~RUBY)
string&.chars
RUBY
end

it 'does not register an offense when using `chars`' do
expect_no_offenses(<<~RUBY)
string.chars
Expand Down

0 comments on commit a41cc46

Please sign in to comment.