From 5726b1d1d7665c33830ad114f45258ecf772b7b6 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Mon, 6 Feb 2023 13:03:27 +0900 Subject: [PATCH] Use RuboCop Performance 1.16.0 ## Summary This PR bumps RuboCop Performance to 1.16.0 and suppresses the following new offenses: ```console % bundle exec rubocop (snip) Offenses: actionpack/lib/action_dispatch/routing/mapper.rb:309:16: C: [Correctable] Performance/StringInclude: Use String#include? instead of a regex match with literal-only pattern. if /#/.match?(to) ^^^^^^^^^^^^^^ actionpack/lib/action_dispatch/routing/mapper.rb:1643:18: C: [Correctable] Performance/StringInclude: Use String#include? instead of a regex match with literal-only pattern. if /#/.match?(to) ^^^^^^^^^^^^^^ actionpack/lib/action_dispatch/routing/route_set.rb:887:67: C: [Correctable] Performance/StringInclude: Use String#include? instead of a regex match with literal-only pattern. path = Journey::Router::Utils.normalize_path(path) unless %r{://}.match?(path) ^^^^^^^^^^^^^^^^^^^^ actionpack/lib/action_dispatch/testing/assertions/routing.rb:86:12: C: [Correctable] Performance/StringInclude: Use String#include? instead of a regex match with literal-only pattern. if %r{://}.match?(expected_path) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ actionpack/lib/action_dispatch/testing/assertions/routing.rb:205:14: C: [Correctable] Performance/StringInclude: Use String#include? instead of a regex match with literal-only pattern. if %r{://}.match?(path) ^^^^^^^^^^^^^^^^^^^^ actionpack/lib/action_dispatch/testing/integration.rb:235:12: C: [Correctable] Performance/StringInclude: Use String#include? instead of a regex match with literal-only pattern. if %r{://}.match?(path) ^^^^^^^^^^^^^^^^^^^^ actiontext/bin/webpack:18:6: C: [Correctable] Performance/StringInclude: Use String#include? instead of a regex match with literal-only pattern. if /This file was generated by Bundler/.match?(File.read(bundle_binstub, 150)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ actiontext/bin/webpack-dev-server:18:6: C: [Correctable] Performance/StringInclude: Use String#include? instead of a regex match with literal-only pattern. if /This file was generated by Bundler/.match?(File.read(bundle_binstub, 150)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ activerecord/lib/active_record/connection_adapters/postgresql/quoting.rb:120:64: C: [Correctable] Performance/StringInclude: Use String#include? instead of a regex match with literal-only pattern. elsif column.type == :uuid && value.is_a?(String) && /\(\)/.match?(value) ^^^^^^^^^^^^^^^^^^^^ railties/lib/rails/commands/secrets/secrets_command.rb:28:12: C: [Correctable] Performance/StringInclude: Use String#include? instead of a regex match with literal-only pattern. if /secrets\.yml\.enc/.match?(error.message) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 3088 files inspected, 10 offenses detected, 10 offenses autocorrectable ``` ## Additional Information This behavior change is based on: https://github.com/rubocop/rubocop-performance/pull/332 --- Gemfile.lock | 2 +- actionpack/lib/action_dispatch/routing/mapper.rb | 4 ++-- actionpack/lib/action_dispatch/routing/route_set.rb | 2 +- actionpack/lib/action_dispatch/testing/assertions/routing.rb | 4 ++-- actionpack/lib/action_dispatch/testing/integration.rb | 2 +- actiontext/bin/webpack | 2 +- actiontext/bin/webpack-dev-server | 2 +- .../active_record/connection_adapters/postgresql/quoting.rb | 2 +- railties/lib/rails/commands/secrets/secrets_command.rb | 2 +- 9 files changed, 11 insertions(+), 11 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 1434d9d598f..181b3a671b4 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -410,7 +410,7 @@ GEM rubocop (>= 0.90, < 2.0) rubocop-packaging (0.5.2) rubocop (>= 1.33, < 2.0) - rubocop-performance (1.15.2) + rubocop-performance (1.16.0) rubocop (>= 1.7.0, < 2.0) rubocop-ast (>= 0.4.0) rubocop-rails (2.17.4) diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb index 946c4e48228..bbe817c72ce 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -306,7 +306,7 @@ module ActionDispatch end def split_to(to) - if /#/.match?(to) + if to&.include?("#") to.split("#").map!(&:-@) else [] @@ -1640,7 +1640,7 @@ module ActionDispatch when Symbol options[:action] = to when String - if /#/.match?(to) + if to.include?("#") options[:to] = to else options[:controller] = to diff --git a/actionpack/lib/action_dispatch/routing/route_set.rb b/actionpack/lib/action_dispatch/routing/route_set.rb index 7cb1bef06ac..4a13de98bc8 100644 --- a/actionpack/lib/action_dispatch/routing/route_set.rb +++ b/actionpack/lib/action_dispatch/routing/route_set.rb @@ -884,7 +884,7 @@ module ActionDispatch def recognize_path(path, environment = {}) method = (environment[:method] || "GET").to_s.upcase - path = Journey::Router::Utils.normalize_path(path) unless %r{://}.match?(path) + path = Journey::Router::Utils.normalize_path(path) unless path.include?("://") extras = environment[:extras] || {} begin diff --git a/actionpack/lib/action_dispatch/testing/assertions/routing.rb b/actionpack/lib/action_dispatch/testing/assertions/routing.rb index ddd93959852..42e82151cd8 100644 --- a/actionpack/lib/action_dispatch/testing/assertions/routing.rb +++ b/actionpack/lib/action_dispatch/testing/assertions/routing.rb @@ -83,7 +83,7 @@ module ActionDispatch # # Asserts that the generated route gives us our custom route # assert_generates "changesets/12", { controller: 'scm', action: 'show_diff', revision: "12" } def assert_generates(expected_path, options, defaults = {}, extras = {}, message = nil) - if %r{://}.match?(expected_path) + if expected_path.include?("://") fail_on(URI::InvalidURIError, message) do uri = URI.parse(expected_path) expected_path = uri.path.to_s.empty? ? "/" : uri.path @@ -202,7 +202,7 @@ module ActionDispatch controller = @controller if defined?(@controller) request = ActionController::TestRequest.create controller&.class - if %r{://}.match?(path) + if path.include?("://") fail_on(URI::InvalidURIError, msg) do uri = URI.parse(path) request.env["rack.url_scheme"] = uri.scheme || "http" diff --git a/actionpack/lib/action_dispatch/testing/integration.rb b/actionpack/lib/action_dispatch/testing/integration.rb index a4c368e1fbb..43b606cda8b 100644 --- a/actionpack/lib/action_dispatch/testing/integration.rb +++ b/actionpack/lib/action_dispatch/testing/integration.rb @@ -232,7 +232,7 @@ module ActionDispatch method = :post end - if %r{://}.match?(path) + if path.include?("://") path = build_expanded_path(path) do |location| https! URI::HTTPS === location if location.scheme diff --git a/actiontext/bin/webpack b/actiontext/bin/webpack index ac2adfb6b99..ac959ee7e53 100755 --- a/actiontext/bin/webpack +++ b/actiontext/bin/webpack @@ -15,7 +15,7 @@ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile", bundle_binstub = File.expand_path("../bundle", __FILE__) if File.file?(bundle_binstub) - if /This file was generated by Bundler/.match?(File.read(bundle_binstub, 150)) + if File.read(bundle_binstub, 150).include?("This file was generated by Bundler") load(bundle_binstub) else abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run. diff --git a/actiontext/bin/webpack-dev-server b/actiontext/bin/webpack-dev-server index c1ac52d675e..54b03c0b9f9 100755 --- a/actiontext/bin/webpack-dev-server +++ b/actiontext/bin/webpack-dev-server @@ -15,7 +15,7 @@ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile", bundle_binstub = File.expand_path("../bundle", __FILE__) if File.file?(bundle_binstub) - if /This file was generated by Bundler/.match?(File.read(bundle_binstub, 150)) + if File.read(bundle_binstub, 150).include?("This file was generated by Bundler") load(bundle_binstub) else abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run. diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/quoting.rb b/activerecord/lib/active_record/connection_adapters/postgresql/quoting.rb index 33090461c3b..7e15e95894e 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/quoting.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/quoting.rb @@ -117,7 +117,7 @@ module ActiveRecord def quote_default_expression(value, column) # :nodoc: if value.is_a?(Proc) value.call - elsif column.type == :uuid && value.is_a?(String) && /\(\)/.match?(value) + elsif column.type == :uuid && value.is_a?(String) && value.include?("()") value # Does not quote function default values for UUID columns elsif column.respond_to?(:array?) type = lookup_cast_type_from_column(column) diff --git a/railties/lib/rails/commands/secrets/secrets_command.rb b/railties/lib/rails/commands/secrets/secrets_command.rb index c379e6ce8c3..5470e444422 100644 --- a/railties/lib/rails/commands/secrets/secrets_command.rb +++ b/railties/lib/rails/commands/secrets/secrets_command.rb @@ -25,7 +25,7 @@ module Rails rescue Rails::Secrets::MissingKeyError => error say error.message rescue Errno::ENOENT => error - if /secrets\.yml\.enc/.match?(error.message) + if error.message.include?("secrets.yml.enc") deprecate_in_favor_of_credentials_and_exit else raise