From 4c51098b19a0fa2c285cfc6d1edfc76ecb7e643a Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Tue, 26 May 2020 19:49:05 +0900 Subject: [PATCH] [Fix #8035] Fix a false positive for `Lint/DeprecatedOpenSSLConstant` Fixes #8035. This PR fixes the following false positive for `Lint/DeprecatedOpenSSLConstant` when using double quoted string argument. ```console % cat example.rb OpenSSL::Cipher::AES256.new('cbc') OpenSSL::Cipher::AES256.new("cbc") % bundle exec rubocop --only Lint/DeprecatedOpenSSLConstant -a (snip) Offenses: example.rb:1:1: W: [Corrected] Lint/DeprecatedOpenSSLConstant: Use OpenSSL::Cipher.new('AES-256-cbc') instead of OpenSSL::Cipher::AES256.new('cbc'). OpenSSL::Cipher::AES256.new('cbc') ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ example.rb:2:1: W: [Corrected] Lint/DeprecatedOpenSSLConstant: Use OpenSSL::Cipher.new('AES-256-"cbc"') instead of OpenSSL::Cipher::AES256.new("cbc"). OpenSSL::Cipher::AES256.new("cbc") ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ % cat example.rb OpenSSL::Cipher.new('AES-256-cbc') OpenSSL::Cipher.new('AES-256-"cbc"') ``` #8035 has another different false positive, which resolves as another PR. --- CHANGELOG.md | 1 + lib/rubocop/cop/lint/deprecated_open_ssl_constant.rb | 6 +++++- .../cop/lint/deprecated_open_ssl_constant_spec.rb | 11 +++++++++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5bd33189f45..c60649b9979 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ * [#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][]) * [#8017](https://github.com/rubocop-hq/rubocop/pull/8017): Fix a false positive for `Lint/SuppressedException` when empty rescue with comment in `def`. ([@koic][]) * [#7990](https://github.com/rubocop-hq/rubocop/issues/7990): Fix resolving `inherit_gem` in remote configs. ([@CvX][]) +* [#8035](https://github.com/rubocop-hq/rubocop/issues/8035): Fix the following false positive for `Lint/DeprecatedOpenSSLConstant` when using double quoted string argument. ([@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 9d314ed2622..94d0eb3b22a 100644 --- a/lib/rubocop/cop/lint/deprecated_open_ssl_constant.rb +++ b/lib/rubocop/cop/lint/deprecated_open_ssl_constant.rb @@ -106,7 +106,11 @@ def algorithm_name(node) end def sanitize_arguments(arguments) - arguments.flat_map { |arg| arg.source.tr(":'", '').split('-') } + arguments.flat_map do |arg| + argument = arg.str_type? ? arg.value : arg.source + + argument.tr(":'", '').split('-') + end end def replacement_args(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 04836e55e3f..691e825d201 100644 --- a/spec/rubocop/cop/lint/deprecated_open_ssl_constant_spec.rb +++ b/spec/rubocop/cop/lint/deprecated_open_ssl_constant_spec.rb @@ -27,6 +27,17 @@ RUBY end + it 'registers an offense with cipher constant and double quoted string argument and corrects' do + expect_offense(<<~RUBY) + OpenSSL::Cipher::AES128.new("GCM") + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `OpenSSL::Cipher.new('AES-128-GCM')` instead of `OpenSSL::Cipher::AES128.new("GCM")`. + RUBY + + expect_correction(<<~RUBY) + OpenSSL::Cipher.new('AES-128-GCM') + RUBY + end + it 'registers an offense with AES + blocksize constant and mode argument and corrects' do expect_offense(<<~RUBY) OpenSSL::Cipher::AES128.new(:GCM)