Sha256: 95c18583f928209f79b028218e3cd64f2e7f11d2424458a645ee1ae5693b6692

Contents?: true

Size: 1.28 KB

Versions: 1

Compression:

Stored size: 1.28 KB

Contents

# encoding: utf-8

module Rubocop
  module Cop
    module Lint
      # This cop checks for literals used as operands in and/or
      # expressions in the conditions of if/while/until.
      class LiteralInCondition < Cop
        MSG = 'Literal %s appeared in a condition.'

        LITERALS = [:str, :dstr, :int, :float, :array,
                    :hash, :regexp, :nil, :true, :false]

        def on_if(node)
          check_for_literal(node)

          super
        end

        def on_while(node)
          check_for_literal(node)

          super
        end

        def on_while_post(node)
          check_for_literal(node)

          super
        end

        def on_until(node)
          check_for_literal(node)

          super
        end

        def on_until_post(node)
          check_for_literal(node)

          super
        end

        private

        def check_for_literal(node)
          cond, = *node

          on_node([:and, :or], cond) do |logic_node|
            *operands = *logic_node
            operands.each do |op|
              if LITERALS.include?(op.type)
                add_offence(:warning, op.loc.expression,
                            format(MSG, op.loc.expression.source))

              end
            end
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rubocop-0.9.0 lib/rubocop/cop/lint/literal_in_condition.rb