Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ban_catch_throw.rb « cop « rubocop - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3f215eb31407498f36996dbb18230868ddec0eb3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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::Base
      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)
      end
    end
  end
end