Skip to content

Commit

Permalink
Enable Performance/RegexpMatch rubocop
Browse files Browse the repository at this point in the history
  • Loading branch information
kaylareopelle committed Feb 24, 2023
1 parent 45a43b5 commit 4474626
Show file tree
Hide file tree
Showing 20 changed files with 23 additions and 28 deletions.
5 changes: 0 additions & 5 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion bin/nrdebug
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/new_relic/agent/configuration/environment_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/new_relic/agent/configuration/server_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/new_relic/agent/configuration/yaml_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/new_relic/agent/connect/request_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/new_relic/agent/custom_event_aggregator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/new_relic/agent/database/obfuscation_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/new_relic/agent/error_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/new_relic/agent/http_clients/net_http_wrappers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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('/')
Expand Down
2 changes: 1 addition & 1 deletion lib/new_relic/agent/samplers/memory_sampler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion lib/new_relic/agent/utilization/vendor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/new_relic/cli/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions lib/new_relic/latest_changes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/tasks/helpers/format.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions test/multiverse/lib/multiverse/suite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion test/new_relic/agent/pipe_channel_manager_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/new_relic/fake_server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
2 changes: 1 addition & 1 deletion test/new_relic/framework_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4474626

Please sign in to comment.