lib/rubocop/cop/lint/useless_setter_call.rb in rubocop-0.49.1 vs lib/rubocop/cop/lint/useless_setter_call.rb in rubocop-0.50.0
- old
+ new
@@ -23,41 +23,40 @@
# x = Something.new
# x.attr = 5
# x
# end
class UselessSetterCall < Cop
- include OnMethodDef
-
MSG = 'Useless setter call to local variable `%s`.'.freeze
ASSIGNMENT_TYPES = %i[lvasgn ivasgn cvasgn gvasgn].freeze
- private
+ def on_def(node)
+ return unless node.body
- def on_method_def(_node, _method_name, _args, body)
- return unless body
-
- last_expr = last_expression(body)
+ last_expr = last_expression(node.body)
return unless setter_call_to_local_variable?(last_expr)
- tracker = MethodVariableTracker.new(body)
+ tracker = MethodVariableTracker.new(node.body)
receiver, = *last_expr
variable_name, = *receiver
return unless tracker.contain_local_object?(variable_name)
add_offense(receiver, :name, format(MSG, receiver.loc.name.source))
end
+ alias on_defs on_def
+ private
+
def last_expression(body)
expression = body.begin_type? ? body.children : body
expression.is_a?(Array) ? expression.last : expression
end
def setter_call_to_local_variable?(node)
return unless node && node.send_type?
- receiver, method, _args = *node
- return unless receiver && receiver.lvar_type?
- method =~ /(?:\w|\[\])=$/
+ return unless node.receiver && node.receiver.lvar_type?
+
+ node.method_name =~ /(?:\w|\[\])=$/
end
# This class tracks variable assignments in a method body
# and if a variable contains object passed as argument at the end of
# the method.