Class: RuboCop::Cop::Lint::UselessAssignment

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/lint/useless_assignment.rb

Overview

This cop checks for every useless assignment to local variable in every scope. The basic idea for this cop was from the warning of `ruby -cw`:

assigned but unused variable - foo

Currently this cop has advanced logic that detects unreferenced reassignments and properly handles varied cases such as branch, loop, rescue, ensure, etc.

Examples:


# bad

def some_method
  some_var = 1
  do_something
end

# good

def some_method
  some_var = 1
  do_something(some_var)
end

Constant Summary collapse

MSG =
'Useless assignment to variable - `%<variable>s`.'

Constants inherited from Base

Base::RESTRICT_ON_SEND

Instance Attribute Summary

Attributes inherited from Base

#config, #processed_source

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#add_global_offense, #add_offense, autocorrect_incompatible_with, badge, #callbacks_needed, callbacks_needed, #config_to_allow_offenses, #config_to_allow_offenses=, #cop_config, #cop_name, cop_name, department, documentation_url, exclude_from_registry, #excluded_file?, #external_dependency_checksum, inherited, #initialize, lint?, match?, #message, #offenses, #on_investigation_end, #on_new_investigation, #on_other_file, #parse, #ready, #relevant_file?, support_autocorrect?, support_multiple_source?, #target_rails_version, #target_ruby_version

Methods included from ExcludeLimit

#exclude_limit

Methods included from AutocorrectLogic

#autocorrect?, #autocorrect_enabled?, #autocorrect_requested?, #correctable?, #disable_uncorrectable?, #safe_autocorrect?

Methods included from IgnoredNode

#ignore_node, #ignored_node?, #part_of_ignored_node?

Methods included from Util

silence_warnings

Constructor Details

This class inherits a constructor from RuboCop::Cop::Base

Class Method Details

.joining_forcesObject


36
37
38
# File 'lib/rubocop/cop/lint/useless_assignment.rb', line 36

def self.joining_forces
  VariableForce
end

Instance Method Details

#after_leaving_scope(scope, _variable_table) ⇒ Object


40
41
42
# File 'lib/rubocop/cop/lint/useless_assignment.rb', line 40

def after_leaving_scope(scope, _variable_table)
  scope.variables.each_value { |variable| check_for_unused_assignments(variable) }
end

#check_for_unused_assignments(variable) ⇒ Object


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/rubocop/cop/lint/useless_assignment.rb', line 44

def check_for_unused_assignments(variable)
  return if variable.should_be_unused?

  variable.assignments.each do |assignment|
    next if assignment.used?

    message = message_for_useless_assignment(assignment)

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

    add_offense(location, message: message)
  end
end

#collect_variable_like_names(scope) ⇒ Object


108
109
110
111
112
113
114
115
# File 'lib/rubocop/cop/lint/useless_assignment.rb', line 108

def collect_variable_like_names(scope)
  names = scope.each_node.with_object(Set.new) do |node, set|
    set << node.method_name if variable_like_method_invocation?(node)
  end

  variable_names = scope.variables.each_value.map(&:name)
  names.merge(variable_names)
end

#message_for_useless_assignment(assignment) ⇒ Object


62
63
64
65
66
# File 'lib/rubocop/cop/lint/useless_assignment.rb', line 62

def message_for_useless_assignment(assignment)
  variable = assignment.variable

  format(MSG, variable: variable.name) + message_specification(assignment, variable).to_s
end

#message_specification(assignment, variable) ⇒ Object


68
69
70
71
72
73
74
75
76
# File 'lib/rubocop/cop/lint/useless_assignment.rb', line 68

def message_specification(assignment, variable)
  if assignment.multiple_assignment?
    multiple_assignment_message(variable.name)
  elsif assignment.operator_assignment?
    operator_assignment_message(variable.scope, assignment)
  else
    similar_name_message(variable)
  end
end

#multiple_assignment_message(variable_name) ⇒ Object


78
79
80
81
# File 'lib/rubocop/cop/lint/useless_assignment.rb', line 78

def multiple_assignment_message(variable_name)
  " Use `_` or `_#{variable_name}` as a variable name to indicate " \
    "that it won't be used."
end

#operator_assignment_message(scope, assignment) ⇒ Object


83
84
85
86
87
88
89
# File 'lib/rubocop/cop/lint/useless_assignment.rb', line 83

def operator_assignment_message(scope, assignment)
  return_value_node = return_value_node_of_scope(scope)
  return unless assignment.meta_assignment_node.equal?(return_value_node)

  " Use `#{assignment.operator.sub(/=$/, '')}` " \
    "instead of `#{assignment.operator}`."
end

#return_value_node_of_scope(scope) ⇒ Object

TODO: More precise handling (rescue, ensure, nested begin, etc.)


98
99
100
101
102
103
104
105
106
# File 'lib/rubocop/cop/lint/useless_assignment.rb', line 98

def return_value_node_of_scope(scope)
  body_node = scope.body_node

  if body_node.begin_type?
    body_node.children.last
  else
    body_node
  end
end

#similar_name_message(variable) ⇒ Object


91
92
93
94
95
# File 'lib/rubocop/cop/lint/useless_assignment.rb', line 91

def similar_name_message(variable)
  variable_like_names = collect_variable_like_names(variable.scope)
  similar_name = NameSimilarity.find_similar_name(variable.name, variable_like_names)
  " Did you mean `#{similar_name}`?" if similar_name
end

#variable_like_method_invocation?(node) ⇒ Boolean

Returns:

  • (Boolean)

117
118
119
120
121
# File 'lib/rubocop/cop/lint/useless_assignment.rb', line 117

def variable_like_method_invocation?(node)
  return false unless node.send_type?

  node.receiver.nil? && !node.arguments?
end