Class: RuboCop::Cop::ThreadSafety::DirChdir

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

Overview

Avoid using ‘Dir.chdir` due to its process-wide effect.

Examples:

# bad
Dir.chdir("/var/run")

# bad
FileUtils.chdir("/var/run")

Constant Summary collapse

MESSAGE =
'Avoid using `%<module>s.%<method>s` due to its process-wide effect.'
RESTRICT_ON_SEND =
%i[chdir cd].freeze

Instance Method Summary collapse

Instance Method Details

#chdir?(node) ⇒ Object



19
20
21
22
23
24
# File 'lib/rubocop/cop/thread_safety/dir_chdir.rb', line 19

def_node_matcher :chdir?, <<~MATCHER
  {
    (send (const {nil? cbase} {:Dir :FileUtils}) :chdir ...)
    (send (const {nil? cbase} :FileUtils) :cd ...)
  }
MATCHER

#on_send(node) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/rubocop/cop/thread_safety/dir_chdir.rb', line 26

def on_send(node)
  chdir?(node) do
    add_offense(
      node,
      message: format(MESSAGE, module: node.receiver.short_name, method: node.method_name)
    )
  end
end