class RuboCop::Cop::Style::BitwisePredicate
Prefer bitwise predicate methods over direct comparison operations.
@safety
This cop is unsafe, as it can produce false positives if the receiver is not an `Integer` object.
@example
# bad - checks any set bits (variable & flags).positive? # good variable.anybits?(flags) # bad - checks all set bits (variable & flags) == flags # good variable.allbits?(flags) # bad - checks no set bits (variable & flags).zero? # good variable.nobits?(flags)
Constants
- MSG
- RESTRICT_ON_SEND
Public Instance Methods
Source
# File lib/rubocop/cop/style/bitwise_predicate.rb, line 73 def on_send(node) return unless node.receiver&.begin_type? return unless (preferred_method = preferred_method(node)) bit_operation = node.receiver.children.first lhs, _operator, rhs = *bit_operation preferred = "#{lhs.source}.#{preferred_method}(#{rhs.source})" add_offense(node, message: format(MSG, preferred: preferred)) do |corrector| corrector.replace(node, preferred) end end
Private Instance Methods
Source
# File lib/rubocop/cop/style/bitwise_predicate.rb, line 88 def preferred_method(node) if anybits?(node) 'anybits?' elsif allbits?(node) 'allbits?' elsif nobits?(node) 'nobits?' end end