From 8cfb15d5e01a00037c31162f16c41268e5c63dc7 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Thu, 19 May 2022 01:26:24 +0900 Subject: [PATCH] Use `Cop::Base` API for `ThreadSafety` department Follow https://github.com/rubocop-hq/rubocop/pull/7868. The legacy `Cop::Cop` API is soft deprecated and this PR use new `Cop::Base` API instead. > maintain any RuboCop extensions, as the legacy API will be removed in RuboCop 2.0. https://metaredux.com/posts/2020/10/21/rubocop-1-0.html This new API requires RuboCop 0.87 or higher. https://github.com/rubocop/rubocop/blob/master/CHANGELOG.md#0870-2020-07-06 That dependent version update will be resolved in #7, so there are no updates in this PR. --- .../class_and_module_attributes.rb | 2 +- .../instance_variable_in_class_method.rb | 4 +- .../mutable_class_instance_variable.rb | 37 ++++++++++--------- lib/rubocop/cop/thread_safety/new_thread.rb | 2 +- 4 files changed, 24 insertions(+), 21 deletions(-) diff --git a/lib/rubocop/cop/thread_safety/class_and_module_attributes.rb b/lib/rubocop/cop/thread_safety/class_and_module_attributes.rb index f72a8db..ea58996 100644 --- a/lib/rubocop/cop/thread_safety/class_and_module_attributes.rb +++ b/lib/rubocop/cop/thread_safety/class_and_module_attributes.rb @@ -12,7 +12,7 @@ module ThreadSafety # class User # cattr_accessor :current_user # end - class ClassAndModuleAttributes < Cop + class ClassAndModuleAttributes < Base MSG = 'Avoid mutating class and module attributes.' RESTRICT_ON_SEND = %i[ mattr_writer mattr_accessor cattr_writer cattr_accessor diff --git a/lib/rubocop/cop/thread_safety/instance_variable_in_class_method.rb b/lib/rubocop/cop/thread_safety/instance_variable_in_class_method.rb index 5e472f6..8c060d7 100644 --- a/lib/rubocop/cop/thread_safety/instance_variable_in_class_method.rb +++ b/lib/rubocop/cop/thread_safety/instance_variable_in_class_method.rb @@ -53,7 +53,7 @@ module ThreadSafety # # module_function :test # end - class InstanceVariableInClassMethod < Cop + class InstanceVariableInClassMethod < Base MSG = 'Avoid instance variables in class methods.' RESTRICT_ON_SEND = %i[ instance_variable_set @@ -73,7 +73,7 @@ def on_ivar(node) return if method_definition?(node) return if synchronized?(node) - add_offense(node, location: :name, message: MSG) + add_offense(node.loc.name, message: MSG) end alias on_ivasgn on_ivar diff --git a/lib/rubocop/cop/thread_safety/mutable_class_instance_variable.rb b/lib/rubocop/cop/thread_safety/mutable_class_instance_variable.rb index c88d8e3..ec554f3 100644 --- a/lib/rubocop/cop/thread_safety/mutable_class_instance_variable.rb +++ b/lib/rubocop/cop/thread_safety/mutable_class_instance_variable.rb @@ -72,7 +72,8 @@ module ThreadSafety # end # end.freeze # end - class MutableClassInstanceVariable < Cop + class MutableClassInstanceVariable < Base + extend AutoCorrector include FrozenStringLiteral include ConfigurableEnforcedStyle @@ -108,23 +109,21 @@ def on_masgn(node) end end - def autocorrect(node) + def autocorrect(corrector, node) expr = node.source_range - lambda do |corrector| - splat_value = splat_value(node) - if splat_value - correct_splat_expansion(corrector, expr, splat_value) - elsif node.array_type? && !node.bracketed? - corrector.insert_before(expr, '[') - corrector.insert_after(expr, ']') - elsif requires_parentheses?(node) - corrector.insert_before(expr, '(') - corrector.insert_after(expr, ')') - end - - corrector.insert_after(expr, '.freeze') + splat_value = splat_value(node) + if splat_value + correct_splat_expansion(corrector, expr, splat_value) + elsif node.array_type? && !node.bracketed? + corrector.insert_before(expr, '[') + corrector.insert_after(expr, ']') + elsif requires_parentheses?(node) + corrector.insert_before(expr, '(') + corrector.insert_after(expr, ')') end + + corrector.insert_after(expr, '.freeze') end private @@ -152,7 +151,9 @@ def strict_check(value) return if operation_produces_threadsafe_object?(value) return if frozen_string_literal?(value) - add_offense(value) + add_offense(value) do |corrector| + autocorrect(corrector, value) + end end def check(value) @@ -160,7 +161,9 @@ def check(value) range_enclosed_in_parentheses?(value) return if frozen_string_literal?(value) - add_offense(value) + add_offense(value) do |corrector| + autocorrect(corrector, value) + end end def in_class?(node) diff --git a/lib/rubocop/cop/thread_safety/new_thread.rb b/lib/rubocop/cop/thread_safety/new_thread.rb index a4c78c0..5a3660d 100644 --- a/lib/rubocop/cop/thread_safety/new_thread.rb +++ b/lib/rubocop/cop/thread_safety/new_thread.rb @@ -10,7 +10,7 @@ module ThreadSafety # @example # # bad # Thread.new { do_work } - class NewThread < Cop + class NewThread < Base MSG = 'Avoid starting new threads.' RESTRICT_ON_SEND = %i[new].freeze