Skip to content

Commit

Permalink
[Fix #9582] Fix incorrect auto-correct for `Style/ClassEqualityCompar…
Browse files Browse the repository at this point in the history
…ison`

Fixes #9582.

This PR fixes incorrect auto-correct for `Style/ClassEqualityComparison`
when comparing `Module#name` for equality.
  • Loading branch information
koic authored and bbatsov committed Mar 10, 2021
1 parent 93711ca commit 8e4edcd
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
@@ -0,0 +1 @@
* [#9582](https://github.com/rubocop/rubocop/issues/9582): Fix incorrect auto-correct for `Style/ClassEqualityComparison` when comparing `Module#name` for equality. ([@koic][])
2 changes: 2 additions & 0 deletions lib/rubocop/cop/style/class_equality_comparison.rb
Expand Up @@ -50,6 +50,8 @@ def on_send(node)

def class_name(class_node, node)
if node.children.first.method?(:name)
return class_node.receiver.source if class_node.receiver

class_node.source.delete('"').delete("'")
else
class_node.source
Expand Down
11 changes: 11 additions & 0 deletions spec/rubocop/cop/style/class_equality_comparison_spec.rb
Expand Up @@ -49,6 +49,17 @@
RUBY
end

it 'registers an offense and corrects when comparing `Module#name` for equality' do
expect_offense(<<~RUBY)
var.class.name == Date.name
^^^^^^^^^^^^^^^^^^^^^^^ Use `instance_of?(Date)` instead of comparing classes.
RUBY

expect_correction(<<~RUBY)
var.instance_of?(Date)
RUBY
end

it 'registers an offense and corrects when comparing double quoted class name for equality' do
expect_offense(<<~RUBY)
var.class.name == "Date"
Expand Down

0 comments on commit 8e4edcd

Please sign in to comment.