class Rubocop::Cop::Lint::AssignmentInCondition
if/while/until.
This cop checks for assignments in the conditions of
def check(node)
def check(node) condition, = *node # assignments inside blocks are not what we're looking for return if condition.type == :block on_node([:begin, *ASGN_NODES], condition) do |asgn_node| # skip safe assignment nodes if safe assignment is allowed return if safe_assignment_allowed? && safe_assignment?(asgn_node) # assignment nodes from shorthand ops like ||= don't have operator if asgn_node.type != :begin && asgn_node.loc.operator warning(asgn_node, :operator) end end end
def on_if(node)
def on_if(node) check(node) end
def on_until(node)
def on_until(node) check(node) end
def on_while(node)
def on_while(node) check(node) end
def safe_assignment?(node)
def safe_assignment?(node) node.type == :begin && node.children.size == 1 && ASGN_NODES.include?(node.children[0].type) end
def safe_assignment_allowed?
def safe_assignment_allowed? cop_config['AllowSafeAssignment'] end