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

Follow-up clean-up of Ruby < 2.6 #10805

Closed
wants to merge 6 commits into from
Closed
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
5 changes: 0 additions & 5 deletions lib/rubocop/cop/lint/redundant_require_statement.rb
Expand Up @@ -12,8 +12,6 @@ module Lint
# ruby 2.2.8p477 (2017-09-14 revision 59906) [x86_64-darwin13]
# ["enumerator.so", "rational.so", "complex.so", "thread.rb"]
#
# This cop targets Ruby 2.2 or higher containing these 4 features.
#
# @example
# # bad
# require 'unloaded_feature'
Expand All @@ -24,9 +22,6 @@ module Lint
class RedundantRequireStatement < Base
include RangeHelp
extend AutoCorrector
extend TargetRubyVersion

minimum_target_ruby_version 2.2

MSG = 'Remove unnecessary `require` statement.'
RESTRICT_ON_SEND = %i[require].freeze
Expand Down
3 changes: 0 additions & 3 deletions lib/rubocop/cop/lint/safe_navigation_chain.rb
Expand Up @@ -25,9 +25,6 @@ module Lint
# x&.foo || bar
class SafeNavigationChain < Base
include NilMethods
extend TargetRubyVersion

minimum_target_ruby_version 2.3

MSG = 'Do not chain ordinary method call after safe navigation operator.'

Expand Down
2 changes: 0 additions & 2 deletions lib/rubocop/cop/lint/unified_integer.rb
Expand Up @@ -33,8 +33,6 @@ def on_const(node)
return unless klass

add_offense(node, message: format(MSG, klass: klass)) do |corrector|
next if target_ruby_version <= 2.3

corrector.replace(node.loc.name, 'Integer')
end
end
Expand Down
3 changes: 0 additions & 3 deletions lib/rubocop/cop/style/dir.rb
Expand Up @@ -18,9 +18,6 @@ module Style
# path = __dir__
class Dir < Base
extend AutoCorrector
extend TargetRubyVersion

minimum_target_ruby_version 2.0

MSG = "Use `__dir__` to get an absolute path to the current file's directory."
RESTRICT_ON_SEND = %i[expand_path dirname].freeze
Expand Down
3 changes: 0 additions & 3 deletions lib/rubocop/cop/style/frozen_string_literal_comment.rb
Expand Up @@ -86,9 +86,6 @@ class FrozenStringLiteralComment < Base
include FrozenStringLiteral
include RangeHelp
extend AutoCorrector
extend TargetRubyVersion

minimum_target_ruby_version 2.3

MSG_MISSING_TRUE = 'Missing magic comment `# frozen_string_literal: true`.'
MSG_MISSING = 'Missing frozen string literal comment.'
Expand Down
5 changes: 0 additions & 5 deletions lib/rubocop/cop/style/hash_transform_keys.rb
Expand Up @@ -7,8 +7,6 @@ module Style
# `_.map {...}.to_h`, and `Hash[_.map {...}]` that are actually just
# transforming the keys of a hash, and tries to use a simpler & faster
# call to `transform_keys` instead.
# It should only be enabled on Ruby version 2.5 or newer.
# (`transform_keys` was added in Ruby 2.5.)
#
# @safety
# This cop is unsafe, as it can produce false positives if we are
Expand All @@ -28,9 +26,6 @@ module Style
class HashTransformKeys < Base
include HashTransformMethod
extend AutoCorrector
extend TargetRubyVersion

minimum_target_ruby_version 2.5

# @!method on_bad_each_with_object(node)
def_node_matcher :on_bad_each_with_object, <<~PATTERN
Expand Down
3 changes: 0 additions & 3 deletions lib/rubocop/cop/style/hash_transform_values.rb
Expand Up @@ -26,9 +26,6 @@ module Style
class HashTransformValues < Base
include HashTransformMethod
extend AutoCorrector
extend TargetRubyVersion

minimum_target_ruby_version 2.4

# @!method on_bad_each_with_object(node)
def_node_matcher :on_bad_each_with_object, <<~PATTERN
Expand Down
10 changes: 1 addition & 9 deletions lib/rubocop/cop/style/numeric_predicate.rb
Expand Up @@ -82,7 +82,7 @@ def check(node)
predicate(node)
end

return unless numeric && operator && replacement_supported?(operator)
return unless numeric && operator

