Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Lint/DeprecatedClassMethods aware of ENV.clone and ENV.dup #10824

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1 @@
* [#10824](https://github.com/rubocop/rubocop/pull/10824): Make `Lint/DeprecatedClassMethods` aware of `ENV.clone` and `ENV.dup`. ([@koic][])
14 changes: 10 additions & 4 deletions lib/rubocop/cop/lint/deprecated_class_methods.rb
Expand Up @@ -8,22 +8,22 @@ module Lint
# @example
#
# # bad
#
# File.exists?(some_path)
# Dir.exists?(some_path)
# iterator?
# ENV.freeze # Calling `Env.freeze` raises `TypeError` since Ruby 2.7.
# ENV.clone
# ENV.dup # Calling `Env.dup` raises `TypeError` since Ruby 3.1.
# Socket.gethostbyname(host)
# Socket.gethostbyaddr(host)
#
# @example
#
# # good
#
# File.exist?(some_path)
# Dir.exist?(some_path)
# block_given?
# ENV # `ENV.freeze` cannot prohibit changes to environment variables.
# ENV.to_h
# ENV.to_h # `ENV.dup` cannot dup `ENV`, use `ENV.to_h` to get a copy of `ENV` as a hash.
# Addrinfo.getaddrinfo(nodename, service)
# Addrinfo.tcp(host, port).getnameinfo
class DeprecatedClassMethods < Base
Expand Down Expand Up @@ -111,6 +111,12 @@ def instance_method?
DeprecatedClassMethod.new(:freeze, class_constant: :ENV) =>
Replacement.new(nil, class_constant: :ENV),

DeprecatedClassMethod.new(:clone, class_constant: :ENV) =>
Replacement.new(:to_h, class_constant: :ENV),

DeprecatedClassMethod.new(:dup, class_constant: :ENV) =>
Replacement.new(:to_h, class_constant: :ENV),

DeprecatedClassMethod.new(:gethostbyaddr, class_constant: :Socket, correctable: false) =>
Replacement.new(:getnameinfo, class_constant: :Addrinfo, instance_method: true),

Expand Down
34 changes: 34 additions & 0 deletions spec/rubocop/cop/lint/deprecated_class_methods_spec.rb
Expand Up @@ -99,6 +99,40 @@
end
end

context 'when using `ENV.clone`' do
it 'registers an offense' do
expect_offense(<<~RUBY)
ENV.clone
^^^^^ `ENV.clone` is deprecated in favor of `ENV.to_h`.
RUBY

expect_correction(<<~RUBY)
ENV.to_h
RUBY
end

it 'does not register an offense for method calls to `ENV` other than `clone`' do
expect_no_offenses('ENV.values')
end
end

context 'when using `ENV.dup`' do
it 'registers an offense' do
expect_offense(<<~RUBY)
ENV.dup
^^^ `ENV.dup` is deprecated in favor of `ENV.to_h`.
RUBY

expect_correction(<<~RUBY)
ENV.to_h
RUBY
end

it 'does not register an offense for method calls to `ENV` other than `dup`' do
expect_no_offenses('ENV.values')
end
end

context 'prefer `Addrinfo#getnameinfo` over `Socket.gethostbyaddr`' do
it 'registers an offense for Socket.gethostbyaddr' do
expect_offense(<<~RUBY)
Expand Down