Class: RuboCop::Cop::ThreadSafety::ClassAndModuleAttributes

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

Overview

Avoid mutating class and module attributes.

They are implemented by class variables, which are not thread-safe.

Examples:

# bad
class User
  cattr_accessor :current_user
end

Constant Summary collapse

MSG =
'Avoid mutating class and module attributes.'
RESTRICT_ON_SEND =
%i[
  mattr_writer mattr_accessor cattr_writer cattr_accessor
  class_attribute
  attr attr_accessor attr_writer
  attr_internal attr_internal_accessor attr_internal_writer
].freeze

Instance Method Summary collapse

Instance Method Details

#attr?(node) ⇒ Object



32
33
34
35
36
# File 'lib/rubocop/cop/thread_safety/class_and_module_attributes.rb', line 32

def_node_matcher :attr?, <<~MATCHER
  (send nil?
    {:attr :attr_accessor :attr_writer}
    ...)
MATCHER

#attr_internal?(node) ⇒ Object



39
40
41
42
43
# File 'lib/rubocop/cop/thread_safety/class_and_module_attributes.rb', line 39

def_node_matcher :attr_internal?, <<~MATCHER
  (send nil?
    {:attr_internal :attr_internal_accessor :attr_internal_writer}
    ...)
MATCHER

#class_attr?(node) ⇒ Object



46
47
48
49
50
# File 'lib/rubocop/cop/thread_safety/class_and_module_attributes.rb', line 46

def_node_matcher :class_attr?, <<~MATCHER
  (send nil?
    :class_attribute
    ...)
MATCHER

#mattr?(node) ⇒ Object



25
26
27
28
29
# File 'lib/rubocop/cop/thread_safety/class_and_module_attributes.rb', line 25

def_node_matcher :mattr?, <<~MATCHER
  (send nil?
    {:mattr_writer :mattr_accessor :cattr_writer :cattr_accessor}
    ...)
MATCHER

#on_send(node) ⇒ Object



52
53
54
55
56
# File 'lib/rubocop/cop/thread_safety/class_and_module_attributes.rb', line 52

def on_send(node)
  return unless mattr?(node) || (!class_attribute_allowed? && class_attr?(node)) || singleton_attr?(node)

  add_offense(node)
end