diff --git a/lib/rubocop-thread_safety.rb b/lib/rubocop-thread_safety.rb index ccd0e72..c246c4e 100644 --- a/lib/rubocop-thread_safety.rb +++ b/lib/rubocop-thread_safety.rb @@ -10,7 +10,7 @@ require 'rubocop/cop/mixin/operation_with_threadsafe_result' -require 'rubocop/cop/thread_safety/instance_variable_in_class_method' +require 'rubocop/cop/thread_safety/class_instance_variable' require 'rubocop/cop/thread_safety/class_and_module_attributes' require 'rubocop/cop/thread_safety/mutable_class_instance_variable' require 'rubocop/cop/thread_safety/new_thread' diff --git a/lib/rubocop/cop/thread_safety/instance_variable_in_class_method.rb b/lib/rubocop/cop/thread_safety/class_instance_variable.rb similarity index 97% rename from lib/rubocop/cop/thread_safety/instance_variable_in_class_method.rb rename to lib/rubocop/cop/thread_safety/class_instance_variable.rb index 10de928..4128d2b 100644 --- a/lib/rubocop/cop/thread_safety/instance_variable_in_class_method.rb +++ b/lib/rubocop/cop/thread_safety/class_instance_variable.rb @@ -3,7 +3,7 @@ module RuboCop module Cop module ThreadSafety - # Avoid instance variables in class methods. + # Avoid class instance variables. # # @example # # bad @@ -61,8 +61,8 @@ module ThreadSafety # # module_function :test # end - class InstanceVariableInClassMethod < Base - MSG = 'Avoid instance variables in class methods.' + class ClassInstanceVariable < Base + MSG = 'Avoid class instance variables.' RESTRICT_ON_SEND = %i[ instance_variable_set instance_variable_get diff --git a/spec/rubocop/cop/thread_safety/instance_variable_in_class_method_spec.rb b/spec/rubocop/cop/thread_safety/class_instance_variable_spec.rb similarity index 88% rename from spec/rubocop/cop/thread_safety/instance_variable_in_class_method_spec.rb rename to spec/rubocop/cop/thread_safety/class_instance_variable_spec.rb index 5eb322b..e53b185 100644 --- a/spec/rubocop/cop/thread_safety/instance_variable_in_class_method_spec.rb +++ b/spec/rubocop/cop/thread_safety/class_instance_variable_spec.rb @@ -1,12 +1,14 @@ # frozen_string_literal: true -RSpec.describe RuboCop::Cop::ThreadSafety::InstanceVariableInClassMethod, :config do +RSpec.describe RuboCop::Cop::ThreadSafety::ClassInstanceVariable, :config do + let(:msg) { 'Avoid class instance variables.' } + it 'registers an offense for assigning to an ivar in a class method' do expect_offense(<<~RUBY) class Test def self.some_method(params) @params = params - ^^^^^^^ Avoid instance variables in class methods. + ^^^^^^^ #{msg} end end RUBY @@ -112,7 +114,7 @@ class Test Class.new do def self.area @area ||= some_computation - ^^^^^ Avoid instance variables in class methods. + ^^^^^ #{msg} end end end @@ -125,7 +127,7 @@ def self.area class Test def self.some_method do_work(@params) - ^^^^^^^ Avoid instance variables in class methods. + ^^^^^^^ #{msg} end end RUBY @@ -136,7 +138,7 @@ def self.some_method module ClassMethods def some_method(params) @params = params - ^^^^^^^ Avoid instance variables in class methods. + ^^^^^^^ #{msg} end end RUBY @@ -148,7 +150,7 @@ module Test class_methods do def some_method(params) @params = params - ^^^^^^^ Avoid instance variables in class methods. + ^^^^^^^ #{msg} end end end @@ -161,7 +163,7 @@ class Test class << self def some_method(params) @params = params - ^^^^^^^ Avoid instance variables in class methods. + ^^^^^^^ #{msg} end end end @@ -173,7 +175,7 @@ def some_method(params) class Test define_singleton_method(:some_method) do |params| @params = params - ^^^^^^^ Avoid instance variables in class methods. + ^^^^^^^ #{msg} end end RUBY @@ -184,7 +186,7 @@ class Test class Test def self.some_method do_work(instance_variable_get(:@params)) - ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid instance variables in class methods. + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{msg} end end RUBY @@ -196,7 +198,7 @@ class Test class << self def some_method(name, params) instance_variable_set(:"@\#{name}", params) - ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid instance variables in class methods. + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{msg} end end end @@ -208,7 +210,7 @@ def some_method(name, params) module ClassMethods def some_method(params) instance_variable_set(:@params, params) - ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid instance variables in class methods. + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{msg} end end RUBY @@ -220,7 +222,7 @@ module Test class_methods do def some_method(params) instance_variable_set(:@params, params) - ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid instance variables in class methods. + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{msg} end end end @@ -232,7 +234,7 @@ def some_method(params) class Test define_singleton_method(:some_method) do |params| instance_variable_set(:@params, params) - ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid instance variables in class methods. + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{msg} end end RUBY @@ -245,7 +247,7 @@ module Test def some_method(params) instance_variable_set(:@params, params) - ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid instance variables in class methods. + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{msg} end end RUBY @@ -256,7 +258,7 @@ def some_method(params) module Test def some_method(params) instance_variable_set(:@params, params) - ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid instance variables in class methods. + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{msg} end module_function :some_method @@ -271,7 +273,7 @@ module Test def some_method(params) @params = params - ^^^^^^^ Avoid instance variables in class methods. + ^^^^^^^ #{msg} end end RUBY @@ -282,7 +284,7 @@ def some_method(params) module Test def some_method(params) @params = params - ^^^^^^^ Avoid instance variables in class methods. + ^^^^^^^ #{msg} end module_function :some_method @@ -295,7 +297,7 @@ def some_method(params) def separate_with(separator) Example.class_eval do @separator = separator - ^^^^^^^^^^ Avoid instance variables in class methods. + ^^^^^^^^^^ #{msg} end end RUBY @@ -304,7 +306,7 @@ def separate_with(separator) def separate_with(separator) ::Example.class_eval do @separator = separator - ^^^^^^^^^^ Avoid instance variables in class methods. + ^^^^^^^^^^ #{msg} end end RUBY @@ -315,7 +317,7 @@ def separate_with(separator) def separate_with(separator) Example.class_exec do @separator = separator - ^^^^^^^^^^ Avoid instance variables in class methods. + ^^^^^^^^^^ #{msg} end end RUBY