[numeric, replacement(numeric, operator)]
end
Expand All @@ -107,14 +107,6 @@ def require_parentheses?(node)
node.send_type? && node.binary_operation? && !node.parenthesized?
end

def replacement_supported?(operator)
if %i[> <].include?(operator)
target_ruby_version >= 2.3
else
true
end
end

def invert
lambda do |comparison, numeric|
comparison = { :> => :<, :< => :> }[comparison] || comparison
Expand Down
1 change: 0 additions & 1 deletion lib/rubocop/cop/style/redundant_begin.rb
Expand Up @@ -81,7 +81,6 @@ def on_def(node)
alias on_defs on_def

def on_block(node)
return if target_ruby_version < 2.5
return if node.send_node.lambda_literal?
return if node.braces?
return unless node.body&.kwbegin_type?
Expand Down
3 changes: 0 additions & 3 deletions lib/rubocop/cop/style/safe_navigation.rb
Expand Up @@ -80,14 +80,11 @@ class SafeNavigation < Base
include NilMethods
include RangeHelp
extend AutoCorrector
extend TargetRubyVersion

MSG = 'Use safe navigation (`&.`) instead of checking if an object ' \
'exists before calling the method.'
LOGIC_JUMP_KEYWORDS = %i[break fail next raise return throw yield].freeze

minimum_target_ruby_version 2.3

# if format: (if checked_variable body nil)
# unless format: (if checked_variable nil body)
# @!method modifier_if_safe_navigation_candidate(node)
Expand Down
3 changes: 0 additions & 3 deletions lib/rubocop/cop/style/symbol_array.rb
Expand Up @@ -34,9 +34,6 @@ class SymbolArray < Base
include ConfigurableEnforcedStyle
include PercentArray
extend AutoCorrector
extend TargetRubyVersion

minimum_target_ruby_version 2.0

PERCENT_MSG = 'Use `%i` or `%I` for an array of symbols.'
ARRAY_MSG = 'Use %<prefer>s for an array of symbols.'
Expand Down
3 changes: 0 additions & 3 deletions lib/rubocop/cop/style/unpack_first.rb
Expand Up @@ -19,9 +19,6 @@ module Style
#
class UnpackFirst < Base
extend AutoCorrector
extend TargetRubyVersion

minimum_target_ruby_version 2.4

MSG = 'Use `%<receiver>s.unpack1(%<format>s)` instead of ' \
'`%<receiver>s.unpack(%<format>s)%<method>s`.'
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/util.rb
Expand Up @@ -159,7 +159,7 @@ def to_supported_styles(enforced_style)
private

def compatible_external_encoding_for?(src)
src = src.dup if RUBY_VERSION < '2.3' || RUBY_ENGINE == 'jruby'
src = src.dup if RUBY_ENGINE == 'jruby'
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm uncertain if this is still needed on JRuby > 9.2.

src.force_encoding(Encoding.default_external).valid_encoding?
end
end
Expand Down
24 changes: 0 additions & 24 deletions lib/rubocop/rspec/shared_contexts.rb
Expand Up @@ -116,30 +116,6 @@ def source_range(range, buffer: source_buffer)
end
end

RSpec.shared_context 'ruby 2.0', :ruby20 do
let(:ruby_version) { 2.0 }
end

RSpec.shared_context 'ruby 2.1', :ruby21 do
let(:ruby_version) { 2.1 }
end

RSpec.shared_context 'ruby 2.2', :ruby22 do
let(:ruby_version) { 2.2 }
end

RSpec.shared_context 'ruby 2.3', :ruby23 do
let(:ruby_version) { 2.3 }
end

RSpec.shared_context 'ruby 2.4', :ruby24 do
let(:ruby_version) { 2.4 }
end

RSpec.shared_context 'ruby 2.5', :ruby25 do
let(:ruby_version) { 2.5 }
end

RSpec.shared_context 'ruby 2.6', :ruby26 do
let(:ruby_version) { 2.6 }
end
Expand Down
32 changes: 15 additions & 17 deletions spec/rubocop/cop/gemspec/required_ruby_version_spec.rb
Expand Up @@ -72,24 +72,22 @@
end
end

