Sha256: f9ff85cc2e5e904fdf9a10043e93a6de35fe2ea95dc7156b5a4844ad52c52c22

Contents?: true

Size: 1.09 KB

Versions: 7

Compression:

Stored size: 1.09 KB

Contents

# Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
# frozen_string_literal: true

module RuboCop
  module Cop
    module Security
      module Thread
        # This cop checks for the use of `Thread#new`.
        #
        # @example
        #
        #   # bad
        #   Thread.new
        #
        #   # good
        #   Contrast::Agent::Thread.new
        class New < Cop
          MSG = '`Thread#new` will not run its contents in Contrast scope. Use Contrast::Agent::Thread.'

          def_node_matcher :thread_new, <<~PATTERN
            (send (const {nil? cbase} :Thread) ${:new} ...)
          PATTERN

          def on_send node
            thread_new(node) do |method|
              add_offense(node.receiver,
                          location: :expression,
                          message: format(MSG, method: method))
            end
          end

          def autocorrect node
            ->(corrector) { corrector.replace(node.loc.expression, 'Contrast::Agent::Thread') }
          end
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
contrast-agent-3.10.2 resources/rubocops/thread/new_cop.rb
contrast-agent-3.10.1 resources/rubocops/thread/new_cop.rb
contrast-agent-3.10.0 resources/rubocops/thread/new_cop.rb
contrast-agent-3.9.1 resources/rubocops/thread/new_cop.rb
contrast-agent-3.9.0 resources/rubocops/thread/new_cop.rb
contrast-agent-3.8.5 resources/rubocops/thread/new_cop.rb
contrast-agent-3.8.4 resources/rubocops/thread/new_cop.rb