class RuboCop::Cop::Lint::UselessSetterCall
end
x
x.attr = 5
x = Something.new
def something
# good
@example
end
x.attr = 5
x = Something.new
def something
# bad
@example
return value will be changed.
As well, autocorrection is unsafe because the method’s
detected by the cop, and it can yield a false positive.
value that is also accessible outside the local scope. This is not
There are edge cases in which the local variable references a
@safety
expression of a function definition.
Checks for setter call to local variable as the final
def last_expression(body)
def last_expression(body) expression = body.begin_type? ? body.children : body expression.is_a?(Array) ? expression.last : expression end
def on_def(node)
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) loc_name = receiver.loc.name add_offense(loc_name, message: format(MSG, variable: loc_name.source)) do |corrector| corrector.insert_after(last_expr, "\n#{indent(last_expr)}#{loc_name.source}") end end