Class: RuboCop::Cop::ThreadSafety::MethodRedefinition

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/thread_safety/method_redefinition.rb

Overview

Avoid the thread-unsafe combination of remove_method followed by defining a method with the same name. This can lead to a race condition, as these two actions are not atomic. As a safer alternative, consider aliasing the method to itself instead.

Examples:

# bad
remove_method :foo
def foo; end

# good
alias_method :foo, :foo
def foo; end

# good
alias foo foo
def foo; end

Constant Summary collapse

MSG =
'Do not use `remove_method` followed by method definition.'
RESTRICT_ON_SEND =
%i[remove_method].freeze

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object Also known as: on_csend



28
29
30
31
32
33
34
35
36
# File 'lib/rubocop/cop/thread_safety/method_redefinition.rb', line 28

def on_send(node)
  return unless (def_node = node.right_sibling)
  return unless def_node.def_type?
  return unless node.arguments.one?
  return unless (method_name_node = node.first_argument).type?(:str, :sym)
  return unless def_node.method?(method_name_node.value)

  add_offense(node)
end