Sha256: 2a38f58d96b42be5027e1b026a327b10b34d2e5e36051937b058c67112a6ad23

Contents?: true

Size: 1.85 KB

Versions: 14

Compression:

Stored size: 1.85 KB

Contents

# encoding: utf-8
# frozen_string_literal: true

module RuboCop
  module Cop
    module Lint
      # 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
      #
      #   # 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 < Cop
        MSG = 'Circular argument reference - `%s`.'.freeze

        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)
          case arg_value.type
          when :send
            # Ruby 2.0 will have type send every time, and "send nil" if it is
            # calling itself with a specified "self" receiver
            receiver, name = *arg_value
            return unless name == arg_name && receiver.nil?
          when :lvar
            # Ruby 2.2.2 will have type lvar if it is calling its own method
            # without a specified "self"
            return unless arg_value.to_a == [arg_name]
          else
            return
          end

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

Version data entries

14 entries across 14 versions & 2 rubygems

Version Path
fluent-plugin-detect-memb-exceptions-0.0.2 vendor/bundle/ruby/2.0.0/gems/rubocop-0.42.0/lib/rubocop/cop/lint/circular_argument_reference.rb
fluent-plugin-detect-memb-exceptions-0.0.1 vendor/bundle/ruby/2.0.0/gems/rubocop-0.42.0/lib/rubocop/cop/lint/circular_argument_reference.rb
rubocop-0.43.0 lib/rubocop/cop/lint/circular_argument_reference.rb
rubocop-0.42.0 lib/rubocop/cop/lint/circular_argument_reference.rb
rubocop-0.41.2 lib/rubocop/cop/lint/circular_argument_reference.rb
rubocop-0.41.1 lib/rubocop/cop/lint/circular_argument_reference.rb
rubocop-0.41.0 lib/rubocop/cop/lint/circular_argument_reference.rb
rubocop-0.40.0 lib/rubocop/cop/lint/circular_argument_reference.rb
rubocop-0.39.0 lib/rubocop/cop/lint/circular_argument_reference.rb
rubocop-0.38.0 lib/rubocop/cop/lint/circular_argument_reference.rb
rubocop-0.37.2 lib/rubocop/cop/lint/circular_argument_reference.rb
rubocop-0.37.1 lib/rubocop/cop/lint/circular_argument_reference.rb
rubocop-0.37.0 lib/rubocop/cop/lint/circular_argument_reference.rb
rubocop-0.36.0 lib/rubocop/cop/lint/circular_argument_reference.rb