Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash for Style/CaseLikeIf when checking against equal? and match? without a receiver #8345

Merged
merged 1 commit into from Jul 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,7 @@
### Bug fixes

* [#8324](https://github.com/rubocop-hq/rubocop/issues/8324): Fix crash for `Layout/SpaceAroundMethodCallOperator` when using `Proc#call` shorthand syntax. ([@fatkodima][])
* [#8344](https://github.com/rubocop-hq/rubocop/issues/8344): Fix crash for `Style/CaseLikeIf` when checking against `equal?` and `match?` without a receiver. ([@fatkodima][])
* [#8323](https://github.com/rubocop-hq/rubocop/issues/8323): Fix a false positive for `Style/HashAsLastArrayItem` when hash is not a last array item. ([@fatkodima][])
* [#8299](https://github.com/rubocop-hq/rubocop/issues/8299): Fix an incorrect auto-correct for `Style/RedundantCondition` when using `raise`, `rescue`, or `and` without argument parentheses in `else`. ([@koic][])
* [#8335](https://github.com/rubocop-hq/rubocop/issues/8335): Fix incorrect character class detection for nested or POSIX bracket character classes in `Style/RedundantRegexpEscape`. ([@owst][])
Expand Down
2 changes: 2 additions & 0 deletions lib/rubocop/cop/style/case_like_if.rb
Expand Up @@ -112,6 +112,7 @@ def find_target_in_send_node(node)
def find_target_in_equality_node(node)
argument = node.arguments.first
receiver = node.receiver
return unless receiver

if argument.literal? || const_reference?(argument)
receiver
Expand All @@ -123,6 +124,7 @@ def find_target_in_equality_node(node)
def find_target_in_match_node(node)
argument = node.arguments.first
receiver = node.receiver
return unless receiver

if receiver.regexp_type?
argument
Expand Down
18 changes: 18 additions & 0 deletions spec/rubocop/cop/style/case_like_if_spec.rb
Expand Up @@ -86,6 +86,15 @@
RUBY
end

it 'does not register an offense when using `equal?` without a receiver' do
expect_no_offenses(<<~RUBY)
if equal?(Foo)
elsif Bar == x
else
end
RUBY
end

it 'registers an offense and corrects when using `is_a?`' do
expect_offense(<<~RUBY)
if x.is_a?(Foo)
Expand Down Expand Up @@ -131,6 +140,15 @@
RUBY
end

it 'does not register an offense when using `match?` without a receiver' do
expect_no_offenses(<<~RUBY)
if match?(/foo/)
elsif x.match?(/bar/)
else
end
RUBY
end

it 'registers an offense and corrects when using `=~`' do
expect_offense(<<~RUBY)
if /foo/ =~ x
Expand Down