class RubyLint::Analysis::UnusedVariables

The UnusedVariables class checks for variables that are defined but never used. Whenever it finds one of these variables it will add a corresponding warning message.

Constants

VARIABLE_TYPES

Hash containing the various variable types for which to add warnings and human readable names for these types.

@return [Hash]

Public Instance Methods

on_casgn(node) click to toggle source

Handles regular constants as well as constant paths.

@param [RubyLint::AST::Node] node

# File lib/ruby-lint/analysis/unused_variables.rb, line 53
def on_casgn(node)
  path     = ConstantPath.new(node)
  variable = path.resolve(current_scope)
  name     = path.to_s

  if variable and !variable.used?
    warning("unused constant #{name}", node)
  end
end
on_ivasgn(node) click to toggle source

@param [RubyLint::AST::Node] node

# File lib/ruby-lint/analysis/unused_variables.rb, line 37
def on_ivasgn(node)
  name        = node.name
  variable    = current_scope.lookup(:ivar, name)
  method_type = current_scope.method_call_type
  getter      = current_scope.lookup(method_type, name[1..-1])

  if variable and !variable.used? and !getter
    warning("unused instance variable #{name}", node)
  end
end

Private Instance Methods

add_warning?(variable) click to toggle source

@param [RubyLint::Definition::RubyObject] variable @return [TrueClass|FalseClass]

# File lib/ruby-lint/analysis/unused_variables.rb, line 86
def add_warning?(variable)
  return variable && !variable.used? && !ignore_variable?(variable.name)
end
ignore_variable?(name) click to toggle source

@param [String] name @return [TrueClass|FalseClass]

# File lib/ruby-lint/analysis/unused_variables.rb, line 94
def ignore_variable?(name)
  return name[0] == '_' || name.empty?
end
verify_argument(node) click to toggle source

Adds warnings for unused method arguments.

@param [RubyLint::AST::Node] node

# File lib/ruby-lint/analysis/unused_variables.rb, line 74
def verify_argument(node)
  variable = current_scope.lookup(:lvar, node.name)

  if add_warning?(variable)
    warning("unused argument #{variable.name}", node)
  end
end