/
raise_exception.rb
39 lines (33 loc) · 1003 Bytes
/
raise_exception.rb
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
35
36
37
38
39
# frozen_string_literal: true
module RuboCop
module Cop
module Lint
# This cop checks for `raise` or `fail` statements which are
# raising `Exception` class.
#
# @example
# # bad
# raise Exception, 'Error message here'
#
# # good
# raise StandardError, 'Error message here'
class RaiseException < Cop
MSG = 'Use `StandardError` over `Exception`.'
def_node_matcher :exception?, <<~PATTERN
(send nil? ${:raise :fail} (const {cbase nil?} :Exception) ... )
PATTERN
def_node_matcher :exception_new_with_message?, <<~PATTERN
(send nil? ${:raise :fail}
(send (const {cbase nil?} :Exception) :new ... ))
PATTERN
def on_send(node)
add_offense(node) if raise_exception?(node)
end
private
def raise_exception?(node)
exception?(node) || exception_new_with_message?(node)
end
end
end
end
end