Class: Rubocop::Cop::Style::EvenOdd

Inherits:
Cop
  • Object
show all
Defined in:
lib/rubocop/cop/style/even_odd.rb

Overview

This cop checks for places where Fixnum#even? or Fixnum#odd? should have been used.

Examples:


# bad
if x % 2 == 0

# good
if x.even?

Constant Summary collapse

MSG_EVEN =
'Use Fixnum.even?'
MSG_ODD =
'Use Fixnum.odd?'
ZERO =
s(:int, 0)
ONE =
s(:int, 1)
TWO =
s(:int, 2)

Constants inherited from Cop

Cop::OPERATOR_METHODS

Instance Attribute Summary

Attributes inherited from Cop

#config, #corrections, #offences, #processed_source

Instance Method Summary collapse

Methods inherited from Cop

#add_offence, all, #autocorrect?, #convention, #cop_config, cop_name, #cop_name, cop_type, #debug?, #ignore_node, inherited, #initialize, lint?, #message, non_rails, rails?, style?, #support_autocorrect?, #warning

Constructor Details

This class inherits a constructor from Rubocop::Cop::Cop

Instance Method Details

#on_send(node) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/rubocop/cop/style/even_odd.rb', line 24

def on_send(node)
  receiver, method, args = *node

  return unless [:==, :!=].include?(method)
  return unless div_by_2?(receiver)

  if args == ZERO
    convention(node,
               :expression,
               method == :== ? MSG_EVEN : MSG_ODD)
  elsif args == ONE
    convention(node,
               :expression,
               method == :== ? MSG_ODD : MSG_EVEN)
  end
end