Skip to content

Commit

Permalink
[Fix rubocop#8012] Fix incorrect autocorrect for `Lint/DeprecatedOpen…
Browse files Browse the repository at this point in the history
…SSLConstant`

Fixes rubocop#8012.

This PR fixes an incorrect autocorrect for `Lint/DeprecatedOpenSSLConstant`
when deprecated OpenSSL constant is used in a block.
  • Loading branch information
koic committed May 22, 2020
1 parent ebc19ad commit d84c0a2
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,7 @@
### Bug fixes

* [#8008](https://github.com/rubocop-hq/rubocop/issues/8008): Fix an error for `Lint/SuppressedException` when empty rescue block in `def`. ([@koic][])
* [#8012](https://github.com/rubocop-hq/rubocop/issues/8012): Fix an incorrect autocorrect for `Lint/DeprecatedOpenSSLConstant` when deprecated OpenSSL constant is used in a block. ([@koic][])

## 0.84.0 (2020-05-21)

Expand Down
4 changes: 1 addition & 3 deletions lib/rubocop/cop/lint/deprecated_open_ssl_constant.rb
Expand Up @@ -88,9 +88,7 @@ def message(node)
end

def correction_range(node)
begin_pos = node.loc.selector.column
end_pos = node.loc.expression.last_column
range_between(begin_pos, end_pos)
range_between(node.loc.dot.end_pos, node.loc.expression.end_pos)
end

def openssl_class(node)
Expand Down
17 changes: 17 additions & 0 deletions spec/rubocop/cop/lint/deprecated_open_ssl_constant_spec.rb
Expand Up @@ -88,6 +88,23 @@
RUBY
end

context 'when used in a block' do
it 'registers an offense when using ::Digest class methods on an algorithm constant and corrects' do
expect_offense(<<~RUBY)
do_something do
OpenSSL::Digest::SHA1.new
^^^^^^^^^^^^^^^^^^^^^^^^^ Use `OpenSSL::Digest.new('SHA1')` instead of `OpenSSL::Digest::SHA1.new`.
end
RUBY

expect_correction(<<~RUBY)
do_something do
OpenSSL::Digest.new('SHA1')
end
RUBY
end
end

it 'does not register an offense when building digest using an algorithm string' do
expect_no_offenses(<<~RUBY)
OpenSSL::Digest.new('SHA256')
Expand Down

0 comments on commit d84c0a2

Please sign in to comment.