class RuboCop::Cop::Lint::SuppressedException
do_something rescue nil
# bad
end
nil
rescue
do_something
begin
# bad
end
nil
rescue
do_something
def some_method
# bad
@example AllowNil: false
do_something rescue nil
# good
end
# do nothing
rescue
do_something
begin
# good
end
nil
rescue
do_something
def some_method
# good
@example AllowNil: true (default)
end
# do nothing
rescue
do_something
begin
# bad
end
# do nothing
rescue
do_something
def some_method
# bad
@example AllowComments: false
end
# do nothing
rescue
do_something
begin
# good
end
# do nothing
rescue
do_something
def some_method
# good
@example AllowComments: true (default)
end
handle_exception
rescue
do_something
begin
# good
end
handle_exception
rescue
do_something
def some_method
# good
end
rescue
do_something
begin
# bad
end
rescue
do_something
def some_method
# bad
@example
Checks for ‘rescue` blocks with no body.
def comment_between_rescue_and_end?(node)
def comment_between_rescue_and_end?(node) ancestor = node.each_ancestor(:kwbegin, :def, :defs, :block, :numblock).first return unless ancestor end_line = ancestor.loc.end.line processed_source[node.first_line...end_line].any? { |line| comment_line?(line) } end
def nil_body?(node)
def nil_body?(node) node.body&.nil_type? end
def on_resbody(node)
def on_resbody(node) return if node.body && !nil_body?(node) return if cop_config['AllowComments'] && comment_between_rescue_and_end?(node) return if cop_config['AllowNil'] && nil_body?(node) add_offense(node) end