Sha256: ec51b3845a651e0c471a12e4d8d2ecf6f89dc7767b2e6fd868bd5f62e5ffe16c

Contents?: true

Size: 912 Bytes

Versions: 4

Compression:

Stored size: 912 Bytes

Contents

# encoding: utf-8

module Rubocop
  module Cop
    module Style
      # This cop checks for uses of double negation (!!) to convert something
      # to a boolean value. As this is both cryptic and usually redundant it
      # should be avoided.
      #
      # @example
      #
      #   # bad
      #   !!something
      #
      #   # good
      #   !something.nil?
      class DoubleNegation < Cop
        MSG = 'Avoid the use of double negation (`!!`).'

        def on_send(node)
          return unless not_node?(node)

          receiver, _method_name, *_args = *node

          add_offense(node, :selector) if not_node?(receiver)
        end

        private

        def not_node?(node)
          _receiver, method_name, *args = *node

          # ! does not take any arguments
          args.empty? && method_name == :! &&
            node.loc.selector.is?('!')
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rubocop-0.22.0 lib/rubocop/cop/style/double_negation.rb
rubocop-0.21.0 lib/rubocop/cop/style/double_negation.rb
rubocop-0.20.1 lib/rubocop/cop/style/double_negation.rb
rubocop-0.20.0 lib/rubocop/cop/style/double_negation.rb