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

Flag break in with_lock for Rails/TransactionExitStatement #1005

Merged

Conversation

splattael
Copy link
Contributor

@splattael splattael commented May 11, 2023

Previously, the following code was not flagged but is considered problematic:

ApplicationRecord.with_lock do
  break if user.active?
end

Refs:


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.
  • If this is a new cop, consider making a corresponding update to the Rails Style Guide.

@splattael splattael force-pushed the rails-transaction-exit-statement-with-lock-fix branch from 5752ca7 to 5103efd Compare May 11, 2023 15:23
end
RUBY
end
shared_examples 'flags transaction exit statements' do |method|
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've extracted the existing specs into shared_examples to make sure that all cases are covered.

!statement_node.ancestors.find(&:block_type?).method?(:transaction)
block_node = statement_node.ancestors.find(&:block_type?)

RESTRICT_ON_SEND.none? { |name| block_node.method?(name) }
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The crux of this PR where with_lock was not checked and produced false negatives.

@koic
Copy link
Member

koic commented May 15, 2023

Can you update the doc to include with_lock?

Previously, the following code was not flagged but is considered
problematic:

  ApplicationRecord.with_lock do
    break if user.active?
  end
@splattael splattael force-pushed the rails-transaction-exit-statement-with-lock-fix branch from 5103efd to 673d52e Compare May 15, 2023 06:08
@splattael
Copy link
Contributor Author

@koic Done! I've only added a bad examples as there are no good examples for with_lock.

I suppose it's because the same resolution as for transaction? 🤔

@koic koic merged commit d45d052 into rubocop:master May 26, 2023
4 checks passed
@koic
Copy link
Member

koic commented May 26, 2023

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