Class: RuboCop::Cop::Lint::UselessAssignment
- Inherits:
-
Base
- Object
- Base
- RuboCop::Cop::Lint::UselessAssignment
show all
- Extended by:
- AutoCorrector
- Defined in:
- lib/rubocop/cop/lint/useless_assignment.rb
Overview
Checks for every useless assignment to local variable in every scope. The basic idea for this cop was from the warning of ‘ruby -cw`:
- source,console
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.
This cop’s autocorrection avoids cases like ‘a ||= 1` because removing assignment from operator assignment can cause `NameError` if this assignment has been used to declare a local variable. For example, replacing `a ||= 1` with `a || 1` may cause “undefined local variable or method `a’ for main:Object (NameError)”.
NOTE: Given the assignment ‘foo = 1, bar = 2`, removing unused variables can lead to a syntax error, so this case is not autocorrected.
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
-
#after_leaving_scope(scope, _variable_table) ⇒ Object
-
#autocorrect(corrector, assignment) ⇒ Object
rubocop:disable Metrics/AbcSize.
-
#chained_assignment?(node) ⇒ Boolean
-
#check_for_unused_assignment(variable, assignment) ⇒ Object
-
#check_for_unused_assignments(variable) ⇒ Object
-
#collect_variable_like_names(scope) ⇒ Object
-
#ignored_assignment?(variable, assignment_node, assignment) ⇒ Boolean
-
#message_for_useless_assignment(assignment) ⇒ Object
-
#message_specification(assignment, variable) ⇒ Object
-
#multiple_assignment_message(variable_name) ⇒ Object
-
#offense_range(assignment) ⇒ Object
-
#operator_assignment_message(scope, assignment) ⇒ Object
-
#remove_exception_assignment_part(corrector, node) ⇒ Object
rubocop:enable Metrics/AbcSize.
-
#remove_local_variable_assignment_part(corrector, node) ⇒ Object
-
#remove_trailing_character_from_operator(corrector, node) ⇒ Object
-
#rename_variable_with_underscore(corrector, node) ⇒ Object
-
#replace_named_capture_group_with_non_capturing_group(corrector, node, variable_name) ⇒ Object
-
#return_value_node_of_scope(scope) ⇒ Object
TODO: More precise handling (rescue, ensure, nested begin, etc.).
-
#sequential_assignment?(node) ⇒ Boolean
-
#similar_name_message(variable) ⇒ Object
-
#variable_in_loop_condition?(assignment_node, variable) ⇒ Boolean
-
#variable_like_method_invocation?(node) ⇒ Boolean
support_autocorrect?
Methods inherited from Base
#active_support_extensions_enabled?, #add_global_offense, #add_offense, #always_autocorrect?, autocorrect_incompatible_with, badge, #begin_investigation, #callbacks_needed, callbacks_needed, #config_to_allow_offenses, #config_to_allow_offenses=, #contextual_autocorrect?, #cop_config, cop_name, #cop_name, department, documentation_url, exclude_from_registry, #excluded_file?, #external_dependency_checksum, inherited, #initialize, #inspect, lint?, match?, #message, #offenses, #on_investigation_end, #on_new_investigation, #on_other_file, #parse, #parser_engine, #ready, #relevant_file?, requires_gem, #string_literals_frozen_by_default?, support_autocorrect?, support_multiple_source?, #target_gem_version, #target_rails_version, #target_ruby_version
cop_dir_for, #exclude_limit, read_limits
#autocorrect?, #autocorrect_enabled?, #autocorrect_requested?, #autocorrect_with_disable_uncorrectable?, #correctable?, #disable_uncorrectable?, #safe_autocorrect?
#ignore_node, #ignored_node?, #part_of_ignored_node?
Methods included from Util
silence_warnings
Class Method Details
.joining_forces ⇒ Object
45
46
47
|
# File 'lib/rubocop/cop/lint/useless_assignment.rb', line 45
def self.joining_forces
VariableForce
end
|
Instance Method Details
#after_leaving_scope(scope, _variable_table) ⇒ Object
49
50
51
|
# File 'lib/rubocop/cop/lint/useless_assignment.rb', line 49
def after_leaving_scope(scope, _variable_table)
scope.variables.each_value { |variable| check_for_unused_assignments(variable) }
end
|
#autocorrect(corrector, assignment) ⇒ Object
rubocop:disable Metrics/AbcSize
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
# File 'lib/rubocop/cop/lint/useless_assignment.rb', line 172
def autocorrect(corrector, assignment)
if assignment.exception_assignment?
remove_exception_assignment_part(corrector, assignment.node)
elsif assignment.multiple_assignment? || assignment.rest_assignment? ||
assignment.for_assignment?
rename_variable_with_underscore(corrector, assignment.node)
elsif assignment.operator_assignment?
remove_trailing_character_from_operator(corrector, assignment.node)
elsif assignment.regexp_named_capture?
replace_named_capture_group_with_non_capturing_group(corrector, assignment.node,
assignment.variable.name)
else
remove_local_variable_assignment_part(corrector, assignment.node)
end
end
|
#chained_assignment?(node) ⇒ Boolean
111
112
113
114
115
|
# File 'lib/rubocop/cop/lint/useless_assignment.rb', line 111
def chained_assignment?(node)
return true if node.lvasgn_type? && node.expression&.send_type?
node.respond_to?(:expression) && node.expression&.lvasgn_type?
end
|
#check_for_unused_assignment(variable, assignment) ⇒ Object
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
# File 'lib/rubocop/cop/lint/useless_assignment.rb', line 61
def check_for_unused_assignment(variable, assignment)
assignment_node = assignment.node
return if ignored_assignment?(variable, assignment_node, assignment)
message = message_for_useless_assignment(assignment)
range = offense_range(assignment)
add_offense(range, message: message) do |corrector|
next if sequential_assignment?(assignment_node) ||
assignment_node.parent&.or_asgn_type?
autocorrect(corrector, assignment)
end
ignore_node(assignment_node) if chained_assignment?(assignment_node)
end
|
#check_for_unused_assignments(variable) ⇒ Object
53
54
55
56
57
58
59
|
# File 'lib/rubocop/cop/lint/useless_assignment.rb', line 53
def check_for_unused_assignments(variable)
return if variable.should_be_unused?
variable.assignments.reverse_each do |assignment|
check_for_unused_assignment(variable, assignment)
end
end
|
#collect_variable_like_names(scope) ⇒ Object
156
157
158
159
160
161
162
163
|
# File 'lib/rubocop/cop/lint/useless_assignment.rb', line 156
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
|
#ignored_assignment?(variable, assignment_node, assignment) ⇒ Boolean
82
83
84
85
|
# File 'lib/rubocop/cop/lint/useless_assignment.rb', line 82
def ignored_assignment?(variable, assignment_node, assignment)
assignment.used? || part_of_ignored_node?(assignment_node) ||
variable_in_loop_condition?(assignment_node, variable)
end
|
#message_for_useless_assignment(assignment) ⇒ Object
87
88
89
90
91
|
# File 'lib/rubocop/cop/lint/useless_assignment.rb', line 87
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
117
118
119
120
121
122
123
124
125
|
# File 'lib/rubocop/cop/lint/useless_assignment.rb', line 117
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
127
128
129
130
|
# File 'lib/rubocop/cop/lint/useless_assignment.rb', line 127
def multiple_assignment_message(variable_name)
" Use `_` or `_#{variable_name}` as a variable name to indicate " \
"that it won't be used."
end
|
#offense_range(assignment) ⇒ Object
93
94
95
96
97
98
99
|
# File 'lib/rubocop/cop/lint/useless_assignment.rb', line 93
def offense_range(assignment)
if assignment.regexp_named_capture?
assignment.node.children.first.source_range
else
assignment.node.loc.name
end
end
|
#operator_assignment_message(scope, assignment) ⇒ Object
132
133
134
135
136
137
|
# File 'lib/rubocop/cop/lint/useless_assignment.rb', line 132
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.delete_suffix('=')}` instead of `#{assignment.operator}`."
end
|
#remove_exception_assignment_part(corrector, node) ⇒ Object
rubocop:enable Metrics/AbcSize
189
190
191
192
193
|
# File 'lib/rubocop/cop/lint/useless_assignment.rb', line 189
def remove_exception_assignment_part(corrector, node)
range = node.parent.children.first&.source_range || node.parent.location.keyword
corrector.remove(range.end.join(node.source_range.end))
end
|
#remove_local_variable_assignment_part(corrector, node) ⇒ Object
210
211
212
|
# File 'lib/rubocop/cop/lint/useless_assignment.rb', line 210
def remove_local_variable_assignment_part(corrector, node)
corrector.remove(node.loc.name.begin.join(node.expression.source_range.begin))
end
|
#remove_trailing_character_from_operator(corrector, node) ⇒ Object
199
200
201
|
# File 'lib/rubocop/cop/lint/useless_assignment.rb', line 199
def remove_trailing_character_from_operator(corrector, node)
corrector.remove(node.parent.location.operator.end.adjust(begin_pos: -1))
end
|
#rename_variable_with_underscore(corrector, node) ⇒ Object
195
196
197
|
# File 'lib/rubocop/cop/lint/useless_assignment.rb', line 195
def rename_variable_with_underscore(corrector, node)
corrector.replace(node, '_')
end
|
#replace_named_capture_group_with_non_capturing_group(corrector, node, variable_name) ⇒ Object
203
204
205
206
207
208
|
# File 'lib/rubocop/cop/lint/useless_assignment.rb', line 203
def replace_named_capture_group_with_non_capturing_group(corrector, node, variable_name)
corrector.replace(
node.children.first,
node.children.first.source.sub(/\(\?<#{variable_name}>/, '(?:')
)
end
|
#return_value_node_of_scope(scope) ⇒ Object
TODO: More precise handling (rescue, ensure, nested begin, etc.)
146
147
148
149
150
151
152
153
154
|
# File 'lib/rubocop/cop/lint/useless_assignment.rb', line 146
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
|
#sequential_assignment?(node) ⇒ Boolean
101
102
103
104
105
106
107
108
109
|
# File 'lib/rubocop/cop/lint/useless_assignment.rb', line 101
def sequential_assignment?(node)
if node.lvasgn_type? && node.expression&.array_type? &&
node.each_descendant.any?(&:assignment?)
return true
end
return false unless node.parent
sequential_assignment?(node.parent)
end
|
#similar_name_message(variable) ⇒ Object
139
140
141
142
143
|
# File 'lib/rubocop/cop/lint/useless_assignment.rb', line 139
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_in_loop_condition?(assignment_node, variable) ⇒ Boolean
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
|
# File 'lib/rubocop/cop/lint/useless_assignment.rb', line 214
def variable_in_loop_condition?(assignment_node, variable)
return false if assignment_node.each_ancestor(:any_def).any?
loop_node = assignment_node.each_ancestor.find do |ancestor|
ancestor.type?(*VariableForce::LOOP_TYPES)
end
return false unless loop_node.respond_to?(:condition)
condition_node = loop_node.condition
variable_name = variable.name
return true if condition_node.lvar_type? && condition_node.children.first == variable_name
condition_node.each_descendant(:lvar) do |lvar_node|
return true if lvar_node.children.first == variable_name
end
false
end
|
#variable_like_method_invocation?(node) ⇒ Boolean
165
166
167
168
169
|
# File 'lib/rubocop/cop/lint/useless_assignment.rb', line 165
def variable_like_method_invocation?(node)
return false unless node.send_type?
node.receiver.nil? && !node.arguments?
end
|