Sha256: 62bb005a82925c177ef7425fc84020bcdc0a4b2b27b32683e2f9903727f27c8e

Contents?: true

Size: 1016 Bytes

Versions: 11

Compression:

Stored size: 1016 Bytes

Contents

# frozen_string_literal: true

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.'.freeze

        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.none?(&:explicit?)

          node = variable.declaration_node

          location = if node.match_with_lvasgn_type?
                       node.children.first.source_range
                     else
                       node.loc.name
                     end

          add_offense(nil, location)
        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/underscore_prefixed_variable_name.rb
dirwatch-0.0.8 vendor/bundle/ruby/2.5.0/gems/rubocop-0.46.0/lib/rubocop/cop/lint/underscore_prefixed_variable_name.rb
dirwatch-0.0.6 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/lint/underscore_prefixed_variable_name.rb
dirwatch-0.0.5 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/lint/underscore_prefixed_variable_name.rb
dirwatch-0.0.4 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/lint/underscore_prefixed_variable_name.rb
dirwatch-0.0.3 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/lint/underscore_prefixed_variable_name.rb
dirwatch-0.0.2 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/lint/underscore_prefixed_variable_name.rb
rubocop-0.46.0 lib/rubocop/cop/lint/underscore_prefixed_variable_name.rb
rubocop-0.45.0 lib/rubocop/cop/lint/underscore_prefixed_variable_name.rb
rubocop-0.44.1 lib/rubocop/cop/lint/underscore_prefixed_variable_name.rb
rubocop-0.44.0 lib/rubocop/cop/lint/underscore_prefixed_variable_name.rb