/
suppressed_exception.rb
90 lines (85 loc) · 1.81 KB
/
suppressed_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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# frozen_string_literal: true
module RuboCop
module Cop
module Lint
# This cop checks for *rescue* blocks with no body.
#
# @example
#
# # bad
# def some_method
# do_something
# rescue
# end
#
# # bad
# begin
# do_something
# rescue
# end
#
# # good
# def some_method
# do_something
# rescue
# handle_exception
# end
#
# # good
# begin
# do_something
# rescue
# handle_exception
# end
#
# @example AllowComments: true (default)
#
# # good
# def some_method
# do_something
# rescue
# # do nothing
# end
#
# # good
# begin
# do_something
# rescue
# # do nothing
# end
#
# @example AllowComments: false
#
# # bad
# def some_method
# do_something
# rescue
# # do nothing
# end
#
# # bad
# begin
# do_something
# rescue
# # do nothing
# end
class SuppressedException < Cop
MSG = 'Do not suppress exceptions.'
def on_resbody(node)
return if node.body
return if cop_config['AllowComments'] && comment_between_rescue_and_end?(node)
add_offense(node)
end
private
def comment_between_rescue_and_end?(node)
end_line = nil
node.each_ancestor(:kwbegin) do |ancestor|
end_line = ancestor.loc.end.line
break
end
processed_source[node.first_line...end_line].any? { |line| comment_line?(line) }
end
end
end
end
end