Class: RubyLint::Analysis::UnusedVariables

Inherits:
Base show all
Defined in:
lib/ruby-lint/analysis/unused_variables.rb

Overview

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.

Constant Summary

VARIABLE_TYPES =

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

Returns:

  • (Hash)
{
  :lvasgn => 'local variable',
  :gvasgn => 'global variable',
  :cvasgn => 'class variable'
}

Constants inherited from Base

Base::SCOPES

Instance Attribute Summary

Attributes inherited from Base

#config, #report, #vm

Attributes inherited from Iterator

#arity_cache, #arity_cache Hash containing the amount of arguments for

Instance Method Summary collapse

Methods inherited from Base

#add_message, #after_initialize, analyze?, #current_scope, #error, #info, #previous_scope, register, #set_current_scope, #set_previous_scope, #warning

Methods included from MethodEvaluation

#unpack_block

Methods inherited from Iterator

#execute_callback, #initialize, #iterate, #skip_child_nodes!

Constructor Details

This class inherits a constructor from RubyLint::Iterator

Instance Method Details

#add_warning?(variable) ⇒ TrueClass|FalseClass (private)

Parameters:

Returns:

  • (TrueClass|FalseClass)


86
87
88
# 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) ⇒ TrueClass|FalseClass (private)

Parameters:

Returns:

  • (TrueClass|FalseClass)


94
95
96
# File 'lib/ruby-lint/analysis/unused_variables.rb', line 94

def ignore_variable?(name)
  return name[0] == '_' || name.empty?
end

#on_casgn(node) ⇒ Object

Handles regular constants as well as constant paths.

Parameters:



53
54
55
56
57
58
59
60
61
# 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) ⇒ Object

Parameters:



37
38
39
40
41
42
43
44
45
46
# 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

#verify_argument(node) ⇒ Object (private)

Adds warnings for unused method arguments.

Parameters:



74
75
76
77
78
79
80
# 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