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

False positive for ModuleInitialize #1505

Open
jimeh opened this issue Jan 10, 2020 · 1 comment · May be fixed by #1746
Open

False positive for ModuleInitialize #1505

jimeh opened this issue Jan 10, 2020 · 1 comment · May be fixed by #1746
Labels

Comments

@jimeh
Copy link

jimeh commented Jan 10, 2020

I've come across two instances of a false positive ModuleInitialize warnings when using anonymous class definitions.

First the RSpec related one:

module Alfa
  RSpec.describe Bravo do
    let(:klass) do
      Class.new do
        def initialize; end
      end
    end
  end
end

And secondly a DSL-based one:

module Alfa
  bravo Class.new do
    def initialize; end
  end
end

The DSL variant is probably not very likely to be done in a real-world scenario, but technically the initialize method is not defined on the module that Reek warns about. The RSpec variant is something I've come across in the real world however.

If the anonymous class is assigned to a constant it does not fail however, which makes sense cause it's no longer an anonymous class:

module Alfa
  Bravo = Class.new do
    def initialize; end
  end
end

A slightly different variant of this was reported in #1137 and fixed in #1287, but it specifically requires the anonymous class definition to be within a method definition on the module. Hence both of these are not reported:

module Alfa
  def self.bravo
    Class.new do
      def initialize; end
    end
  end
end
module Alfa
  def bravo
    Class.new do
      def initialize; end
    end
  end
end
@mvz mvz added the defect label Jan 10, 2020
@JuanVqz JuanVqz linked a pull request Oct 18, 2023 that will close this issue
JuanVqz added a commit to JuanVqz/reek that referenced this issue Oct 18, 2023
JuanVqz added a commit to JuanVqz/reek that referenced this issue Oct 18, 2023
JuanVqz added a commit to JuanVqz/reek that referenced this issue Oct 18, 2023
JuanVqz added a commit to JuanVqz/reek that referenced this issue Oct 21, 2023
@Jwashton
Copy link

Jwashton commented Dec 5, 2023

In a very similar case, I also get a false positive for this rule when defining a Data class with an initializer.

module Example
  Thing = Data.define(:value) do
    def initialize(value: 'some default')
      super(value:)
    end
  end
end

@mvz mvz changed the title False positive for MonduleInitialize False positive for ModuleInitialize Dec 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants