class RuboCop::Cop::Lint::CircularArgumentReference

This cop checks for circular argument references in optional keyword arguments and optional ordinal arguments.

This cop mirrors a warning produced by MRI since 2.2.

@example

# bad

def bake(pie: pie)
  pie.heat_up
end

@example

# good

def bake(pie:)
  pie.refrigerate
end

@example

# good

def bake(pie: self.pie)
  pie.feed_to(user)
end

@example

# bad

def cook(dry_ingredients = dry_ingredients)
  dry_ingredients.reduce(&:+)
end

@example

# good

def cook(dry_ingredients = self.dry_ingredients)
  dry_ingredients.combine
end

Constants

MSG

Public Instance Methods

on_kwoptarg(node) click to toggle source
# File lib/rubocop/cop/lint/circular_argument_reference.rb, line 53
def on_kwoptarg(node)
  check_for_circular_argument_references(*node)
end
on_optarg(node) click to toggle source
# File lib/rubocop/cop/lint/circular_argument_reference.rb, line 57
def on_optarg(node)
  check_for_circular_argument_references(*node)
end

Private Instance Methods

check_for_circular_argument_references(arg_name, arg_value) click to toggle source
# File lib/rubocop/cop/lint/circular_argument_reference.rb, line 63
def check_for_circular_argument_references(arg_name, arg_value)
  return unless arg_value.lvar_type?
  return unless arg_value.to_a == [arg_name]

  add_offense(arg_value, message: format(MSG, arg_name: arg_name))
end