class RuboCop::Cop::Style::RedundantSelfAssignment
foo.concat(ary)
# good
hash.merge!(other)
args += foo
args.concat(foo)
# good
hash = hash.merge!(other)
args = args.concat(ary)
# bad
@example
its receiver in place.
user defined methods having one of the expected names, but not modifying
This cop is unsafe, because it can produce false positives for
@safety
modification methods.
Checks for places where redundant assignments are made for in place
def correction_range(node)
def correction_range(node) range_between(node.source_range.begin_pos, node.first_argument.source_range.begin_pos) end
def method_returning_self?(method_name)
def method_returning_self?(method_name) METHODS_RETURNING_SELF.include?(method_name) end
def on_lvasgn(node)
def on_lvasgn(node) return unless (rhs = node.rhs) return unless rhs.type?(:any_block, :call) && method_returning_self?(rhs.method_name) return unless (receiver = rhs.receiver) receiver_type = ASSIGNMENT_TYPE_TO_RECEIVER_TYPE[node.type] return unless receiver.type == receiver_type && receiver.children.first == node.lhs message = format(MSG, method_name: rhs.method_name) add_offense(node.loc.operator, message: message) do |corrector| corrector.replace(node, rhs.source) end end
def on_send(node)
def on_send(node) return unless node.assignment_method? return unless redundant_assignment?(node) message = format(MSG, method_name: node.first_argument.method_name) add_offense(node.loc.operator, message: message) do |corrector| corrector.remove(correction_range(node)) end end
def redundant_assignment?(node)
def redundant_assignment?(node) receiver_name = node.method_name.to_s.delete_suffix('=').to_sym redundant_self_assignment?(node, node.receiver, receiver_name) end