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.48 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/useless_restrict_on_send.rb
harbr-0.1.47 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/useless_restrict_on_send.rb
harbr-0.1.46 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/useless_restrict_on_send.rb
harbr-0.1.45 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/useless_restrict_on_send.rb
harbr-0.1.44 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/useless_restrict_on_send.rb
harbr-0.1.43 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/useless_restrict_on_send.rb
harbr-0.1.42 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/useless_restrict_on_send.rb
harbr-0.1.41 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/useless_restrict_on_send.rb
harbr-0.1.39 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/useless_restrict_on_send.rb
harbr-0.1.38 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/useless_restrict_on_send.rb
harbr-0.1.37 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/useless_restrict_on_send.rb
getargv-0.3.3-universal-darwin vendor/bundle/ruby/3.3.0/gems/rubocop-1.59.0/lib/rubocop/cop/internal_affairs/useless_restrict_on_send.rb
rubocop-1.59.0 lib/rubocop/cop/internal_affairs/useless_restrict_on_send.rb
study_line-0.1.6 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/useless_restrict_on_send.rb
study_line-0.1.5 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/useless_restrict_on_send.rb
study_line-0.1.4 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/useless_restrict_on_send.rb
study_line-0.1.3 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/useless_restrict_on_send.rb
study_line-0.1.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/useless_restrict_on_send.rb
study_line-0.1.1 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/internal_affairs/useless_restrict_on_send.rb
rubocop-1.58.0 lib/rubocop/cop/internal_affairs/useless_restrict_on_send.rb