Skip to content

Commit

Permalink
[Fix rubocop#3249] Do not error when parsing rescue nil in ShadowedEx…
Browse files Browse the repository at this point in the history
…ception
  • Loading branch information
rrosenblum authored and Neodelf committed Oct 15, 2016
1 parent a594290 commit b5391db
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* [#3248](https://github.com/bbatsov/rubocop/issues/3248): Support 'ruby-' prefix in `.ruby-version`. ([@tjwp][])
* [#3250](https://github.com/bbatsov/rubocop/pull/3250): Make regexp for cop names less restrictive in CommentConfig lines. ([@tjwp][])
* [#3261](https://github.com/bbatsov/rubocop/pull/3261): Prefer `TargetRubyVersion` to `.ruby-version`. ([@tjwp][])
* [#3249](https://github.com/bbatsov/rubocop/issues/3249): Account for `rescue nil` in `Style/ShadowedException`. ([@rrosenblum][])

## 0.41.1 (2016-06-26)

Expand Down
8 changes: 7 additions & 1 deletion lib/rubocop/cop/lint/shadowed_exception.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,13 @@ def evaluate_exceptions(rescue_group)

rescued_exceptions.each_with_object([]) do |exception, converted|
begin
converted << instance_eval(exception, __FILE__, __LINE__)
evaled_exception = instance_eval(exception, __FILE__, __LINE__)
# `rescue nil` is valid syntax in all versions of Ruby. In Ruby
# 1.9.3, it effectively disables the `rescue`. In versions
# after 1.9.3, a `TypeError` is thrown when the statement is
# rescued. In order to account for this, we convert `nil` to
# `NilClass`.
converted << (evaled_exception || NilClass)
rescue StandardError, ScriptError
next
end
Expand Down
55 changes: 55 additions & 0 deletions spec/rubocop/cop/lint/shadowed_exception_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,37 @@

expect(cop.offenses).to be_empty
end

it 'accepts rescuing nil' do
inspect_source(cop, ['begin',
' a',
'rescue nil',
' b',
'end'])

expect(cop.offenses).to be_empty
end

it 'accepts rescuing nil and another exception' do
inspect_source(cop, ['begin',
' a',
'rescue nil, Exception',
' b',
'end'])

expect(cop.offenses).to be_empty
end

it 'registers an offense when rescuing nil multiple exceptions of ' \
'different levels' do
inspect_source(cop, ['begin',
' a',
'rescue nil, StandardError, Exception',
' b',
'end'])

expect(cop.messages).to eq(['Do not shadow rescued Exceptions'])
end
end

context 'multiple rescues' do
Expand Down Expand Up @@ -257,5 +288,29 @@
expect(cop.offenses).to be_empty
end
end

it 'accepts rescuing nil before another exception' do
inspect_source(cop, ['begin',
' a',
'rescue nil',
' b',
'rescue',
' c',
'end'])

expect(cop.offenses).to be_empty
end

it 'accepts rescuing nil after another exception' do
inspect_source(cop, ['begin',
' a',
'rescue',
' b',
'rescue nil',
' c',
'end'])

expect(cop.offenses).to be_empty
end
end
end

0 comments on commit b5391db

Please sign in to comment.