Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Cop::Base API for ThreadSafety department #8

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -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
Expand Down
Expand Up @@ -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
Expand All @@ -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

Expand Down
37 changes: 20 additions & 17 deletions lib/rubocop/cop/thread_safety/mutable_class_instance_variable.rb
Expand Up @@ -72,7 +72,8 @@ module ThreadSafety
# end
# end.freeze
# end
class MutableClassInstanceVariable < Cop
class MutableClassInstanceVariable < Base
extend AutoCorrector
include FrozenStringLiteral
include ConfigurableEnforcedStyle

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -152,15 +151,19 @@ 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)
return unless mutable_literal?(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)
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/thread_safety/new_thread.rb
Expand Up @@ -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

Expand Down