Sha256: dbee835821c7c9abac7e6635cd3f98cc0c1019eb8c4de187ecd77efcae9a1c9a

Contents?: true

Size: 1.41 KB

Versions: 11

Compression:

Stored size: 1.41 KB

Contents

# frozen_string_literal: true

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.'.freeze
        VAR_MSG = 'Variable `%s` used in void context.'.freeze
        LIT_MSG = 'Literal `%s` used in void context.'.freeze

        OPS = %w(* / % + - == === != < > <= >= <=>).freeze

        def on_begin(node)
          check_begin(node)
        end

        def on_kwbegin(node)
          check_begin(node)
        end

        private

        def check_begin(node)
          expressions = *node

          expressions.drop_last(1).each do |expr|
            check_for_void_op(expr)
            check_for_literal(expr)
            check_for_var(expr)
          end
        end

        def check_for_void_op(node)
          return unless node.send_type? && node.loc.selector

          op = node.loc.selector.source

          add_offense(node, :selector, format(OP_MSG, op)) if OPS.include?(op)
        end

        def check_for_var(node)
          return unless node.variable? || node.const_type?
          add_offense(node, :name, format(VAR_MSG, node.loc.name.source))
        end

        def check_for_literal(node)
          return if !node.literal? || node.xstr_type?

          add_offense(node, :expression, format(LIT_MSG, node.source))
        end
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 2 rubygems

Version Path
dirwatch-0.0.9 vendor/bundle/ruby/2.5.0/gems/rubocop-0.46.0/lib/rubocop/cop/lint/void.rb
dirwatch-0.0.8 vendor/bundle/ruby/2.5.0/gems/rubocop-0.46.0/lib/rubocop/cop/lint/void.rb
dirwatch-0.0.6 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/lint/void.rb
dirwatch-0.0.5 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/lint/void.rb
dirwatch-0.0.4 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/lint/void.rb
dirwatch-0.0.3 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/lint/void.rb
dirwatch-0.0.2 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/lint/void.rb
rubocop-0.46.0 lib/rubocop/cop/lint/void.rb
rubocop-0.45.0 lib/rubocop/cop/lint/void.rb
rubocop-0.44.1 lib/rubocop/cop/lint/void.rb
rubocop-0.44.0 lib/rubocop/cop/lint/void.rb