2020-03-09 11:07:45 -04:00
|
|
|
# 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
|
|
|
|
#
|
2022-09-14 11:12:56 -04:00
|
|
|
class BanCatchThrow < RuboCop::Cop::Base
|
2020-03-09 11:07:45 -04:00
|
|
|
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)
|
|
|
|
|
2022-09-14 11:12:56 -04:00
|
|
|
add_offense(node)
|
2020-03-09 11:07:45 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|