Sha256: 2695d1d77b1e5461e4526fc01bcaf866542e020deb03796a94de163f4b1c161b

Contents?: true

Size: 1.45 KB

Versions: 185

Compression:

Stored size: 1.45 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module InternalAffairs
      # Check for useless `RESTRICT_ON_SEND`.
      #
      # @example
      #   # bad
      #   class FooCop
      #     RESTRICT_ON_SEND = %i[bad_method].freeze
      #   end
      #
      #   # good
      #   class FooCop
      #     RESTRICT_ON_SEND = %i[bad_method].freeze
      #     def on_send(node)
      #       # ...
      #     end
      #   end
      #
      #   # good
      #   class FooCop
      #     RESTRICT_ON_SEND = %i[bad_method].freeze
      #     def after_send(node)
      #       # ...
      #     end
      #   end
      #
      class UselessRestrictOnSend < Base
        extend AutoCorrector

        MSG = 'Useless `RESTRICT_ON_SEND` is defined.'

        # @!method defined_send_callback?(node)
        def_node_search :defined_send_callback?, <<~PATTERN
          {
            (def {:on_send :after_send} ...)
            (alias (sym {:on_send :after_send}) _source ...)
            (send nil? :alias_method {(sym {:on_send :after_send}) (str {"on_send" "after_send"})} _source ...)
          }
        PATTERN

        def on_casgn(node)
          return if !restrict_on_send?(node) || defined_send_callback?(node.parent)

          add_offense(node) do |corrector|
            corrector.remove(node)
          end
        end

        private

        def restrict_on_send?(node)
          node.name == :RESTRICT_ON_SEND
        end
      end
    end
  end
end

Version data entries

185 entries across 180 versions & 18 rubygems

Version Path
rubocop-1.71.0 lib/rubocop/cop/internal_affairs/useless_restrict_on_send.rb
rubocop-1.70.0 lib/rubocop/cop/internal_affairs/useless_restrict_on_send.rb
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/internal_affairs/useless_restrict_on_send.rb
rubocop-1.69.2 lib/rubocop/cop/internal_affairs/useless_restrict_on_send.rb
rubocop-1.69.1 lib/rubocop/cop/internal_affairs/useless_restrict_on_send.rb
rubocop-1.69.0 lib/rubocop/cop/internal_affairs/useless_restrict_on_send.rb
rubocop-1.68.0 lib/rubocop/cop/internal_affairs/useless_restrict_on_send.rb
rubocop-1.67.0 lib/rubocop/cop/internal_affairs/useless_restrict_on_send.rb
rubocop-1.66.1 lib/rubocop/cop/internal_affairs/useless_restrict_on_send.rb
rubocop-1.66.0 lib/rubocop/cop/internal_affairs/useless_restrict_on_send.rb
rubocop-1.65.1 lib/rubocop/cop/internal_affairs/useless_restrict_on_send.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/internal_affairs/useless_restrict_on_send.rb
rubocop-1.65.0 lib/rubocop/cop/internal_affairs/useless_restrict_on_send.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/internal_affairs/useless_restrict_on_send.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/internal_affairs/useless_restrict_on_send.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/internal_affairs/useless_restrict_on_send.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-1.64.1/lib/rubocop/cop/internal_affairs/useless_restrict_on_send.rb
rubocop-1.64.1 lib/rubocop/cop/internal_affairs/useless_restrict_on_send.rb
rubocop-1.63.4 lib/rubocop/cop/internal_affairs/useless_restrict_on_send.rb
rubocop-1.63.3 lib/rubocop/cop/internal_affairs/useless_restrict_on_send.rb