From b3e38e63756353112d7f315ae2adb4a3e4593947 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Fri, 22 May 2020 22:19:14 +0900 Subject: [PATCH] [Fix #8012] Fix incorrect autocorrect for `Lint/DeprecatedOpenSSLConstant` Fixes #8012. This PR fixes an incorrect autocorrect for `Lint/DeprecatedOpenSSLConstant` when deprecated OpenSSL constant is used in a block. --- CHANGELOG.md | 1 + .../cop/lint/deprecated_open_ssl_constant.rb | 4 +--- .../lint/deprecated_open_ssl_constant_spec.rb | 17 +++++++++++++++++ 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 86585f83c4c..7e41d2b702e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/lib/rubocop/cop/lint/deprecated_open_ssl_constant.rb b/lib/rubocop/cop/lint/deprecated_open_ssl_constant.rb index 61fc1dd9101..9d314ed2622 100644 --- a/lib/rubocop/cop/lint/deprecated_open_ssl_constant.rb +++ b/lib/rubocop/cop/lint/deprecated_open_ssl_constant.rb @@ -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) diff --git a/spec/rubocop/cop/lint/deprecated_open_ssl_constant_spec.rb b/spec/rubocop/cop/lint/deprecated_open_ssl_constant_spec.rb index 111f12583ec..04836e55e3f 100644 --- a/spec/rubocop/cop/lint/deprecated_open_ssl_constant_spec.rb +++ b/spec/rubocop/cop/lint/deprecated_open_ssl_constant_spec.rb @@ -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')