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

Removes empty initialize methods: Style/RedundantInitialize: Remove unnecessary empty initialize method. #602

Closed
dorianmariecom opened this issue Jan 31, 2024 · 5 comments · Fixed by #605
Labels
rule change 👩‍⚖️ Suggest a style guide rule change

Comments

@dorianmariecom
Copy link

dorianmariecom commented Jan 31, 2024

Before:

class A
  def initialize
    raise NotImplementedError
  end
end

class B < A
  def initialize
  end
end

puts B.new

After:

# frozen_string_literal: true

class A
  def initialize
    raise NotImplementedError
  end
end

class B < A
end

puts B.new
> standardrb -v
1.33.0
@al2o3cr
Copy link
Contributor

al2o3cr commented Feb 5, 2024

This is also troublesome if the empty initialize takes arguments:

class A
  def initialize(not_used)
    # this space intentionally left blank
  end
end

a_variable_instead_of_some_kind_of_lookup = A

a_variable_instead_of_some_kind_of_lookup.new("foo")

Just had this happen on a client project.

@searls searls added the rule change 👩‍⚖️ Suggest a style guide rule change label Feb 6, 2024
@searls
Copy link
Contributor

searls commented Feb 6, 2024

Makes sense 👍. Standard should be skeptical of any cop that runs the risk of screwing up the user's intended behavior, and it's clear there are too many uncovered edge cases for this cop to be acceptable.

In the future, it'd be helpful to link to the RuboCop docs and share Standard's current configuration (in this case: Style/RedundantInitialize and

Style/RedundantInitialize:
  Enabled: true

@jasonkarns
Copy link
Collaborator

Worth noting that this cop is not Safe per rubocop's metadata. I presume that we are less likely to enable cops that are not safe.

@czeise
Copy link
Contributor

czeise commented Feb 6, 2024

Worth noting that this cop is not Safe per rubocop's metadata. I presume that we are less likely to enable cops that are not safe.

Definitely. It looks like this particular cop wasn't initially marked as unsafe until this same issue was identified.

@dorianmariecom
Copy link
Author

Thanks all

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rule change 👩‍⚖️ Suggest a style guide rule change
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants