Skip to content

Conversation

adamstegman
Copy link

@adamstegman adamstegman commented Oct 9, 2025

abort is very similar to exit in Ruby (docs), but it takes a string message or Exception. Rails applications should avoid its usage for the same reasons as they avoid exit and exit!. Since they are used in the same way, I thought it belonged in the same cop, but am glad to move it to a new one if that's preferred.


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.

`abort` is very similar to `exit` in Ruby
([docs](https://docs.ruby-lang.org/en/3.4/Kernel.html#method-i-abort)),
but it takes a string message or Exception. Rails applications should
avoid its usage for the same reasons as they avoid `exit` and `exit!`.
Since they are used in the same way, I thought it belonged in the same
cop, but am glad to move it to a new one if that's preferred.
@adamstegman adamstegman marked this pull request as ready for review October 9, 2025 19:26

MSG = 'Do not use `exit` in Rails applications.'
RESTRICT_ON_SEND = %i[exit exit!].freeze
MSG = 'Do not use `exit` or `abort` in Rails applications.'
Copy link
Member

Choose a reason for hiding this comment

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

It might be clearer to limit it to methods that are actually used, for example as shown below.
The test code should also be updated accordingly to reflect this change.

diff --git a/lib/rubocop/cop/rails/exit.rb b/lib/rubocop/cop/rails/exit.rb
index fd1901970..5517f1454 100644
--- a/lib/rubocop/cop/rails/exit.rb
+++ b/lib/rubocop/cop/rails/exit.rb
@@ -26,12 +26,16 @@ module RuboCop
       class Exit < Base
         include ConfigurableEnforcedStyle
 
-        MSG = 'Do not use `exit` or `abort` in Rails applications.'
+        MSG = 'Do not use `%<current>s` in Rails applications.'
         RESTRICT_ON_SEND = %i[exit exit! abort].freeze
         EXPLICIT_RECEIVERS = %i[Kernel Process].freeze
 
         def on_send(node)
-          add_offense(node.loc.selector) if offending_node?(node)
+          return unless offending_node?(node)
+
+          message = format(MSG, current: node.method_name)
+
+          add_offense(node.loc.selector, message: message)
         end
 
         private

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.

2 participants