class RuboCop::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
Public Instance Methods
on_def(node)
click to toggle source
# File lib/rubocop/cop/lint/useless_setter_call.rb, line 31 def on_def(node) return unless node.body last_expr = last_expression(node.body) return unless setter_call_to_local_variable?(last_expr) tracker = MethodVariableTracker.new(node.body) receiver, = *last_expr variable_name, = *receiver return unless tracker.contain_local_object?(variable_name) add_offense( receiver, location: :name, message: format(MSG, variable: receiver.loc.name.source) ) end
Also aliased as: on_defs
Private Instance Methods
last_expression(body)
click to toggle source
# File lib/rubocop/cop/lint/useless_setter_call.rb, line 56 def last_expression(body) expression = body.begin_type? ? body.children : body expression.is_a?(Array) ? expression.last : expression end