Created
February 25, 2020 10:50
-
-
Save masutaka/68d372b7f7910f552eeb215bb2e5d3cc to your computer and use it in GitHub Desktop.
Forbid Dir.chdir using rubocop custom cop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require: | |
- ./lib/rubocop/cop/hoge |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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
Note to others that find this, that it is now built into the rubocop-thread_safety gem (https://github.com/rubocop/rubocop-thread_safety/blob/master/docs/modules/ROOT/pages/cops_threadsafety.adoc#threadsafetydirchdir)