From ec4a6f25b531219ca11aac9dc72b127444d200fd Mon Sep 17 00:00:00 2001 From: MarttiCheng Date: Tue, 6 Apr 2021 18:53:27 +0900 Subject: [PATCH] Drop Ruby 2.4 support Follow https://github.com/rubocop/rubocop/pull/9648. --- .circleci/config.yml | 3 - .rubocop.yml | 2 +- CHANGELOG.md | 1 + rubocop-performance.gemspec | 2 +- .../cop/performance/delete_prefix_spec.rb | 302 ++++++++---------- .../cop/performance/delete_suffix_spec.rb | 302 ++++++++---------- ...edundant_equality_comparison_block_spec.rb | 169 +++++----- tasks/cops_documentation.rake | 20 +- 8 files changed, 365 insertions(+), 436 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index df78d9f..363333f 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -38,9 +38,6 @@ workflows: build: jobs: - documentation-checks - - rake_default: - name: Ruby 2.4 - image: circleci/ruby:2.4 - rake_default: name: Ruby 2.5 image: circleci/ruby:2.5 diff --git a/.rubocop.yml b/.rubocop.yml index b0e6b3b..c4ff10f 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -12,7 +12,7 @@ AllCops: - 'vendor/**/*' - 'spec/fixtures/**/*' - 'tmp/**/*' - TargetRubyVersion: 2.4 + TargetRubyVersion: 2.5 SuggestExtensions: false InternalAffairs/NodeMatcherDirective: diff --git a/CHANGELOG.md b/CHANGELOG.md index 524175e..b5bfc20 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ### Changes * [#228](https://github.com/rubocop/rubocop-performance/pull/228): Mark `Performance/RedundantMerge` as unsafe. ([@dvandersluis][]) +* [#232](https://github.com/rubocop/rubocop-performance/pull/232): Drop Ruby 2.4 support. ([@koic][]) ## 1.10.2 (2021-03-23) diff --git a/rubocop-performance.gemspec b/rubocop-performance.gemspec index 74872dc..790e8b4 100644 --- a/rubocop-performance.gemspec +++ b/rubocop-performance.gemspec @@ -7,7 +7,7 @@ Gem::Specification.new do |s| s.name = 'rubocop-performance' s.version = RuboCop::Performance::Version::STRING s.platform = Gem::Platform::RUBY - s.required_ruby_version = '>= 2.4.0' + s.required_ruby_version = '>= 2.5.0' s.authors = ['Bozhidar Batsov', 'Jonas Arvidsson', 'Yuji Nakayama'] s.description = <<~DESCRIPTION A collection of RuboCop cops to check for performance optimizations diff --git a/spec/rubocop/cop/performance/delete_prefix_spec.rb b/spec/rubocop/cop/performance/delete_prefix_spec.rb index b1126af..dc7a153 100644 --- a/spec/rubocop/cop/performance/delete_prefix_spec.rb +++ b/spec/rubocop/cop/performance/delete_prefix_spec.rb @@ -4,37 +4,87 @@ let(:cop_config) { { 'SafeMultiline' => safe_multiline } } let(:safe_multiline) { true } - context 'TargetRubyVersion <= 2.4', :ruby24 do - it "does not register an offense when using `gsub(/\Aprefix/, '')`" do - expect_no_offenses(<<~RUBY) + context 'when using `\A` as starting pattern' do + it "registers an offense and corrects when `gsub(/\Aprefix/, '')`" do + expect_offense(<<~RUBY) str.gsub(/\\Aprefix/, '') + ^^^^ Use `delete_prefix` instead of `gsub`. + RUBY + + expect_correction(<<~RUBY) + str.delete_prefix('prefix') RUBY end - it "does not register an offense when using `gsub!(/\Aprefix/, '')`" do - expect_no_offenses(<<~RUBY) + it "registers an offense and corrects when `gsub!(/\Aprefix/, '')`" do + expect_offense(<<~RUBY) str.gsub!(/\\Aprefix/, '') + ^^^^^ Use `delete_prefix!` instead of `gsub!`. + RUBY + + expect_correction(<<~RUBY) + str.delete_prefix!('prefix') RUBY end - it "does not register an offense when using `sub(/\Aprefix/, '')`" do - expect_no_offenses(<<~RUBY) + it "registers an offense and corrects when `sub(/\Aprefix/, '')`" do + expect_offense(<<~RUBY) str.sub(/\\Aprefix/, '') + ^^^ Use `delete_prefix` instead of `sub`. + RUBY + + expect_correction(<<~RUBY) + str.delete_prefix('prefix') RUBY end - it "does not register an offense when using `sub!(/\Aprefix/, '')`" do - expect_no_offenses(<<~RUBY) + it "registers an offense and corrects when `sub!(/\Aprefix/, '')`" do + expect_offense(<<~RUBY) str.sub!(/\\Aprefix/, '') + ^^^^ Use `delete_prefix!` instead of `sub!`. + RUBY + + expect_correction(<<~RUBY) + str.delete_prefix!('prefix') RUBY end end - context 'TargetRubyVersion >= 2.5', :ruby25 do - context 'when using `\A` as starting pattern' do - it "registers an offense and corrects when `gsub(/\Aprefix/, '')`" do + context 'when using `^` as starting pattern' do + context 'when `SafeMultiline: true`' do + let(:safe_multiline) { true } + + it 'does not register an offense and corrects when using `gsub`' do + expect_no_offenses(<<~RUBY) + str.gsub(/^prefix/, '') + RUBY + end + + it 'does not register an offense and corrects when using `gsub!`' do + expect_no_offenses(<<~RUBY) + str.gsub!(/^prefix/, '') + RUBY + end + + it 'does not register an offense and corrects when using `sub`' do + expect_no_offenses(<<~RUBY) + str.sub(/^prefix/, '') + RUBY + end + + it 'does not register an offense and corrects when using `sub!`' do + expect_no_offenses(<<~RUBY) + str.sub!(/^prefix/, '') + RUBY + end + end + + context 'when `SafeMultiline: false`' do + let(:safe_multiline) { false } + + it 'registers an offense and corrects when using `gsub`' do expect_offense(<<~RUBY) - str.gsub(/\\Aprefix/, '') + str.gsub(/^prefix/, '') ^^^^ Use `delete_prefix` instead of `gsub`. RUBY @@ -43,9 +93,9 @@ RUBY end - it "registers an offense and corrects when `gsub!(/\Aprefix/, '')`" do + it 'registers an offense and corrects when using `gsub!`' do expect_offense(<<~RUBY) - str.gsub!(/\\Aprefix/, '') + str.gsub!(/^prefix/, '') ^^^^^ Use `delete_prefix!` instead of `gsub!`. RUBY @@ -54,9 +104,9 @@ RUBY end - it "registers an offense and corrects when `sub(/\Aprefix/, '')`" do + it 'registers an offense and corrects when using `sub`' do expect_offense(<<~RUBY) - str.sub(/\\Aprefix/, '') + str.sub(/^prefix/, '') ^^^ Use `delete_prefix` instead of `sub`. RUBY @@ -65,9 +115,9 @@ RUBY end - it "registers an offense and corrects when `sub!(/\Aprefix/, '')`" do + it 'registers an offense and corrects when using `sub!`' do expect_offense(<<~RUBY) - str.sub!(/\\Aprefix/, '') + str.sub!(/^prefix/, '') ^^^^ Use `delete_prefix!` instead of `sub!`. RUBY @@ -76,173 +126,95 @@ RUBY end end + end - context 'when using `^` as starting pattern' do - context 'when `SafeMultiline: true`' do - let(:safe_multiline) { true } - - it 'does not register an offense and corrects when using `gsub`' do - expect_no_offenses(<<~RUBY) - str.gsub(/^prefix/, '') - RUBY - end - - it 'does not register an offense and corrects when using `gsub!`' do - expect_no_offenses(<<~RUBY) - str.gsub!(/^prefix/, '') - RUBY - end - - it 'does not register an offense and corrects when using `sub`' do - expect_no_offenses(<<~RUBY) - str.sub(/^prefix/, '') - RUBY - end - - it 'does not register an offense and corrects when using `sub!`' do - expect_no_offenses(<<~RUBY) - str.sub!(/^prefix/, '') - RUBY - end - end - - context 'when `SafeMultiline: false`' do - let(:safe_multiline) { false } - - it 'registers an offense and corrects when using `gsub`' do - expect_offense(<<~RUBY) - str.gsub(/^prefix/, '') - ^^^^ Use `delete_prefix` instead of `gsub`. - RUBY - - expect_correction(<<~RUBY) - str.delete_prefix('prefix') - RUBY - end - - it 'registers an offense and corrects when using `gsub!`' do - expect_offense(<<~RUBY) - str.gsub!(/^prefix/, '') - ^^^^^ Use `delete_prefix!` instead of `gsub!`. - RUBY - - expect_correction(<<~RUBY) - str.delete_prefix!('prefix') - RUBY - end - - it 'registers an offense and corrects when using `sub`' do - expect_offense(<<~RUBY) - str.sub(/^prefix/, '') - ^^^ Use `delete_prefix` instead of `sub`. - RUBY - - expect_correction(<<~RUBY) - str.delete_prefix('prefix') - RUBY - end - - it 'registers an offense and corrects when using `sub!`' do - expect_offense(<<~RUBY) - str.sub!(/^prefix/, '') - ^^^^ Use `delete_prefix!` instead of `sub!`. - RUBY - - expect_correction(<<~RUBY) - str.delete_prefix!('prefix') - RUBY - end - end + context 'when using non-starting pattern' do + it 'does not register an offense when using `gsub`' do + expect_no_offenses(<<~RUBY) + str.gsub(/pattern/, '') + RUBY end - context 'when using non-starting pattern' do - it 'does not register an offense when using `gsub`' do - expect_no_offenses(<<~RUBY) - str.gsub(/pattern/, '') - RUBY - end - - it 'does not register an offense when using `gsub!`' do - expect_no_offenses(<<~RUBY) - str.gsub!(/pattern/, '') - RUBY - end - - it 'does not register an offense when using `sub`' do - expect_no_offenses(<<~RUBY) - str.sub(/pattern/, '') - RUBY - end - - it 'does not register an offense when using `sub!`' do - expect_no_offenses(<<~RUBY) - str.sub!(/pattern/, '') - RUBY - end + it 'does not register an offense when using `gsub!`' do + expect_no_offenses(<<~RUBY) + str.gsub!(/pattern/, '') + RUBY end - context 'with starting pattern `\A` and ending pattern `\z`' do - it 'does not register an offense and corrects when using `gsub`' do - expect_no_offenses(<<~RUBY) - str.gsub(/\\Aprefix\\z/, '') - RUBY - end + it 'does not register an offense when using `sub`' do + expect_no_offenses(<<~RUBY) + str.sub(/pattern/, '') + RUBY + end - it 'does not register an offense and corrects when using `gsub!`' do - expect_no_offenses(<<~RUBY) - str.gsub!(/\\Aprefix\\z/, '') - RUBY - end + it 'does not register an offense when using `sub!`' do + expect_no_offenses(<<~RUBY) + str.sub!(/pattern/, '') + RUBY + end + end - it 'does not register an offense and corrects when using `sub`' do - expect_no_offenses(<<~RUBY) - str.sub(/\\Aprefix\\z/, '') - RUBY - end + context 'with starting pattern `\A` and ending pattern `\z`' do + it 'does not register an offense and corrects when using `gsub`' do + expect_no_offenses(<<~RUBY) + str.gsub(/\\Aprefix\\z/, '') + RUBY + end - it 'does not register an offense and corrects when using `sub!`' do - expect_no_offenses(<<~RUBY) - str.sub!(/\\Aprefix\\z/, '') - RUBY - end + it 'does not register an offense and corrects when using `gsub!`' do + expect_no_offenses(<<~RUBY) + str.gsub!(/\\Aprefix\\z/, '') + RUBY end - context 'when using a non-blank string as replacement string' do - it 'does not register an offense and corrects when using `gsub`' do - expect_no_offenses(<<~RUBY) - str.gsub(/\\Aprefix/, 'foo') - RUBY - end + it 'does not register an offense and corrects when using `sub`' do + expect_no_offenses(<<~RUBY) + str.sub(/\\Aprefix\\z/, '') + RUBY + end - it 'does not register an offense and corrects when using `gsub!`' do - expect_no_offenses(<<~RUBY) - str.gsub!(/\\Aprefix/, 'foo') - RUBY - end + it 'does not register an offense and corrects when using `sub!`' do + expect_no_offenses(<<~RUBY) + str.sub!(/\\Aprefix\\z/, '') + RUBY + end + end - it 'does not register an offense and corrects when using `sub`' do - expect_no_offenses(<<~RUBY) - str.sub(/\\Aprefix/, 'foo') - RUBY - end + context 'when using a non-blank string as replacement string' do + it 'does not register an offense and corrects when using `gsub`' do + expect_no_offenses(<<~RUBY) + str.gsub(/\\Aprefix/, 'foo') + RUBY + end - it 'does not register an offense and corrects when using `sub!`' do - expect_no_offenses(<<~RUBY) - str.sub!(/\\Aprefix/, 'foo') - RUBY - end + it 'does not register an offense and corrects when using `gsub!`' do + expect_no_offenses(<<~RUBY) + str.gsub!(/\\Aprefix/, 'foo') + RUBY end - it 'does not register an offense when using `delete_prefix`' do + it 'does not register an offense and corrects when using `sub`' do expect_no_offenses(<<~RUBY) - str.delete_prefix('prefix') + str.sub(/\\Aprefix/, 'foo') RUBY end - it 'does not register an offense when using `delete_prefix!`' do + it 'does not register an offense and corrects when using `sub!`' do expect_no_offenses(<<~RUBY) - str.delete_prefix!('prefix') + str.sub!(/\\Aprefix/, 'foo') RUBY end end + + it 'does not register an offense when using `delete_prefix`' do + expect_no_offenses(<<~RUBY) + str.delete_prefix('prefix') + RUBY + end + + it 'does not register an offense when using `delete_prefix!`' do + expect_no_offenses(<<~RUBY) + str.delete_prefix!('prefix') + RUBY + end end diff --git a/spec/rubocop/cop/performance/delete_suffix_spec.rb b/spec/rubocop/cop/performance/delete_suffix_spec.rb index 899d063..4b15a02 100644 --- a/spec/rubocop/cop/performance/delete_suffix_spec.rb +++ b/spec/rubocop/cop/performance/delete_suffix_spec.rb @@ -4,37 +4,87 @@ let(:cop_config) { { 'SafeMultiline' => safe_multiline } } let(:safe_multiline) { true } - context 'TargetRubyVersion <= 2.4', :ruby24 do - it "does not register an offense when using `gsub(/suffix\z/, '')`" do - expect_no_offenses(<<~RUBY) + context 'when using `\z` as ending pattern' do + it "registers an offense and corrects when `gsub(/suffix\z/, '')`" do + expect_offense(<<~RUBY) str.gsub(/suffix\\z/, '') + ^^^^ Use `delete_suffix` instead of `gsub`. + RUBY + + expect_correction(<<~RUBY) + str.delete_suffix('suffix') RUBY end - it "does not register an offense when using `gsub!(/suffix\z/, '')`" do - expect_no_offenses(<<~RUBY) + it "registers an offense and corrects when `gsub!(/suffix\z/, '')`" do + expect_offense(<<~RUBY) str.gsub!(/suffix\\z/, '') + ^^^^^ Use `delete_suffix!` instead of `gsub!`. + RUBY + + expect_correction(<<~RUBY) + str.delete_suffix!('suffix') RUBY end - it "does not register an offense when using `sub(/suffix\z/, '')`" do - expect_no_offenses(<<~RUBY) + it "registers an offense and corrects when `sub(/suffix\z/, '')`" do + expect_offense(<<~RUBY) str.sub(/suffix\\z/, '') + ^^^ Use `delete_suffix` instead of `sub`. + RUBY + + expect_correction(<<~RUBY) + str.delete_suffix('suffix') RUBY end - it "does not register an offense when using `sub!(/suffix\z/, '')`" do - expect_no_offenses(<<~RUBY) + it "registers an offense and corrects when `sub!(/suffix\z/, '')`" do + expect_offense(<<~RUBY) str.sub!(/suffix\\z/, '') + ^^^^ Use `delete_suffix!` instead of `sub!`. + RUBY + + expect_correction(<<~RUBY) + str.delete_suffix!('suffix') RUBY end end - context 'TargetRubyVersion >= 2.5', :ruby25 do - context 'when using `\z` as ending pattern' do - it "registers an offense and corrects when `gsub(/suffix\z/, '')`" do + context 'when using `$` as ending pattern' do + context 'when `SafeMultiline: true`' do + let(:safe_multiline) { true } + + it 'does not register an offense and corrects when using `gsub`' do + expect_no_offenses(<<~RUBY) + str.gsub(/suffix$/, '') + RUBY + end + + it 'does not register an offense and corrects when using `gsub!`' do + expect_no_offenses(<<~RUBY) + str.gsub!(/suffix$/, '') + RUBY + end + + it 'does not register an offense and corrects when using `sub`' do + expect_no_offenses(<<~RUBY) + str.sub(/suffix$/, '') + RUBY + end + + it 'does not register an offense and corrects when using `sub!`' do + expect_no_offenses(<<~RUBY) + str.sub!(/suffix$/, '') + RUBY + end + end + + context 'when `SafeMultiline: false`' do + let(:safe_multiline) { false } + + it 'registers an offense and corrects when using `gsub`' do expect_offense(<<~RUBY) - str.gsub(/suffix\\z/, '') + str.gsub(/suffix$/, '') ^^^^ Use `delete_suffix` instead of `gsub`. RUBY @@ -43,9 +93,9 @@ RUBY end - it "registers an offense and corrects when `gsub!(/suffix\z/, '')`" do + it 'registers an offense and corrects when using `gsub!`' do expect_offense(<<~RUBY) - str.gsub!(/suffix\\z/, '') + str.gsub!(/suffix$/, '') ^^^^^ Use `delete_suffix!` instead of `gsub!`. RUBY @@ -54,9 +104,9 @@ RUBY end - it "registers an offense and corrects when `sub(/suffix\z/, '')`" do + it 'registers an offense and corrects when using `sub`' do expect_offense(<<~RUBY) - str.sub(/suffix\\z/, '') + str.sub(/suffix$/, '') ^^^ Use `delete_suffix` instead of `sub`. RUBY @@ -65,9 +115,9 @@ RUBY end - it "registers an offense and corrects when `sub!(/suffix\z/, '')`" do + it 'registers an offense and corrects when using `sub!`' do expect_offense(<<~RUBY) - str.sub!(/suffix\\z/, '') + str.sub!(/suffix$/, '') ^^^^ Use `delete_suffix!` instead of `sub!`. RUBY @@ -76,173 +126,95 @@ RUBY end end + end - context 'when using `$` as ending pattern' do - context 'when `SafeMultiline: true`' do - let(:safe_multiline) { true } - - it 'does not register an offense and corrects when using `gsub`' do - expect_no_offenses(<<~RUBY) - str.gsub(/suffix$/, '') - RUBY - end - - it 'does not register an offense and corrects when using `gsub!`' do - expect_no_offenses(<<~RUBY) - str.gsub!(/suffix$/, '') - RUBY - end - - it 'does not register an offense and corrects when using `sub`' do - expect_no_offenses(<<~RUBY) - str.sub(/suffix$/, '') - RUBY - end - - it 'does not register an offense and corrects when using `sub!`' do - expect_no_offenses(<<~RUBY) - str.sub!(/suffix$/, '') - RUBY - end - end - - context 'when `SafeMultiline: false`' do - let(:safe_multiline) { false } - - it 'registers an offense and corrects when using `gsub`' do - expect_offense(<<~RUBY) - str.gsub(/suffix$/, '') - ^^^^ Use `delete_suffix` instead of `gsub`. - RUBY - - expect_correction(<<~RUBY) - str.delete_suffix('suffix') - RUBY - end - - it 'registers an offense and corrects when using `gsub!`' do - expect_offense(<<~RUBY) - str.gsub!(/suffix$/, '') - ^^^^^ Use `delete_suffix!` instead of `gsub!`. - RUBY - - expect_correction(<<~RUBY) - str.delete_suffix!('suffix') - RUBY - end - - it 'registers an offense and corrects when using `sub`' do - expect_offense(<<~RUBY) - str.sub(/suffix$/, '') - ^^^ Use `delete_suffix` instead of `sub`. - RUBY - - expect_correction(<<~RUBY) - str.delete_suffix('suffix') - RUBY - end - - it 'registers an offense and corrects when using `sub!`' do - expect_offense(<<~RUBY) - str.sub!(/suffix$/, '') - ^^^^ Use `delete_suffix!` instead of `sub!`. - RUBY - - expect_correction(<<~RUBY) - str.delete_suffix!('suffix') - RUBY - end - end + context 'when using non-ending pattern' do + it 'does not register an offense when using `gsub`' do + expect_no_offenses(<<~RUBY) + str.gsub(/pattern/, '') + RUBY end - context 'when using non-ending pattern' do - it 'does not register an offense when using `gsub`' do - expect_no_offenses(<<~RUBY) - str.gsub(/pattern/, '') - RUBY - end - - it 'does not register an offense when using `gsub!`' do - expect_no_offenses(<<~RUBY) - str.gsub!(/pattern/, '') - RUBY - end - - it 'does not register an offense when using `sub`' do - expect_no_offenses(<<~RUBY) - str.sub(/pattern/, '') - RUBY - end - - it 'does not register an offense when using `sub!`' do - expect_no_offenses(<<~RUBY) - str.sub!(/pattern/, '') - RUBY - end + it 'does not register an offense when using `gsub!`' do + expect_no_offenses(<<~RUBY) + str.gsub!(/pattern/, '') + RUBY end - context 'with starting pattern `\A` and ending pattern `\z`' do - it 'does not register an offense and corrects when using `gsub`' do - expect_no_offenses(<<~RUBY) - str.gsub(/\\Asuffix\\z/, '') - RUBY - end + it 'does not register an offense when using `sub`' do + expect_no_offenses(<<~RUBY) + str.sub(/pattern/, '') + RUBY + end - it 'does not register an offense and corrects when using `gsub!`' do - expect_no_offenses(<<~RUBY) - str.gsub!(/\\Asuffix\\z/, '') - RUBY - end + it 'does not register an offense when using `sub!`' do + expect_no_offenses(<<~RUBY) + str.sub!(/pattern/, '') + RUBY + end + end - it 'does not register an offense and corrects when using `sub`' do - expect_no_offenses(<<~RUBY) - str.sub(/\\Asuffix\\z/, '') - RUBY - end + context 'with starting pattern `\A` and ending pattern `\z`' do + it 'does not register an offense and corrects when using `gsub`' do + expect_no_offenses(<<~RUBY) + str.gsub(/\\Asuffix\\z/, '') + RUBY + end - it 'does not register an offense and corrects when using `sub!`' do - expect_no_offenses(<<~RUBY) - str.sub!(/\\Asuffix\\z/, '') - RUBY - end + it 'does not register an offense and corrects when using `gsub!`' do + expect_no_offenses(<<~RUBY) + str.gsub!(/\\Asuffix\\z/, '') + RUBY end - context 'when using a non-blank string as replacement string' do - it 'does not register an offense and corrects when using `gsub`' do - expect_no_offenses(<<~RUBY) - str.gsub(/suffix\\z/, 'foo') - RUBY - end + it 'does not register an offense and corrects when using `sub`' do + expect_no_offenses(<<~RUBY) + str.sub(/\\Asuffix\\z/, '') + RUBY + end - it 'does not register an offense and corrects when using `gsub!`' do - expect_no_offenses(<<~RUBY) - str.gsub!(/suffix\\z/, 'foo') - RUBY - end + it 'does not register an offense and corrects when using `sub!`' do + expect_no_offenses(<<~RUBY) + str.sub!(/\\Asuffix\\z/, '') + RUBY + end + end - it 'does not register an offense and corrects when using `sub`' do - expect_no_offenses(<<~RUBY) - str.sub(/suffix\\z/, 'foo') - RUBY - end + context 'when using a non-blank string as replacement string' do + it 'does not register an offense and corrects when using `gsub`' do + expect_no_offenses(<<~RUBY) + str.gsub(/suffix\\z/, 'foo') + RUBY + end - it 'does not register an offense and corrects when using `sub!`' do - expect_no_offenses(<<~RUBY) - str.sub!(/suffix\\z/, 'foo') - RUBY - end + it 'does not register an offense and corrects when using `gsub!`' do + expect_no_offenses(<<~RUBY) + str.gsub!(/suffix\\z/, 'foo') + RUBY end - it 'does not register an offense when using `delete_suffix`' do + it 'does not register an offense and corrects when using `sub`' do expect_no_offenses(<<~RUBY) - str.delete_suffix('suffix') + str.sub(/suffix\\z/, 'foo') RUBY end - it 'does not register an offense when using `delete_suffix!`' do + it 'does not register an offense and corrects when using `sub!`' do expect_no_offenses(<<~RUBY) - str.delete_suffix!('suffix') + str.sub!(/suffix\\z/, 'foo') RUBY end end + + it 'does not register an offense when using `delete_suffix`' do + expect_no_offenses(<<~RUBY) + str.delete_suffix('suffix') + RUBY + end + + it 'does not register an offense when using `delete_suffix!`' do + expect_no_offenses(<<~RUBY) + str.delete_suffix!('suffix') + RUBY + end end diff --git a/spec/rubocop/cop/performance/redundant_equality_comparison_block_spec.rb b/spec/rubocop/cop/performance/redundant_equality_comparison_block_spec.rb index 5220062..2065a41 100644 --- a/spec/rubocop/cop/performance/redundant_equality_comparison_block_spec.rb +++ b/spec/rubocop/cop/performance/redundant_equality_comparison_block_spec.rb @@ -1,120 +1,109 @@ # frozen_string_literal: true RSpec.describe RuboCop::Cop::Performance::RedundantEqualityComparisonBlock, :config do - context 'TargetRubyVersion >= 2.5', :ruby25 do - RuboCop::Cop::Performance::RedundantEqualityComparisonBlock::TARGET_METHODS.each do |method_name| - it "registers and corrects an offense when using `#{method_name}` with `===` comparison block" do - expect_offense(<<~RUBY, method_name: method_name) - items.#{method_name} { |item| pattern === item } - ^{method_name}^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `#{method_name}(pattern)` instead of block. - RUBY - - expect_correction(<<~RUBY) - items.#{method_name}(pattern) - RUBY - end - - it "registers and corrects an offense when using `#{method_name}` with `==` comparison block" do - expect_offense(<<~RUBY, method_name: method_name) - items.#{method_name} { |item| item == other } - ^{method_name}^^^^^^^^^^^^^^^^^^^^^^^^^ Use `#{method_name}(other)` instead of block. - RUBY - - expect_correction(<<~RUBY) - items.#{method_name}(other) - RUBY - end - - it "registers and corrects an offense when using `#{method_name}` with `is_a?` comparison block" do - expect_offense(<<~RUBY, method_name: method_name) - items.#{method_name} { |item| item.is_a?(Klass) } - ^{method_name}^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `#{method_name}(Klass)` instead of block. - RUBY - - expect_correction(<<~RUBY) - items.#{method_name}(Klass) - RUBY - end - - it "registers and corrects an offense when using `#{method_name}` with `kind_of?` comparison block" do - expect_offense(<<~RUBY, method_name: method_name) - items.#{method_name} { |item| item.kind_of?(Klass) } - ^{method_name}^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `#{method_name}(Klass)` instead of block. - RUBY - - expect_correction(<<~RUBY) - items.#{method_name}(Klass) - RUBY - end - - it "does not register an offense when using `#{method_name}` with `===` comparison block and" \ - 'block argument is not used as a receiver for `===`' do - expect_no_offenses(<<~RUBY, method_name: method_name) - items.#{method_name} { |item| item === pattern } - RUBY - end - end - - it 'registers and corrects an offense when using method chanin and `all?` with `===` comparison block' do - expect_offense(<<~RUBY) - items.do_something.all? { |item| item == other } - ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `all?(other)` instead of block. + RuboCop::Cop::Performance::RedundantEqualityComparisonBlock::TARGET_METHODS.each do |method_name| + it "registers and corrects an offense when using `#{method_name}` with `===` comparison block" do + expect_offense(<<~RUBY, method_name: method_name) + items.#{method_name} { |item| pattern === item } + ^{method_name}^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `#{method_name}(pattern)` instead of block. RUBY expect_correction(<<~RUBY) - items.do_something.all?(other) + items.#{method_name}(pattern) RUBY end - it 'does not register an offense when using `all?` without `===` comparison block' do - expect_no_offenses(<<~RUBY) - items.all?(other) + it "registers and corrects an offense when using `#{method_name}` with `==` comparison block" do + expect_offense(<<~RUBY, method_name: method_name) + items.#{method_name} { |item| item == other } + ^{method_name}^^^^^^^^^^^^^^^^^^^^^^^^^ Use `#{method_name}(other)` instead of block. RUBY - end - it 'does not register an offense when using multiple block arguments' do - expect_no_offenses(<<~RUBY) - items.all? { |key, _value| key == other } + expect_correction(<<~RUBY) + items.#{method_name}(other) RUBY end - it 'does not register an offense when using block argument is used for an argument of `is_a`' do - expect_no_offenses(<<~RUBY) - klasses.all? { |klass| item.is_a?(klass) } + it "registers and corrects an offense when using `#{method_name}` with `is_a?` comparison block" do + expect_offense(<<~RUBY, method_name: method_name) + items.#{method_name} { |item| item.is_a?(Klass) } + ^{method_name}^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `#{method_name}(Klass)` instead of block. RUBY - end - it 'does not register an offense when using block argument is used for an argument of `kind_of?`' do - expect_no_offenses(<<~RUBY) - klasses.all? { |klass| item.kind_of?(klass) } + expect_correction(<<~RUBY) + items.#{method_name}(Klass) RUBY end - it 'does not register an offense when using block argument is not used as it is' do - expect_no_offenses(<<~RUBY) - items.all? { |item| item.do_something == other } + it "registers and corrects an offense when using `#{method_name}` with `kind_of?` comparison block" do + expect_offense(<<~RUBY, method_name: method_name) + items.#{method_name} { |item| item.kind_of?(Klass) } + ^{method_name}^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `#{method_name}(Klass)` instead of block. RUBY - end - it 'does not register an offense when using one argument with comma separator in block argument' do - expect_no_offenses(<<~RUBY) - items.all? { |item,| item == other } + expect_correction(<<~RUBY) + items.#{method_name}(Klass) RUBY end - it 'does not register an offense when using not target methods with `===` comparison block' do - expect_no_offenses(<<~RUBY) - items.do_something { |item| item == other } + it "does not register an offense when using `#{method_name}` with `===` comparison block and" \ + 'block argument is not used as a receiver for `===`' do + expect_no_offenses(<<~RUBY, method_name: method_name) + items.#{method_name} { |item| item === pattern } RUBY end end - context 'TargetRubyVersion <= 2.4', :ruby24 do - # Ruby 2.4 does not support `items.all?(Klass)`. - it 'does not register an offense when using `all?` with `is_a?` comparison block' do - expect_no_offenses(<<~RUBY) - items.all? { |item| item.is_a?(Klass) } - RUBY - end + it 'registers and corrects an offense when using method chanin and `all?` with `===` comparison block' do + expect_offense(<<~RUBY) + items.do_something.all? { |item| item == other } + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `all?(other)` instead of block. + RUBY + + expect_correction(<<~RUBY) + items.do_something.all?(other) + RUBY + end + + it 'does not register an offense when using `all?` without `===` comparison block' do + expect_no_offenses(<<~RUBY) + items.all?(other) + RUBY + end + + it 'does not register an offense when using multiple block arguments' do + expect_no_offenses(<<~RUBY) + items.all? { |key, _value| key == other } + RUBY + end + + it 'does not register an offense when using block argument is used for an argument of `is_a`' do + expect_no_offenses(<<~RUBY) + klasses.all? { |klass| item.is_a?(klass) } + RUBY + end + + it 'does not register an offense when using block argument is used for an argument of `kind_of?`' do + expect_no_offenses(<<~RUBY) + klasses.all? { |klass| item.kind_of?(klass) } + RUBY + end + + it 'does not register an offense when using block argument is not used as it is' do + expect_no_offenses(<<~RUBY) + items.all? { |item| item.do_something == other } + RUBY + end + + it 'does not register an offense when using one argument with comma separator in block argument' do + expect_no_offenses(<<~RUBY) + items.all? { |item,| item == other } + RUBY + end + + it 'does not register an offense when using not target methods with `===` comparison block' do + expect_no_offenses(<<~RUBY) + items.do_something { |item| item == other } + RUBY end end diff --git a/tasks/cops_documentation.rake b/tasks/cops_documentation.rake index cf1b283..ef48aaa 100644 --- a/tasks/cops_documentation.rake +++ b/tasks/cops_documentation.rake @@ -46,17 +46,15 @@ task documentation_syntax_check: :yard_for_generate_documentation do end examples.to_a.each do |example| - begin - buffer = Parser::Source::Buffer.new('', 1) - buffer.source = example.text - parser = Parser::Ruby25.new(RuboCop::AST::Builder.new) - parser.diagnostics.all_errors_are_fatal = true - parser.parse(buffer) - rescue Parser::SyntaxError => e - path = example.object.file - puts "#{path}: Syntax Error in an example. #{e}" - ok = false - end + buffer = Parser::Source::Buffer.new('', 1) + buffer.source = example.text + parser = Parser::Ruby25.new(RuboCop::AST::Builder.new) + parser.diagnostics.all_errors_are_fatal = true + parser.parse(buffer) + rescue Parser::SyntaxError => e + path = example.object.file + puts "#{path}: Syntax Error in an example. #{e}" + ok = false end end abort unless ok