Skip to content

False positive of Instance Variable Assumption with Struct initialize overload #1492

Closed
@ryym

Description

Hi, I found an issue about Instance Variable Assumption that occurs when a class have a Struct with its own initialize method in its block.

Example code:

class Foo
  Bar = Struct.new(:a) do
    def initialize(a)
      super
      p 'bar created'
    end
  end

  def initialize
    @foo = :foo
  end

  def foo?
    @foo == :foo
  end
end

When I run reek against this file, Reek reports a warning of Instance Variable Assumption, even though @foo in Foo class is initialized correctly.

[1]:InstanceVariableAssumption: Foo assumes too much for instance variable '@foo' [https://github.com/troessner/reek/blob/v5.4.0/docs/Instance-Variable-Assumption.md]

It looks like the cause is Bar which has a block containing an initialize method.
I confirmed that Reek did not warn if:

  • Bar does not have an initialize method.
  • Bar is defined by normal class syntax (class Bar ... end).
  • Bar is defined after the initialize method of Foo.

Similar issue: #1137

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions