summaryrefslogtreecommitdiff
path: root/rubocop/cop/ban_catch_throw.rb
diff options
context:
space:
mode:
Diffstat (limited to 'rubocop/cop/ban_catch_throw.rb')
-rw-r--r--rubocop/cop/ban_catch_throw.rb34
1 files changed, 34 insertions, 0 deletions
diff --git a/rubocop/cop/ban_catch_throw.rb b/rubocop/cop/ban_catch_throw.rb
new file mode 100644
index 00000000000..42301d5512f
--- /dev/null
+++ b/rubocop/cop/ban_catch_throw.rb
@@ -0,0 +1,34 @@
+# frozen_string_literal: true
+
+module RuboCop
+ module Cop
+ # Bans the use of 'catch/throw', as exceptions are better for errors and
+ # they are equivalent to 'goto' for flow control, with all the problems
+ # that implies.
+ #
+ # @example
+ # # bad
+ # catch(:error) do
+ # throw(:error)
+ # end
+ #
+ # # good
+ # begin
+ # raise StandardError
+ # rescue StandardError => err
+ # # ...
+ # end
+ #
+ class BanCatchThrow < RuboCop::Cop::Cop
+ MSG = "Do not use catch or throw unless a gem's API demands it."
+
+ def on_send(node)
+ receiver, method_name, _ = *node
+
+ return unless receiver.nil? && %i[catch throw].include?(method_name)
+
+ add_offense(node, location: :expression)
+ end
+ end
+ end
+end