Skip to content

Commit

Permalink
[Fix #6675] Avoid printing deprecation warnings about constants
Browse files Browse the repository at this point in the history
  • Loading branch information
elmasantos authored and bbatsov committed Mar 14, 2019
1 parent a63d1cc commit 76a1413
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
* [#6806](https://github.com/rubocop-hq/rubocop/pull/6806): Remove `powerpack` dependency. ([@dduugg][])
* [#6810](https://github.com/rubocop-hq/rubocop/pull/6810): Exclude gemspec file by default for `Metrics/BlockLength` cop. ([@koic][])
* [#6813](https://github.com/rubocop-hq/rubocop/pull/6813): Allow unicode/display_width dependency version 1.5.0. ([@tagliala][])
* [#6675](https://github.com/rubocop-hq/rubocop/issues/6675): Avoid printing deprecation warnings about constants. ([@elmasantos][])

## 0.65.0 (2019-02-19)

Expand Down Expand Up @@ -3855,3 +3856,4 @@
[@enkessler]: https://github.com/enkessler
[@tagliala]: https://github.com/tagliala
[@unasuke]: https://github.com/unasuke
[@elmasantos]: https://github.com/elmasantos
15 changes: 14 additions & 1 deletion lib/rubocop/cop/lint/shadowed_exception.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,25 @@ def system_call_err?(error)
error && error.ancestors[1] == SystemCallError
end

def silence_warnings
# Replaces Kernel::silence_warnings since it hides any warnings,
# including the RuboCop ones
old_verbose = $VERBOSE
$VERBOSE = nil
yield
ensure
$VERBOSE = old_verbose
end

def evaluate_exceptions(rescue_group)
if rescue_group
rescued_exceptions = rescued_exceptions(rescue_group)
rescued_exceptions.each_with_object([]) do |exception, converted|
begin
converted << Kernel.const_get(exception)
silence_warnings do
# Avoid printing deprecation warnings about constants
converted << Kernel.const_get(exception)
end
rescue NameError
converted << nil
end
Expand Down
12 changes: 12 additions & 0 deletions spec/rubocop/cop/lint/shadowed_exception_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@
RUBY
end

it 'rescue a exception without causing constant name deprecation warning' do
expect do
inspect_source(<<-RUBY.strip_indent)
def foo
something
rescue TimeoutError
handle_exception
end
RUBY
end.not_to output(/.*TimeoutError is deprecated/).to_stderr
end

it 'accepts rescuing a single custom exception' do
expect_no_offenses(<<-RUBY.strip_indent)
begin
Expand Down

0 comments on commit 76a1413

Please sign in to comment.