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

Auto-correction of Style/StructInheritance may not be safe. #10036

Closed
QWYNG opened this issue Aug 24, 2021 · 0 comments · Fixed by #10037
Closed

Auto-correction of Style/StructInheritance may not be safe. #10036

QWYNG opened this issue Aug 24, 2021 · 0 comments · Fixed by #10037
Labels

Comments

@QWYNG
Copy link

QWYNG commented Aug 24, 2021

Is your feature request related to a problem? Please describe.

Auto-correct of Style/StructInheritance is safe now.
(https://docs.rubocop.org/rubocop/cops_style.html#stylestructinheritance)
But Auto-correct of Style/StructInheritance will change the inheritance chain for the constant like this.

class Person < Struct.new(:name)
  def name
    "Mr #{super}"
  end
end


Person.ancestors
=> [Person, #<Class:0x0000558c1ea93a58>, Struct, Enumerable, Object, PP::ObjectMixin, Kernel, BasicObject]
Person.new(:matz).name
=> "Mr matz"

to...

Person = Struct.new(:name) do
  def name
    "Mr #{super}"
  end
end


Person.ancestors
=> [Person, Struct, Enumerable, Object, PP::ObjectMixin, Kernel, BasicObject]
Person.new(:matz).name
=> `name': super: no superclass method `name' for #<struct Person name=:matz> (NoMethodError)

I think the purpose of Style/StructInheritance is to prevent these extra class levels. But this Auto-correct might change the semantics of the code.

Describe the solution you'd like

Make Auto-correct of Style/StructInheritance unsafe.

Rubocop version

 ~  rubocop -v
1.18.4
@koic koic added the bug label Aug 24, 2021
koic added a commit to koic/rubocop that referenced this issue Aug 24, 2021
…rection

Fixes rubocop#10036.

This PR marks `Style/StructInheritance` as unsafe auto-correction.
bbatsov pushed a commit that referenced this issue Aug 24, 2021
Fixes #10036.

This PR marks `Style/StructInheritance` as unsafe auto-correction.
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.

2 participants