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
Enable Style/RedundantBegin
cop to avoid newly adding redundant begin block
#34764
Conversation
f423163
to
09fe141
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, I'm 👍
I was actually looking for this rubocop config, but couldn't find it. How did you find it?
assert_equal :speak, events[0].payload[:action] | ||
assert_equal data, events[0].payload[:data] | ||
ensure | ||
ActiveSupport::Notifications.unsubscribe "perform_action.action_cable" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fantastic! Now we can just put the ensure
at the top level. That always bothered me before.
end | ||
result = super | ||
payload[:status] = response.status | ||
result |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could switch to tap
while we're here.
if Thread.current.status == "aborting" | ||
rollback_transaction if transaction | ||
else | ||
begin |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Huh, I would've thought this begin could also be removed and the rescue
could be lined up with else
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This begin
is to rescue an exception raised by commit_transaction
(commit_transaction
is called when a transaction block is succeeded).
rails/activerecord/test/cases/transactions_test.rb
Lines 572 to 585 in 49f9dff
def test_rollback_when_commit_raises | |
assert_called(Topic.connection, :begin_db_transaction) do | |
Topic.connection.stub(:commit_db_transaction, -> { raise("OH NOES") }) do | |
assert_called(Topic.connection, :rollback_db_transaction) do | |
e = assert_raise RuntimeError do | |
Topic.transaction do | |
Topic.connection.materialize_transactions | |
end | |
end | |
assert_equal "OH NOES", e.message | |
end | |
end | |
end | |
end |
This begin
couldn't be removed since a begin could be ommited only when toplevel in methods and do-end blocks, but here is in if-else block.
…in block Currently we sometimes find a redundant begin block in code review (e.g. rails#33604 (comment)). I'd like to enable `Style/RedundantBegin` cop to avoid that, since rescue/else/ensure are allowed inside do/end blocks in Ruby 2.5 (https://bugs.ruby-lang.org/issues/12906), so we'd probably meets with that situation than before.
09fe141
to
892e38c
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was actually looking for this rubocop config, but couldn't find it. How did you find it?
.rubocop.yml in our rails apps has no DisabledByDefault: true
.
So it is easy to find the cop to police the redundant code.
% cat redundant_begin.rb
def foo(cond)
yield cond
end
foo do |cond|
begin
ensure
if cond
else
begin
ensure
nil
end
end
end
end
% be rubocop redundant_begin.rb
Inspecting 1 file
C
Offenses:
redundant_begin.rb:6:3: C: Style/RedundantBegin: Redundant begin block detected.
begin
^^^^^
1 file inspected, 1 offense detected
@@ -30,13 +30,11 @@ def process_action(*args) | |||
ActiveSupport::Notifications.instrument("start_processing.action_controller", raw_payload.dup) | |||
|
|||
ActiveSupport::Notifications.instrument("process_action.action_controller", raw_payload) do |payload| | |||
begin | |||
result = super | |||
super.tap do | |||
payload[:status] = response.status |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could switch to
tap
while we're here.
Modified to use tap
.
if Thread.current.status == "aborting" | ||
rollback_transaction if transaction | ||
else | ||
begin |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This begin
is to rescue an exception raised by commit_transaction
(commit_transaction
is called when a transaction block is succeeded).
rails/activerecord/test/cases/transactions_test.rb
Lines 572 to 585 in 49f9dff
def test_rollback_when_commit_raises | |
assert_called(Topic.connection, :begin_db_transaction) do | |
Topic.connection.stub(:commit_db_transaction, -> { raise("OH NOES") }) do | |
assert_called(Topic.connection, :rollback_db_transaction) do | |
e = assert_raise RuntimeError do | |
Topic.transaction do | |
Topic.connection.materialize_transactions | |
end | |
end | |
assert_equal "OH NOES", e.message | |
end | |
end | |
end | |
end |
This begin
couldn't be removed since a begin could be ommited only when toplevel in methods and do-end blocks, but here is in if-else block.
Cool, thanks! |
Does rubocop have anything to remove |
Unfortunately any cop doesn't exist for now.
|
Follow up of rails/rails#34764.
Follow up of rails/rails#34764.
Follow up of rails/rails#34764.
We enabled `Style/RedundantBegin` cop at #34764, but it is hard to detect an offence if returning value put after the block.
We have `Style/RedundantBegin` cop (#34764) but it could not correct in this case.
Currently we sometimes find a redundant begin block in code review
(e.g. #33604 (comment)).
I'd like to enable
Style/RedundantBegin
cop to avoid that, sincerescue/else/ensure are allowed inside do/end blocks in Ruby 2.5
(https://bugs.ruby-lang.org/issues/12906), so we'd probably meets with
that situation than before.