class RuboCop::Cop::Lint::AmbiguousAssignment

Checks for mistyped shorthand assignments.

@example

# bad
x =- y
x =+ y
x =* y
x =! y

# good
x -= y # or x = -y
x += y # or x = +y
x *= y # or x = *y
x != y # or x = !y

Constants

MISTAKES
MSG
SIMPLE_ASSIGNMENT_TYPES

Public Instance Methods

"on_#{asgn_type}"(node)
Alias for: on_asgn
on_asgn(node) click to toggle source
# File lib/rubocop/cop/lint/ambiguous_assignment.rb, line 30
def on_asgn(node)
  return unless (rhs = rhs(node))

  range = range_between(node.loc.operator.end_pos - 1, rhs.source_range.begin_pos + 1)
  source = range.source
  return unless MISTAKES.key?(source)

  add_offense(range, message: format(MSG, op: MISTAKES[source]))
end
Also aliased as: "on_#{asgn_type}"

Private Instance Methods

rhs(node) click to toggle source
# File lib/rubocop/cop/lint/ambiguous_assignment.rb, line 44
def rhs(node)
  if node.casgn_type?
    node.children[2]
  else
    node.children[1]
  end
end