context 'target ruby version > 2.6', :ruby26 do
it 'registers an offense when `required_ruby_version` is specified with >= and is higher than `TargetRubyVersion`' do
expect_offense(<<~RUBY, '/path/to/bar.gemspec')
Gem::Specification.new do |spec|
spec.required_ruby_version = '>= 2.7.0'
^^^^^^^^^^ `required_ruby_version` and `TargetRubyVersion` (2.6, which may be specified in .rubocop.yml) should be equal.
end
RUBY
end
it 'registers an offense when `required_ruby_version` is specified with >= and is higher than `TargetRubyVersion`' do
expect_offense(<<~RUBY, '/path/to/bar.gemspec')
Gem::Specification.new do |spec|
spec.required_ruby_version = '>= 2.7.0'
^^^^^^^^^^ `required_ruby_version` and `TargetRubyVersion` (2.6, which may be specified in .rubocop.yml) should be equal.
end
RUBY
end

it 'registers an offense when `required_ruby_version` is specified with ~> and is higher than `TargetRubyVersion`' do
expect_offense(<<~RUBY, '/path/to/bar.gemspec')
Gem::Specification.new do |spec|
spec.required_ruby_version = '~> 2.7.0'
^^^^^^^^^^ `required_ruby_version` and `TargetRubyVersion` (2.6, which may be specified in .rubocop.yml) should be equal.
end
RUBY
end
it 'registers an offense when `required_ruby_version` is specified with ~> and is higher than `TargetRubyVersion`' do
expect_offense(<<~RUBY, '/path/to/bar.gemspec')
Gem::Specification.new do |spec|
spec.required_ruby_version = '~> 2.7.0'
^^^^^^^^^^ `required_ruby_version` and `TargetRubyVersion` (2.6, which may be specified in .rubocop.yml) should be equal.
end
RUBY
end

# rubocop:disable RSpec/RepeatedExampleGroupDescription
Expand Down
2 changes: 1 addition & 1 deletion spec/rubocop/cop/layout/space_around_operators_spec.rb
Expand Up @@ -12,7 +12,7 @@
}
)
end
let(:target_ruby_version) { 2.5 }
let(:target_ruby_version) { 2.6 }
let(:hash_style) { 'key' }
let(:allow_for_alignment) { true }
let(:exponent_operator_style) { nil }
Expand Down
2 changes: 1 addition & 1 deletion spec/rubocop/cop/lint/ambiguous_range_spec.rb
Expand Up @@ -86,7 +86,7 @@
RUBY
end

it 'can handle an endless range', :ruby26 do
it 'can handle an endless range' do
expect_offense(<<~RUBY)
x || 1#{operator}
^^^^^^ Wrap complex range boundaries with parentheses to avoid ambiguity.
Expand Down
26 changes: 8 additions & 18 deletions spec/rubocop/cop/lint/deprecated_constants_spec.rb
Expand Up @@ -51,25 +51,15 @@
RUBY
end

context 'Ruby <= 2.5', :ruby25 do
it 'does not register an offense when using `Net::HTTPServerException`' do
expect_no_offenses(<<~RUBY)
Net::HTTPServerException
RUBY
end
end

context 'Ruby >= 2.6', :ruby26 do
it 'registers and corrects an offense when using `Net::HTTPServerException`' do
expect_offense(<<~RUBY)
Net::HTTPServerException
^^^^^^^^^^^^^^^^^^^^^^^^ Use `Net::HTTPClientException` instead of `Net::HTTPServerException`, deprecated since Ruby 2.6.
RUBY
it 'registers and corrects an offense when using `Net::HTTPServerException`' do
expect_offense(<<~RUBY)
Net::HTTPServerException
^^^^^^^^^^^^^^^^^^^^^^^^ Use `Net::HTTPClientException` instead of `Net::HTTPServerException`, deprecated since Ruby 2.6.
RUBY

expect_correction(<<~RUBY)
Net::HTTPClientException
RUBY
end
expect_correction(<<~RUBY)
Net::HTTPClientException
RUBY
end

context 'Ruby <= 2.7', :ruby27 do
Expand Down
4 changes: 1 addition & 3 deletions spec/rubocop/cop/lint/duplicate_hash_key_spec.rb
Expand Up @@ -63,9 +63,7 @@
it_behaves_like 'duplicated literal key', 'false'
it_behaves_like 'duplicated literal key', 'nil'
it_behaves_like 'duplicated literal key', "'str'"
context 'target ruby version >= 2.6', :ruby26 do
it_behaves_like 'duplicated literal key', '(42..)'
end
it_behaves_like 'duplicated literal key', '(42..)'

shared_examples 'duplicated non literal key' do |key|
it "does not register an offense for duplicated `#{key}` hash keys" do
Expand Down