From 4474626238932c9c3234371f8a22c68cbc6f85fa Mon Sep 17 00:00:00 2001 From: Kayla Reopelle Date: Fri, 24 Feb 2023 14:12:28 -0800 Subject: [PATCH] Enable Performance/RegexpMatch rubocop --- .rubocop_todo.yml | 5 ----- bin/nrdebug | 2 +- lib/new_relic/agent/configuration/environment_source.rb | 2 +- lib/new_relic/agent/configuration/server_source.rb | 2 +- lib/new_relic/agent/configuration/yaml_source.rb | 2 +- lib/new_relic/agent/connect/request_builder.rb | 2 +- lib/new_relic/agent/custom_event_aggregator.rb | 2 +- lib/new_relic/agent/database/obfuscation_helpers.rb | 2 +- lib/new_relic/agent/error_filter.rb | 4 ++-- lib/new_relic/agent/http_clients/net_http_wrappers.rb | 2 +- .../agent/instrumentation/action_view_subscriber.rb | 2 +- lib/new_relic/agent/samplers/memory_sampler.rb | 2 +- lib/new_relic/agent/utilization/vendor.rb | 2 +- lib/new_relic/cli/command.rb | 2 +- lib/new_relic/latest_changes.rb | 6 +++--- lib/tasks/helpers/format.rb | 2 +- test/multiverse/lib/multiverse/suite.rb | 4 ++-- test/new_relic/agent/pipe_channel_manager_test.rb | 2 +- test/new_relic/fake_server.rb | 2 +- test/new_relic/framework_test.rb | 2 +- 20 files changed, 23 insertions(+), 28 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index eae27435220..571a646edff 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -63,11 +63,6 @@ Minitest/EmptyLineBeforeAssertionMethods: - 'test/new_relic/agent_test.rb' - 'test/new_relic/cli/commands/deployments_test.rb' -# Offense count: 23 -# This cop supports safe autocorrection (--autocorrect). -Performance/RegexpMatch: - Enabled: false - # Offense count: 1 # This cop supports unsafe autocorrection (--autocorrect-all). # Configuration parameters: OnlySumOrWithInitialValue. diff --git a/bin/nrdebug b/bin/nrdebug index 33460a9d534..995f992c9bb 100755 --- a/bin/nrdebug +++ b/bin/nrdebug @@ -216,7 +216,7 @@ class ProcessReport section(f) do c_backtraces, ruby_backtraces = @target.gather_backtraces - if c_backtraces =~ /could not attach/i + if /could not attach/i.match?(c_backtraces) fail("Failed to attach to target process. Please try again with sudo.") end diff --git a/lib/new_relic/agent/configuration/environment_source.rb b/lib/new_relic/agent/configuration/environment_source.rb index b1e2a0c3a4e..0ac72aa4cb0 100644 --- a/lib/new_relic/agent/configuration/environment_source.rb +++ b/lib/new_relic/agent/configuration/environment_source.rb @@ -94,7 +94,7 @@ def set_key_by_type(config_key, environment_key) elsif type == Array self[config_key] = value.split(/\s*,\s*/) elsif type == NewRelic::Agent::Configuration::Boolean - if value =~ /false|off|no/i + if /false|off|no/i.match?(value) self[config_key] = false elsif !value.nil? self[config_key] = true diff --git a/lib/new_relic/agent/configuration/server_source.rb b/lib/new_relic/agent/configuration/server_source.rb index 91af81afde0..7069d4c5fc9 100644 --- a/lib/new_relic/agent/configuration/server_source.rb +++ b/lib/new_relic/agent/configuration/server_source.rb @@ -70,7 +70,7 @@ def merge_agent_config_hash(merged_settings, connect_reply) def fix_transaction_threshold(merged_settings) # when value is "apdex_f" remove the config and defer to default - if merged_settings['transaction_tracer.transaction_threshold'].to_s =~ /apdex_f/i + if /apdex_f/i.match?(merged_settings['transaction_tracer.transaction_threshold'].to_s) merged_settings.delete('transaction_tracer.transaction_threshold') end end diff --git a/lib/new_relic/agent/configuration/yaml_source.rb b/lib/new_relic/agent/configuration/yaml_source.rb index 85535344f77..40c6d09a8fc 100644 --- a/lib/new_relic/agent/configuration/yaml_source.rb +++ b/lib/new_relic/agent/configuration/yaml_source.rb @@ -124,7 +124,7 @@ def substitute_transaction_threshold(config) config['transaction_tracer']['transaction_threshold'].to_s =~ /apdex_f/i # when value is "apdex_f" remove the config and defer to default config['transaction_tracer'].delete('transaction_threshold') - elsif config['transaction_tracer.transaction_threshold'].to_s =~ /apdex_f/i + elsif /apdex_f/i.match?(config['transaction_tracer.transaction_threshold'].to_s) config.delete('transaction_tracer.transaction_threshold') end end diff --git a/lib/new_relic/agent/connect/request_builder.rb b/lib/new_relic/agent/connect/request_builder.rb index f9639231b1a..2d852d5e76b 100644 --- a/lib/new_relic/agent/connect/request_builder.rb +++ b/lib/new_relic/agent/connect/request_builder.rb @@ -48,7 +48,7 @@ def sanitize_environment_report(environment_report) def environment_metadata env_copy = {} - ENV.keys.each { |k| env_copy[k] = ENV[k] if k =~ /^NEW_RELIC_METADATA_/ } + ENV.keys.each { |k| env_copy[k] = ENV[k] if /^NEW_RELIC_METADATA_/.match?(k) } env_copy end diff --git a/lib/new_relic/agent/custom_event_aggregator.rb b/lib/new_relic/agent/custom_event_aggregator.rb index 5af3cf9aad3..517e3bbc5b4 100644 --- a/lib/new_relic/agent/custom_event_aggregator.rb +++ b/lib/new_relic/agent/custom_event_aggregator.rb @@ -27,7 +27,7 @@ def record(type, attributes) return unless enabled? type = @type_strings[type] - unless type =~ EVENT_TYPE_REGEX + unless EVENT_TYPE_REGEX.match?(type) note_dropped_event(type) return false end diff --git a/lib/new_relic/agent/database/obfuscation_helpers.rb b/lib/new_relic/agent/database/obfuscation_helpers.rb index 3b76783f6b8..ab63392fb2c 100644 --- a/lib/new_relic/agent/database/obfuscation_helpers.rb +++ b/lib/new_relic/agent/database/obfuscation_helpers.rb @@ -52,7 +52,7 @@ module ObfuscationHelpers FAILED_TO_OBFUSCATE_MESSAGE = "Failed to obfuscate SQL query - quote characters remained after obfuscation".freeze def obfuscate_single_quote_literals(sql) - return sql unless sql =~ COMPONENTS_REGEX_MAP[:single_quotes] + return sql unless sql&.match?(COMPONENTS_REGEX_MAP[:single_quotes]) sql.gsub(COMPONENTS_REGEX_MAP[:single_quotes], PLACEHOLDER) end diff --git a/lib/new_relic/agent/error_filter.rb b/lib/new_relic/agent/error_filter.rb index dd2c49da9a4..47b9eb6802f 100644 --- a/lib/new_relic/agent/error_filter.rb +++ b/lib/new_relic/agent/error_filter.rb @@ -80,7 +80,7 @@ def ignore(*args) @ignore_messages.update(errors) log_filter(:ignore_messages, errors) when String - if errors =~ /^[\d\,\-]+$/ + if /^[\d\,\-]+$/.match?(errors) @ignore_status_codes |= parse_status_codes(errors) log_filter(:ignore_status_codes, errors) else @@ -104,7 +104,7 @@ def expect(*args) @expected_messages.update(errors) log_filter(:expected_messages, errors) when String - if errors =~ /^[\d\,\-]+$/ + if /^[\d\,\-]+$/.match?(errors) @expected_status_codes |= parse_status_codes(errors) log_filter(:expected_status_codes, errors) else diff --git a/lib/new_relic/agent/http_clients/net_http_wrappers.rb b/lib/new_relic/agent/http_clients/net_http_wrappers.rb index ecf46ebbcd0..21f1800bd86 100644 --- a/lib/new_relic/agent/http_clients/net_http_wrappers.rb +++ b/lib/new_relic/agent/http_clients/net_http_wrappers.rb @@ -61,7 +61,7 @@ def uri ::NewRelic::Agent::HTTPClients::URIUtil.parse_and_normalize_url(@request.path) else connection_address = @connection.address - if connection_address =~ Resolv::IPv6::Regex + if Resolv::IPv6::Regex.match?(connection_address) connection_address = "[#{connection_address}]" end diff --git a/lib/new_relic/agent/instrumentation/action_view_subscriber.rb b/lib/new_relic/agent/instrumentation/action_view_subscriber.rb index 6aa9747fdc1..2c3608617b9 100644 --- a/lib/new_relic/agent/instrumentation/action_view_subscriber.rb +++ b/lib/new_relic/agent/instrumentation/action_view_subscriber.rb @@ -75,7 +75,7 @@ def metric_path(name, identifier) # so do not mistake rendering a collection for rendering a file. if identifier.nil? && name != RENDER_COLLECTION_EVENT_NAME 'file' - elsif identifier =~ /template$/ + elsif /template$/.match?(identifier) identifier elsif identifier && (parts = identifier.split('/')).size > 1 parts[-2..-1].join('/') diff --git a/lib/new_relic/agent/samplers/memory_sampler.rb b/lib/new_relic/agent/samplers/memory_sampler.rb index 903e9799349..8904aaa773a 100644 --- a/lib/new_relic/agent/samplers/memory_sampler.rb +++ b/lib/new_relic/agent/samplers/memory_sampler.rb @@ -29,7 +29,7 @@ def initialize end elsif platform.include?('darwin9') # 10.5 @sampler = ShellPS.new("ps -o rsz") - elsif platform =~ /darwin(1|2)\d+/ # >= 10.6 + elsif /darwin(1|2)\d+/.match?(platform) # >= 10.6 @sampler = ShellPS.new("ps -o rss") elsif platform.include?('freebsd') @sampler = ShellPS.new("ps -o rss") diff --git a/lib/new_relic/agent/utilization/vendor.rb b/lib/new_relic/agent/utilization/vendor.rb index 0271adb72d8..bceac3932e5 100644 --- a/lib/new_relic/agent/utilization/vendor.rb +++ b/lib/new_relic/agent/utilization/vendor.rb @@ -133,7 +133,7 @@ def valid_length?(value) def valid_chars?(value) value.each_char do |ch| - next if ch =~ VALID_CHARS + next if VALID_CHARS.match?(ch) code_point = ch[0].ord # this works in Ruby 1.8.7 - 2.1.2 next if code_point >= 0x80 diff --git a/lib/new_relic/cli/command.rb b/lib/new_relic/cli/command.rb index d01ba47989e..9ad9359cd10 100644 --- a/lib/new_relic/cli/command.rb +++ b/lib/new_relic/cli/command.rb @@ -60,7 +60,7 @@ def self.run extra = [] options = ARGV.options do |opts| script_name = File.basename($0) - if script_name =~ /newrelic_cmd$/ + if /newrelic_cmd$/.match?(script_name) $stdout.puts "warning: the 'newrelic_cmd' script has been renamed 'newrelic'" script_name = 'newrelic' end diff --git a/lib/new_relic/latest_changes.rb b/lib/new_relic/latest_changes.rb index 6d2ce135e38..f603383f0a0 100644 --- a/lib/new_relic/latest_changes.rb +++ b/lib/new_relic/latest_changes.rb @@ -30,8 +30,8 @@ def self.read_patch(patch_level, changelog = default_changelog) current_item = nil latest.each do |line| - if line =~ /^\s*\*.*/ - if line =~ /\(#{patch_level}\)/ + if /^\s*\*.*/.match?(line) + if /\(#{patch_level}\)/.match?(line) # Found a patch level item, so start tracking the lines! current_item = line else @@ -52,7 +52,7 @@ def self.extract_latest_changes(contents) changes = [] version_count = 0 contents.each_line do |line| - if line =~ /##\s+v[\d.]+/ + if /##\s+v[\d.]+/.match?(line) version_count += 1 end break if version_count >= 2 diff --git a/lib/tasks/helpers/format.rb b/lib/tasks/helpers/format.rb index 033adf91d2f..a8c1770b736 100644 --- a/lib/tasks/helpers/format.rb +++ b/lib/tasks/helpers/format.rb @@ -110,7 +110,7 @@ def pluck(key, config_hash) end def section_key(key, components) - if key =~ /^disable_/ # "disable_httpclient" + if /^disable_/.match?(key) # "disable_httpclient" DISABLING elsif components.length >= 2 && !(components[1] == "attributes") # "analytics_events.enabled" components.first diff --git a/test/multiverse/lib/multiverse/suite.rb b/test/multiverse/lib/multiverse/suite.rb index 87df88e182c..c2f5c4a60f1 100755 --- a/test/multiverse/lib/multiverse/suite.rb +++ b/test/multiverse/lib/multiverse/suite.rb @@ -275,8 +275,8 @@ def generate_gemfile(gemfile_text, env_index, local = true) File.open(gemfile, 'w') do |f| f.puts 'source "https://rubygems.org"' f.print gemfile_text - f.puts newrelic_gemfile_line unless gemfile_text =~ /^\s*gem .newrelic_rpm./ - f.puts minitest_line unless gemfile_text =~ /^\s*gem .minitest[^_]./ + f.puts newrelic_gemfile_line unless /^\s*gem .newrelic_rpm./.match?(gemfile_text) + f.puts minitest_line unless /^\s*gem .minitest[^_]./.match?(gemfile_text) f.puts "gem 'rake'" unless gemfile_text =~ /^\s*gem .rake[^_]./ || suite == 'rake' f.puts "gem 'rackup'" if need_rackup?(gemfile_text) diff --git a/test/new_relic/agent/pipe_channel_manager_test.rb b/test/new_relic/agent/pipe_channel_manager_test.rb index 23850510078..5625068123b 100644 --- a/test/new_relic/agent/pipe_channel_manager_test.rb +++ b/test/new_relic/agent/pipe_channel_manager_test.rb @@ -317,7 +317,7 @@ def test_blocking_error_rescued wake_mock = MiniTest::Mock.new out_mock = MiniTest::Mock.new 4.times { wake_mock.expect(:out, out_mock) } - error_stub = proc { |msg| desired_error_messages_seen += 1 if msg =~ /^(?:Issue while|Ready pipes)/ } + error_stub = proc { |msg| desired_error_messages_seen += 1 if /^(?:Issue while|Ready pipes)/.match?(msg) } ready_pipes_mock = MiniTest::Mock.new ::IO.stub(:select, [ready_pipes_mock]) do diff --git a/test/new_relic/fake_server.rb b/test/new_relic/fake_server.rb index a74235d87d4..1dd47b259c3 100644 --- a/test/new_relic/fake_server.rb +++ b/test/new_relic/fake_server.rb @@ -5,7 +5,7 @@ require 'webrick' require 'webrick/https' require 'rack' -require 'rackup/handler' unless Rack.release =~ /^1|2/ +require 'rackup/handler' unless /^1|2/.match?(Rack.release) require 'timeout' require 'json' diff --git a/test/new_relic/framework_test.rb b/test/new_relic/framework_test.rb index 08e16383d89..3f9ea0a83f4 100644 --- a/test/new_relic/framework_test.rb +++ b/test/new_relic/framework_test.rb @@ -35,7 +35,7 @@ module Base end # does the path match "rails\d" (ex: rails7) or "railsedge"? - if ENV['BUNDLE_GEMFILE'] =~ /rails(?:\d|edge)/ + if /rails(?:\d|edge)/.match?(ENV['BUNDLE_GEMFILE']) # rubocop:disable Performance/StringInclude assert_truthy NewRelic::Agent.config[:framework].match(/rails/) # rubocop:enable Performance/StringInclude