Sha256: 02c5b7156ed8fd5f5f04537d67d505f03e1a0799c48d3dbec447930db58d35c2

Contents?: true

Size: 1.48 KB

Versions: 4

Compression:

Stored size: 1.48 KB

Contents

# encoding: utf-8

module Rubocop
  module Cop
    module Lint
      # This cop checks for operators, variables and literals used
      # in void context.
      class Void < Cop
        OP_MSG = 'Operator %s used in void context.'
        VAR_MSG = 'Variable %s used in void context.'
        LIT_MSG = 'Literal %s used in void context'

        OPS = %w(* / % + - == === != < > <= >= <=>)
        VARS = [:ivar, :lvar, :cvar, :const]
        LITERALS = [:str, :dstr, :int, :float, :array,
                    :hash, :regexp, :nil, :true, :false]

        def on_begin(node)
          expressions = *node

          expressions[0...-1].each do |expr|
            check_for_void_op(expr)
            check_for_literal(expr)
            check_for_var(expr)
          end
        end

        private

        def check_for_void_op(node)
          return unless node.type == :send

          op = node.loc.selector.source

          if OPS.include?(op)
            add_offence(:warning, node.loc.selector, sprintf(OP_MSG, op))
          end
        end

        def check_for_var(node)
          if VARS.include?(node.type)
            add_offence(:warning, node.loc.name,
                        sprintf(VAR_MSG, node.loc.name.source))
          end
        end

        def check_for_literal(node)
          if LITERALS.include?(node.type)
            add_offence(:warning, node.loc.expression,
                        sprintf(LIT_MSG, node.loc.expression.source))
          end
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rubocop-0.12.0 lib/rubocop/cop/lint/void.rb
rubocop-0.11.1 lib/rubocop/cop/lint/void.rb
rubocop-0.11.0 lib/rubocop/cop/lint/void.rb
rubocop-0.10.0 lib/rubocop/cop/lint/void.rb