Class: RuboCop::Cop::Lint::UselessAssignment
- Includes:
- NameSimilarity
- 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.
Constant Summary collapse
- MSG =
'Useless assignment to variable - `%<variable>s`.'
Constants included from NameSimilarity
NameSimilarity::MINIMUM_SIMILARITY_TO_SUGGEST
Constants included from Util
Instance Attribute Summary
Attributes inherited from Cop
#config, #corrections, #offenses, #processed_source
Instance Method Summary collapse
- #after_leaving_scope(scope, _variable_table) ⇒ Object
- #check_for_unused_assignments(variable) ⇒ Object
- #collect_variable_like_names(scope) ⇒ Object
- #join_force?(force_class) ⇒ Boolean
- #message_for_useless_assignment(assignment) ⇒ Object
- #message_specification(assignment, variable) ⇒ Object
- #multiple_assignment_message(variable_name) ⇒ Object
- #operator_assignment_message(scope, assignment) ⇒ Object
-
#return_value_node_of_scope(scope) ⇒ Object
TODO: More precise handling (rescue, ensure, nested begin, etc.).
- #similar_name_message(variable) ⇒ Object
- #variable_like_method_invocation?(node) ⇒ Boolean
Methods included from NameSimilarity
Methods inherited from Cop
#add_offense, all, autocorrect_incompatible_with, badge, #config_to_allow_offenses, #config_to_allow_offenses=, #cop_config, #cop_name, cop_name, #correct, department, #duplicate_location?, #excluded_file?, #find_location, #highlights, inherited, #initialize, lint?, match?, #message, #messages, non_rails, #parse, qualified_cop_name, #relevant_file?, #target_rails_version, #target_ruby_version
Methods included from AST::Sexp
Methods included from NodePattern::Macros
#def_node_matcher, #def_node_search, #node_search, #node_search_all, #node_search_body, #node_search_first
Methods included from AutocorrectLogic
#autocorrect?, #autocorrect_enabled?, #autocorrect_requested?, #support_autocorrect?
Methods included from IgnoredNode
#ignore_node, #ignored_node?, #part_of_ignored_node?
Methods included from Util
begins_its_line?, comment_line?, double_quotes_required?, escape_string, first_part_of_call_chain, interpret_string_escapes, line_range, needs_escaping?, on_node, parentheses?, same_line?, to_string_literal, to_supported_styles, tokens, trim_string_interporation_escape_character
Methods included from PathUtil
absolute?, chdir, hidden_dir?, hidden_file_in_not_hidden_dir?, match_path?, pwd, relative_path, reset_pwd, smart_path
Constructor Details
This class inherits a constructor from RuboCop::Cop::Cop
Instance Method Details
#after_leaving_scope(scope, _variable_table) ⇒ Object
41 42 43 44 45 |
# File 'lib/rubocop/cop/lint/useless_assignment.rb', line 41 def after_leaving_scope(scope, _variable_table) scope.variables.each_value do |variable| check_for_unused_assignments(variable) end end |
#check_for_unused_assignments(variable) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/rubocop/cop/lint/useless_assignment.rb', line 47 def check_for_unused_assignments(variable) return if variable.should_be_unused? variable.assignments.each do |assignment| next if assignment.used? = (assignment) location = if assignment.regexp_named_capture? assignment.node.children.first.source_range else assignment.node.loc.name end add_offense(nil, location: location, message: ) end end |
#collect_variable_like_names(scope) ⇒ Object
112 113 114 115 116 117 118 119 |
# File 'lib/rubocop/cop/lint/useless_assignment.rb', line 112 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 |
#join_force?(force_class) ⇒ Boolean
37 38 39 |
# File 'lib/rubocop/cop/lint/useless_assignment.rb', line 37 def join_force?(force_class) force_class == VariableForce end |
#message_for_useless_assignment(assignment) ⇒ Object
65 66 67 68 69 70 |
# File 'lib/rubocop/cop/lint/useless_assignment.rb', line 65 def (assignment) variable = assignment.variable format(MSG, variable: variable.name) + (assignment, variable).to_s end |
#message_specification(assignment, variable) ⇒ Object
72 73 74 75 76 77 78 79 80 |
# File 'lib/rubocop/cop/lint/useless_assignment.rb', line 72 def (assignment, variable) if assignment.multiple_assignment? (variable.name) elsif assignment.operator_assignment? (variable.scope, assignment) else (variable) end end |
#multiple_assignment_message(variable_name) ⇒ Object
82 83 84 85 |
# File 'lib/rubocop/cop/lint/useless_assignment.rb', line 82 def (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
87 88 89 90 91 92 93 94 |
# File 'lib/rubocop/cop/lint/useless_assignment.rb', line 87 def (scope, assignment) return_value_node = return_value_node_of_scope(scope) return unless assignment. .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.)
102 103 104 105 106 107 108 109 110 |
# File 'lib/rubocop/cop/lint/useless_assignment.rb', line 102 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
96 97 98 99 |
# File 'lib/rubocop/cop/lint/useless_assignment.rb', line 96 def (variable) similar_name = find_similar_name(variable.name, variable.scope) " Did you mean `#{similar_name}`?" if similar_name end |
#variable_like_method_invocation?(node) ⇒ Boolean
121 122 123 124 125 |
# File 'lib/rubocop/cop/lint/useless_assignment.rb', line 121 def variable_like_method_invocation?(node) return false unless node.send_type? node.receiver.nil? && !node.arguments? end |