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

Fix TransactionExitStatement in_rescue? Logic #695

Merged
merged 1 commit into from Apr 26, 2022

Conversation

dorkrawk
Copy link
Contributor

@dorkrawk dorkrawk commented Apr 22, 2022

The old logic for in_rescue? in the TransactionExitStatement case missed situations where a transaction contained an exit outside of a rescue. For example:

ApplicationRecord.transaction do
  return if user.active?
rescue
  pass
end

This fix adds a new node matcher to find exit statements within the rescue body.


Before submitting the PR make sure the following are checked:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.
  • The PR relates to only one subject with a clear title
    and description in grammatically correct, complete sentences.
  • If this is a new cop, consider making a corresponding update to the Rails Style Guide.

@@ -0,0 +1 @@
* Fixes a false negative where the `in_rescue?` check would bypass situations where the return was inside a transaction but ouside of a rescue. ([@dorkrawk][])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like a typo.

Suggested change
* Fixes a false negative where the `in_rescue?` check would bypass situations where the return was inside a transaction but ouside of a rescue. ([@dorkrawk][])
* Fixes a false negative where the `in_rescue?` check would bypass situations where the return was inside a transaction but outside of a rescue. ([@dorkrawk][])

And can you add the PR address?

* [#695](https://github.com/rubocop/rubocop-rails/pull/695): Fixes a false negative where the `in_rescue?` check would bypass situations where the return was inside a transaction but outside of a rescue. ([@dorkrawk][])

Comment on lines 91 to 92
return if user.active?
^^^^^^ Exit statement `return` is not allowed. Use `raise` (rollback) or `next` (commit).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you indent it?

Suggested change
return if user.active?
^^^^^^ Exit statement `return` is not allowed. Use `raise` (rollback) or `next` (commit).
return if user.active?
^^^^^^ Exit statement `return` is not allowed. Use `raise` (rollback) or `next` (commit).

({return | break | send nil? :throw} ...)
...
)
PATTERN
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you move it? The following is an example:

        def_node_search :exit_statements, <<~PATTERN
          ({return | break | send nil? :throw} ...)
        PATTERN

+       def_node_matcher :rescue_body_return_node?, <<~PATTERN
+         (:resbody ...
+           ...
+           ({return | break | send nil? :throw} ...)
+           ...
+         )
+       PATTERN

        def on_send(node)

@dorkrawk
Copy link
Contributor Author

@koic Ok, I've made the requested updates and re-squashed the commits

@koic koic merged commit 0f9dfd2 into rubocop:master Apr 26, 2022
@koic
Copy link
Member

koic commented Apr 26, 2022

Thanks!

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

Successfully merging this pull request may close these issues.

None yet

2 participants