Sha256: e1302363bd9fed87be6ca79630ee06e35b2257008e7f1414164f088ef2ee63c4

Contents?: true

Size: 1.49 KB

Versions: 1

Compression:

Stored size: 1.49 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Lint
      # Checks for circular argument references in optional keyword
      # arguments and optional ordinal arguments.
      #
      # This cop mirrors a warning produced by MRI since 2.2.
      #
      # NOTE: This syntax is no longer valid on Ruby 2.7 or higher.
      #
      # @example
      #
      #   # bad
      #   def bake(pie: pie)
      #     pie.heat_up
      #   end
      #
      #   # good
      #   def bake(pie:)
      #     pie.refrigerate
      #   end
      #
      #   # good
      #   def bake(pie: self.pie)
      #     pie.feed_to(user)
      #   end
      #
      #   # bad
      #   def cook(dry_ingredients = dry_ingredients)
      #     dry_ingredients.reduce(&:+)
      #   end
      #
      #   # good
      #   def cook(dry_ingredients = self.dry_ingredients)
      #     dry_ingredients.combine
      #   end
      class CircularArgumentReference < Base
        MSG = 'Circular argument reference - `%<arg_name>s`.'

        def on_kwoptarg(node)
          check_for_circular_argument_references(*node)
        end

        def on_optarg(node)
          check_for_circular_argument_references(*node)
        end

        private

        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
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rubocop-1.69.0 lib/rubocop/cop/lint/circular_argument_reference.rb