Class: RuboCop::Cop::Mdsol::UnlessNotEqual

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/mdsol/unless_not_equal.rb

Overview

Avoid double negative condition.

Credit to Warut: github.com/mdsol/tenjin/pull/621#discussion_r748106992

Examples:

# bad
do_something unless a != b

# good
do_something if a == b

Constant Summary collapse

MSG =
"Avoid double negative. Write `%<correction>s` instead."

Instance Method Summary collapse

Instance Method Details

#on_if(node) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rubocop/cop/mdsol/unless_not_equal.rb', line 31

def on_if(node)
  return if node.ternary? || node.if? || node.elsif?

  unless_not_equal_match(node) do |unless_body_node, left, right|
    correction = "if #{left.source} == #{right.source}"
    msg = format(MSG, correction: correction)
    range = range_with_unless_modifier(unless_body_node)

    add_offense(range, message: msg) do |corrector|
      corrector.replace(range, correction)
    end
  end
end