rubocop-thread_safety icon indicating copy to clipboard operation
rubocop-thread_safety copied to clipboard

False positive: ivar in Structs

Open akimd opened this issue 3 years ago • 1 comments

Hi,

In the following example (scaled down from real code), InstanceVariableInClassMethod's diagnostic is wrong.

$ cat struct.rb
class Foo
  def self.new_struct
    Struct.new(:foo) {
      def bar
        @bar ||= 42
      end
    }.new
  end
end

s = Foo.new_struct
puts s.bar
$ bundle exec rubocop struct.rb
Inspecting 1 file
C

Offenses:

struct.rb:5:9: C: ThreadSafety/InstanceVariableInClassMethod: Avoid instance variables in class methods.
        @bar ||= 42
        ^^^^

1 file inspected, 1 offense detected

This is version 0.4.4.

PS/ Where is the rendered documentation? I meant, there is https://docs.rubocop.org/rubocop-rails/cops_rails.html for instance, but I could not find anything comparable for Thread Safety.

akimd avatar Nov 01 '22 08:11 akimd

Hi! Thanks for the bug report.

How would you feel about opening a pull request to start resolving this? I'm happy to support you through it.

I would start by making a new failing test case using your example code.

mikegee avatar Nov 02 '22 23:11 mikegee

Thanks!

akimd avatar Sep 13 '24 14:09 akimd