Sha256: 1770fdda4ea472366e87da7d3c519c80e7746d784cbacc36b15e46ea9adb562f

Contents?: true

Size: 1.02 KB

Versions: 23

Compression:

Stored size: 1.02 KB

Contents

# encoding: utf-8

module RuboCop
  module Cop
    module Lint
      # This cop checks for underscore-prefixed variables that are actually
      # used.
      class UnderscorePrefixedVariableName < Cop
        MSG = 'Do not use prefix `_` for a variable that is used.'

        def join_force?(force_class)
          force_class == VariableForce
        end

        def after_leaving_scope(scope, _variable_table)
          scope.variables.each_value do |variable|
            check_variable(variable)
          end
        end

        def check_variable(variable)
          return unless variable.should_be_unused?
          return if variable.references.empty?
          return if variable.references.none?(&:explicit?)

          node = variable.declaration_node

          location = if node.type == :match_with_lvasgn
                       node.children.first.loc.expression
                     else
                       node.loc.name
                     end

          add_offense(nil, location)
        end
      end
    end
  end
end

Version data entries

23 entries across 23 versions & 2 rubygems

Version Path
rubyjobbuilderdsl-0.0.2 vendor/bundle/ruby/2.1.0/gems/rubocop-0.26.0/lib/rubocop/cop/lint/underscore_prefixed_variable_name.rb
rubyjobbuilderdsl-0.0.1 vendor/bundle/ruby/2.1.0/gems/rubocop-0.26.0/lib/rubocop/cop/lint/underscore_prefixed_variable_name.rb
rubocop-0.35.1 lib/rubocop/cop/lint/underscore_prefixed_variable_name.rb
rubocop-0.35.0 lib/rubocop/cop/lint/underscore_prefixed_variable_name.rb
rubocop-0.34.2 lib/rubocop/cop/lint/underscore_prefixed_variable_name.rb
rubocop-0.34.1 lib/rubocop/cop/lint/underscore_prefixed_variable_name.rb
rubocop-0.34.0 lib/rubocop/cop/lint/underscore_prefixed_variable_name.rb
rubocop-0.33.0 lib/rubocop/cop/lint/underscore_prefixed_variable_name.rb
rubocop-0.32.1 lib/rubocop/cop/lint/underscore_prefixed_variable_name.rb
rubocop-0.32.0 lib/rubocop/cop/lint/underscore_prefixed_variable_name.rb
rubocop-0.31.0 lib/rubocop/cop/lint/underscore_prefixed_variable_name.rb
rubocop-0.30.1 lib/rubocop/cop/lint/underscore_prefixed_variable_name.rb
rubocop-0.30.0 lib/rubocop/cop/lint/underscore_prefixed_variable_name.rb
rubocop-0.29.1 lib/rubocop/cop/lint/underscore_prefixed_variable_name.rb
rubocop-0.29.0 lib/rubocop/cop/lint/underscore_prefixed_variable_name.rb
rubocop-0.28.0 lib/rubocop/cop/lint/underscore_prefixed_variable_name.rb
rubocop-0.27.1 lib/rubocop/cop/lint/underscore_prefixed_variable_name.rb
rubocop-0.27.0 lib/rubocop/cop/lint/underscore_prefixed_variable_name.rb
rubocop-0.26.1 lib/rubocop/cop/lint/underscore_prefixed_variable_name.rb
rubocop-0.26.0 lib/rubocop/cop/lint/underscore_prefixed_variable_name.rb