class RubbyCop::Cop::Lint::UselessSetterCall
This cop checks for setter call to local variable as the final expression of a function definition.
@example
# bad def something x = Something.new x.attr = 5 end
@example
# good def something x = Something.new x.attr = 5 x end
Constants
- ASSIGNMENT_TYPES
- MSG
Private Instance Methods
last_expression(body)
click to toggle source
# File lib/rubbycop/cop/lint/useless_setter_call.rb, line 49 def last_expression(body) expression = body.begin_type? ? body.children : body expression.is_a?(Array) ? expression.last : expression end
on_method_def(_node, _method_name, _args, body)
click to toggle source
# File lib/rubbycop/cop/lint/useless_setter_call.rb, line 35 def on_method_def(_node, _method_name, _args, body) return unless body last_expr = last_expression(body) return unless setter_call_to_local_variable?(last_expr) tracker = MethodVariableTracker.new(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
setter_call_to_local_variable?(node)
click to toggle source
# File lib/rubbycop/cop/lint/useless_setter_call.rb, line 55 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|\[\])=$/ end