lib/rubocop/cop/lint/handle_exceptions.rb in rubocop-0.69.0 vs lib/rubocop/cop/lint/handle_exceptions.rb in rubocop-0.70.0
- old
+ new
@@ -3,53 +3,92 @@
module RuboCop
module Cop
module Lint
# This cop checks for *rescue* blocks with no body.
#
- # @example
+ # @example AllowComments: false (default)
#
# # bad
+ # def some_method
+ # do_something
+ # rescue
+ # end
#
+ # # bad
# def some_method
# do_something
# rescue
# # do nothing
# end
#
- # @example
- #
# # bad
+ # begin
+ # do_something
+ # rescue
+ # end
#
+ # # bad
# begin
# do_something
# rescue
# # do nothing
# end
#
- # @example
+ # # good
+ # def some_method
+ # do_something
+ # rescue
+ # handle_exception
+ # end
#
# # good
+ # begin
+ # do_something
+ # rescue
+ # handle_exception
+ # end
#
+ # @example AllowComments: true
+ #
+ # # bad
# def some_method
# do_something
# rescue
- # handle_exception
# end
#
- # @example
+ # # bad
+ # begin
+ # do_something
+ # rescue
+ # end
#
# # good
+ # def some_method
+ # do_something
+ # rescue
+ # # do nothing but comment
+ # end
#
+ # # good
# begin
# do_something
# rescue
- # handle_exception
+ # # do nothing but comment
# end
class HandleExceptions < Cop
MSG = 'Do not suppress exceptions.'
def on_resbody(node)
- add_offense(node) unless node.body
+ return if node.body
+ return if cop_config['AllowComments'] && comment_lines?(node)
+
+ add_offense(node)
+ end
+
+ private
+
+ def comment_lines?(node)
+ processed_source[line_range(node)].any? { |line| comment_line?(line) }
end
end
end
end
end