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

Lint for redundant keyword_init on structs #13501

Open
aldhsu opened this issue Nov 27, 2024 · 5 comments
Open

Lint for redundant keyword_init on structs #13501

aldhsu opened this issue Nov 27, 2024 · 5 comments

Comments

@aldhsu
Copy link

aldhsu commented Nov 27, 2024

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

Ruby 3.2 enables keyword_init on structs without specifying it. https://www.ruby-lang.org/en/news/2022/12/25/ruby-3-2-0-released/

# before 3.2
foo = Struct.new(:bar, keyword_init: true)

# equivalent after 3.2
foo = Struct.new(:bar)

Describe the solution you'd like

A lint for redundant keyword_init: true on structs.

@koic
Copy link
Member

koic commented Nov 27, 2024

This change introduces a behavioral incompatibility with the Struct.keyword_init? method, so it may be worth considering whether "redundant" is an appropriate designation.

Struct.new(:bar, keyword_init: true).keyword_init? # => true
Struct.new(:bar).keyword_init? # => nil

@aldhsu
Copy link
Author

aldhsu commented Nov 27, 2024

Good catch @koic!
Do you think that behaviour makes sense for the keyword_init? method?

The default has changed. Consumers relying on !Struct.new(:bar).keyword_init? will not be aware of the change. I'm not sure what the patterns usually are for ? methods in Ruby. However, I think in my experience, the expectation is that nil will be treated as falsy rather than truth-y for ? methods.

@koic
Copy link
Member

koic commented Nov 27, 2024

Yeah, RuboCop is used widely across applications, libraries, and frameworks, and user programs can include unexpected cases. Rather than focusing on whether it is a predicate method, it is important to consider the potentials for altering existing behavior, which differs from layout changes.

@aldhsu
Copy link
Author

aldhsu commented Nov 27, 2024

OK that makes sense.

consider the potentials for altering existing behavior, which differs from layout changes.

Would it make sense to have the cop disabled by default with the safety warning that keyword_init? doesn't respond the same when using keyword_init explicitly?

@koic
Copy link
Member

koic commented Nov 27, 2024

Technically, for Ruby 3.2+, it seems possible to pending it by default with SafeAutoCorrect: false.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants