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

Enable Style/RedundantBegin cop to avoid newly adding redundant begin block #34764

Merged
merged 1 commit into from Dec 21, 2018

Conversation

kamipo
Copy link
Member

@kamipo kamipo commented Dec 20, 2018

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, 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.

Copy link
Contributor

@kaspth kaspth left a 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"
Copy link
Contributor

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
Copy link
Contributor

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
Copy link
Contributor

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.

Copy link
Member Author

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).

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.
Copy link
Member Author

@kamipo kamipo left a 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
Copy link
Member Author

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
Copy link
Member Author

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).

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.

@kaspth
Copy link
Contributor

kaspth commented Dec 20, 2018

Cool, thanks!

@kaspth
Copy link
Contributor

kaspth commented Dec 20, 2018

Does rubocop have anything to remove send on the now public define_method and others? Or passing the block as a param instead of using yield like we did for _read_attribute?

@kamipo
Copy link
Member Author

kamipo commented Dec 20, 2018

Unfortunately any cop doesn't exist for now.

% cat send_and_block_param.rb
Object.send(:define_method, :foo) {}
Object.send(:remove_method, :foo)

def foo1
  bar { yield }
end

def foo2(&block)
  bar(&block)
end

def bar
  yield
end

% be rubocop send_and_block_param.rb
Inspecting 1 file
.

1 file inspected, no offenses detected

@kamipo kamipo merged commit b751928 into rails:master Dec 21, 2018
@kamipo kamipo deleted the avoid_redundant_begin branch December 21, 2018 08:36
koic added a commit to koic/oracle-enhanced that referenced this pull request Dec 21, 2018
koic added a commit to koic/rubocop-rails_config that referenced this pull request Dec 21, 2018
koic added a commit to koic/oracle-enhanced that referenced this pull request Dec 23, 2018
kamipo added a commit that referenced this pull request Jan 31, 2019
We enabled `Style/RedundantBegin` cop at #34764, but it is hard to
detect an offence if returning value put after the block.
kamipo added a commit that referenced this pull request Apr 4, 2019
We have `Style/RedundantBegin` cop (#34764) but it could not correct in
this case.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants