Skip to content

Commit

Permalink
[Fix rubocop#8035] Fix a false positive for `Lint/DeprecatedOpenSSLCo…
Browse files Browse the repository at this point in the history
…nstant`

Fixes rubocop#8035.

This PR fixes a false positive for `Lint/DeprecatedOpenSSLConstant`
when argument is a variable, method, or constant.

```console
% cat example.rb
MODE = 'cbc'
OpenSSL::Cipher::AES256.new(MODE)

% bundle exec rubocop --only Lint/DeprecatedOpenSSLConstant -a
(snip)

Offenses:

example.rb:2:1: W: [Corrected] Lint/DeprecatedOpenSSLConstant: Use
OpenSSL::Cipher.new('AES-256-MODE') instead of OpenSSL::Cipher::AES256.new(MODE).
OpenSSL::Cipher::AES256.new(MODE)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

1 file inspected, 1 offense detected, 1 offense corrected

% cat example.rb
MODE = 'cbc'
OpenSSL::Cipher.new('AES-256-MODE')
```

RuboCop does not have a tracing feature a value of variable, (method,) and constant yet.
This PR accepts cases where these values are used to prevent auto-correction mistakes.

On the other hand, this change produces false negatives for the deprecated APIs.
If it is difficult to accept false negatives, I will update this PR to
warn against these cases and auto-correction will not performed.
  • Loading branch information
koic committed May 26, 2020
1 parent c7b9707 commit 85dceda
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -9,6 +9,7 @@
* [#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 a false positive for `Lint/DeprecatedOpenSSLConstant` when using double quoted string argument. ([@koic][])
* [#8035](https://github.com/rubocop-hq/rubocop/issues/8035): Fix a false positive for `Lint/DeprecatedOpenSSLConstant` when argument is a variable, method, or consntant. ([@koic][])

## 0.84.0 (2020-05-21)

Expand Down
2 changes: 2 additions & 0 deletions lib/rubocop/cop/lint/deprecated_open_ssl_constant.rb
Expand Up @@ -53,6 +53,8 @@ class DeprecatedOpenSSLConstant < Cop
PATTERN

def on_send(node)
return if node.arguments.any? { |arg| arg.variable? || arg.send_type? || arg.const_type? }

add_offense(node) if algorithm_const(node)
end

Expand Down
19 changes: 19 additions & 0 deletions spec/rubocop/cop/lint/deprecated_open_ssl_constant_spec.rb
Expand Up @@ -66,6 +66,25 @@
RUBY
end

it 'does not register an offense with cipher constant and argument is a variable' do
expect_no_offenses(<<~RUBY)
mode = "cbc"
OpenSSL::Cipher::AES128.new(mode)
RUBY
end

it 'does not register an offense with cipher constant and send argument is a method' do
expect_no_offenses(<<~RUBY)
OpenSSL::Cipher::AES128.new(do_something)
RUBY
end

it 'does not register an offense with cipher constant and argument is a constant' do
expect_no_offenses(<<~RUBY)
OpenSSL::Cipher::AES128.new(MODE)
RUBY
end

it 'registers an offense when building an instance using an digest constant and corrects' do
expect_offense(<<~RUBY)
OpenSSL::Digest::SHA256.new
Expand Down

0 comments on commit 85dceda

Please sign in to comment.