Skip to content

Commit

Permalink
Merge pull request #12086 from koic/fix_error_for_lint_suppressed_exc…
Browse files Browse the repository at this point in the history
…eption

[Fix #12085] Fix an error for `Lint/SuppressedException`
  • Loading branch information
koic authored Aug 1, 2023
2 parents d8646b4 + 43879ce commit fe172e4
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
1 change: 1 addition & 0 deletions changelog/fix_error_for_lint_suppressed_exception.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#12085](https://github.com/rubocop/rubocop/issues/12085): Fix an error for `Lint/SuppressedException` when `AllowNil: true` is set and endless method definition is used. ([@koic][])
2 changes: 1 addition & 1 deletion lib/rubocop/cop/lint/suppressed_exception.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def comment_between_rescue_and_end?(node)
ancestor = node.each_ancestor(:kwbegin, :def, :defs, :block, :numblock).first
return false unless ancestor

end_line = ancestor.loc.end.line
end_line = ancestor.loc.end&.line || ancestor.loc.last_line
processed_source[node.first_line...end_line].any? { |line| comment_line?(line) }
end

Expand Down
28 changes: 27 additions & 1 deletion spec/rubocop/cop/lint/suppressed_exception_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ def self.foo
end

context 'with AllowComments set to true' do
let(:cop_config) { { 'AllowComments' => true } }
let(:cop_config) { { 'AllowComments' => true, 'AllowNil' => allow_nil } }
let(:allow_nil) { true }

it 'does not register an offense for empty rescue with comment' do
expect_no_offenses(<<~RUBY)
Expand Down Expand Up @@ -244,6 +245,31 @@ def self.foo
end
end

context 'with AllowNil set to true' do
let(:allow_nil) { true }

context 'when using endless method definition', :ruby30 do
it 'does not register an offense for inline nil rescue' do
expect_no_offenses(<<~RUBY)
def some_method = other_method(42) rescue nil
RUBY
end
end
end

context 'with AllowNil set to false' do
let(:allow_nil) { false }

context 'when using endless method definition', :ruby30 do
it 'registers an offense for inline nil rescue' do
expect_offense(<<~RUBY)
def some_method = other_method(42) rescue nil
^^^^^^^^^^ Do not suppress exceptions.
RUBY
end
end
end

it 'registers an offense for empty rescue on single line with a comment after it' do
expect_offense(<<~RUBY)
RSpec.describe Dummy do
Expand Down

0 comments on commit fe172e4

Please sign in to comment.