Skip to content

Instantly share code, notes, and snippets.

@masutaka
Created February 25, 2020 10:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save masutaka/68d372b7f7910f552eeb215bb2e5d3cc to your computer and use it in GitHub Desktop.
Save masutaka/68d372b7f7910f552eeb215bb2e5d3cc to your computer and use it in GitHub Desktop.
Forbid Dir.chdir using rubocop custom cop
require:
- ./lib/rubocop/cop/hoge
# frozen_string_literal: true
module RuboCop
module Cop
module Hoge
# This cop is used to ban the use of Dir.chdir.
# Dir.chdir is NOT thread-safe.
#
# @see https://bugs.ruby-lang.org/issues/9785
class DirChdir < ::RuboCop::Cop::Cop
MSG = 'Do not use Dir.chdir as it is not thread-safe.'
def_node_matcher :dir_chdir_node?, <<~PATTERN
(send (const nil? :Dir) :chdir ...)
PATTERN
def on_send(node)
return unless dir_chdir_node?(node)
add_offense(node, location: :selector)
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment