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
Style/RedundantBegin
should detect redundant begin around memoization
#9601
Comments
Good catch! I think it can enhance |
…s around memoization Fixes rubocop#9601. This PR makes `Style/RedundantBegin` aware of redundant `begin`/`end` blocks around memoization.
…d memoization Fixes #9601. This PR makes `Style/RedundantBegin` aware of redundant `begin`/`end` blocks around memoization.
Thanks!! |
@deivid-rodriguez You are welcome! 😄 |
Thanks @deivid-rodriguez and @koic chef/chef#11220 |
Would have been nice to have a configuration option, as we deliberately use this style for complex memoization expressions to avoid the indent-mega-blocks. 😬 |
@Drenmi Can you show an example of how this fix complicates indentation, if that's what you mean by indent-mega-blocks? Isn't def foo
@foo ||= begin
mega_block do
# ...
# a lot of code
# ...
end
end
end equivalent to def foo
@foo ||=
mega_block do
# ...
# a lot of code
# ...
end
end in terms of indentation? Or does it violate another common style rule? |
The auto-correction with our current settings leaves: def something
@something ||= mega_block do
# ...
end
end after removing |
Ah I see. Yeah, I've been there. I don't like this mega-indented blocks and I do recall having to change the code manually to the alternative I mentioned since rubocop auto-correct prefers the other style. I don't get the problem with mismatched |
Kind of the same issue from @Drenmi here: # before
def decorator
@decorator ||= begin
case reminder.remindable_type
when 'EmailSchedule'
Notifier::EmailScheduleDecorator.new(reminder)
when 'ReportScheduled'
Notifier::ReportScheduledDecorator.new(reminder)
else
raise 'Unsupported type of Notifie'
end
end
end
# after
def decorator
@decorator ||= case reminder.remindable_type
when 'EmailSchedule'
Notifier::EmailScheduleDecorator.new(reminder)
when 'ReportScheduled'
Notifier::ReportScheduledDecorator.new(reminder)
else
raise 'Unsupported type of Notifier'
end
end |
Is your feature request related to a problem? Please describe.
I'd like
rubocop
to be able to detect redundant begin/end blocks around memoization, like here: 3b48d8b.Describe the solution you'd like
I think it would fit nicely inside the existing
Style/RedundantBegin
cop.Describe alternatives you've considered
A separate cop if excluding this case was intentional, I guess.
The text was updated successfully, but these errors were encountered: