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
harbr-0.1.69 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/useless_restrict_on_send.rb
harbr-0.1.68 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/useless_restrict_on_send.rb
harbr-0.1.67 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/useless_restrict_on_send.rb
harbr-0.1.66 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/useless_restrict_on_send.rb
harbr-0.1.65 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/useless_restrict_on_send.rb
harbr-0.1.64 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/useless_restrict_on_send.rb
harbr-0.1.63 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/useless_restrict_on_send.rb
harbr-0.1.62 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/useless_restrict_on_send.rb
harbr-0.1.61 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/useless_restrict_on_send.rb
harbr-0.1.60 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/useless_restrict_on_send.rb
harbr-0.1.59 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/useless_restrict_on_send.rb
harbr-0.1.58 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/useless_restrict_on_send.rb
harbr-0.1.57 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/useless_restrict_on_send.rb
harbr-0.1.56 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/useless_restrict_on_send.rb
harbr-0.1.55 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/useless_restrict_on_send.rb
harbr-0.1.54 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/useless_restrict_on_send.rb
harbr-0.1.53 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/useless_restrict_on_send.rb
harbr-0.1.52 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/useless_restrict_on_send.rb
harbr-0.1.50 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/useless_restrict_on_send.rb
harbr-0.1.49 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/useless_restrict_on_send.rb