Skip to content

Rails/TransactionExitStatement false positive when mixing transaction and other blocks #658

@Tietew

Description

@Tietew

Rails/TransactionExitStatement detected on following break but its false positive.

FooModel.transaction do
  loop do
    break if should_exit? # just exit loop, not transaction
    some_works
  end
  puts "This line is always shown!"
end

Expected behavior

No offences detected.

Actual behavior

Rails/TransactionExitStatement detected at break

Steps to reproduce the problem

$ cat test.rb
FooModel.transaction do
  loop do
    break if should_exit? # just exit loop, not transaction
    some_works
  end
end
$ bundle exec rubocop test.rb --only Rails
Inspecting 1 file
C

Offenses:

test.rb:3:5: C: Rails/TransactionExitStatement: Exit statement break is not allowed. Use raise (rollback) or next (commit).
    break if should_exit? # just exit loop, not transaction
    ^^^^^

1 file inspected, 1 offense detected

RuboCop version

1.26.0 (using Parser 3.1.1.0, rubocop-ast 1.16.0, running on ruby 2.7.5 x86_64-linux)
  - rubocop-rails 2.14.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions