Fixes Performance/RegexpMatch offenses

This commit is contained in:
Klaus Zanders
2023-09-06 12:50:32 +02:00
parent 37f7eb2a89
commit 26cee2729e
25 changed files with 28 additions and 28 deletions
+1 -1
View File
@@ -441,7 +441,7 @@ class ApplicationController < ActionController::Base
# Returns a string that can be used as filename value in Content-Disposition header
def filename_for_content_disposition(name)
request.env['HTTP_USER_AGENT'] =~ %r{(MSIE|Trident)} ? ERB::Util.url_encode(name) : name
%r{(MSIE|Trident)}.match?(request.env['HTTP_USER_AGENT']) ? ERB::Util.url_encode(name) : name
end
def api_request?
+1 -1
View File
@@ -243,7 +243,7 @@ class MembersController < ApplicationController
def each_comma_separated(array, &block)
array.map do |e|
if e.to_s.match /\d(,\d)*/
if e.to_s.match? /\d(,\d)*/
block.call(e)
else
e
+1 -1
View File
@@ -178,7 +178,7 @@ module RepositoriesHelper
str.force_encoding('ASCII-8BIT')
end
return str if str.empty?
return str if /\A[\r\n\t\x20-\x7e]*\Z/n.match(str) # for us-ascii
return str if /\A[\r\n\t\x20-\x7e]*\Z/n.match?(str) # for us-ascii
if str.respond_to?(:force_encoding)
str.force_encoding('UTF-8')
+1 -1
View File
@@ -187,7 +187,7 @@ module SortHelper
# Appends DESC to the sort criterion unless it has a fixed order
def append_desc(criterion)
if criterion =~ / (asc|desc)\z/i
if / (asc|desc)\z/i.match?(criterion)
criterion
else
"#{criterion} DESC"
+1 -1
View File
@@ -204,7 +204,7 @@ class Repository < ApplicationRecord
name = name.to_s
return nil if name.blank?
changesets.where((name.match(/\A\d*\z/) ? ['revision = ?', name] : ['revision LIKE ?', name + '%'])).first
changesets.where((name.match?(/\A\d*\z/) ? ['revision = ?', name] : ['revision LIKE ?', name + '%'])).first
end
def latest_changeset
+1 -1
View File
@@ -29,7 +29,7 @@
module CustomFields
class CreateService < ::BaseServices::Create
def self.careful_new_custom_field(type)
if type.to_s =~ /.+CustomField\z/
if /.+CustomField\z/.match?(type.to_s)
klass = type.to_s.constantize
klass.new if klass.ancestors.include? CustomField
end
@@ -53,7 +53,7 @@ class SecureContextUriValidator < ActiveModel::EachValidator
def self.secure_context_uri?(uri)
return true if uri.scheme == 'https' # https is always safe
return true if uri.host == 'localhost' # Simple localhost
return true if uri.host =~ /\.localhost\.?$/ # i.e. 'foo.localhost' or 'foo.localhost.'
return true if /\.localhost\.?$/.match?(uri.host) # i.e. 'foo.localhost' or 'foo.localhost.'
# Check for loopback interface. The constructor can throw an exception for non IP addresses.
# Those are invalid. And if the host is an IP address then we can check if it is loopback.
+1 -1
View File
@@ -199,7 +199,7 @@ class BackupJob < ApplicationJob
def get_cache_folder_path(attachment)
# expecting paths like /tmp/op_uploaded_files/1639754082-3468-0002-0911/file.ext
# just making extra sure so we don't delete anything wrong later on
unless attachment.diskfile.path =~ /#{attachment.file.cache_dir}\/[^\/]+\/[^\/]+/
unless /#{attachment.file.cache_dir}\/[^\/]+\/[^\/]+/.match?(attachment.diskfile.path)
raise "Unexpected cache path for attachment ##{attachment.id}: #{attachment.diskfile}"
end
+2 -2
View File
@@ -89,10 +89,10 @@ OpenProject::Application.configure do
relative_url = Regexp.escape(OpenProject::Configuration['rails_relative_url_root'])
# When we match SYS controller API, allow non-https access
return true if request.path =~ /#{relative_url}\/sys\//
return true if /#{relative_url}\/sys\//.match?(request.path)
# When we match health checks
return true if request.path =~ /#{relative_url}\/health_checks/
return true if /#{relative_url}\/health_checks/.match?(request.path)
false
end
+1 -1
View File
@@ -10,7 +10,7 @@ OpenProject::Application.configure do
ActiveSupport::Notifications.subscribe("sql.active_record") do |_name, start, finish, _id, data|
# Skip transaction that may be blocked
next if data[:sql].match(/BEGIN|COMMIT/)
next if data[:sql].match?(/BEGIN|COMMIT/)
# Skip smaller durations
duration = ((finish - start) * 1000).round(4)
+1 -1
View File
@@ -45,7 +45,7 @@ end
# As it is complicated to return all the paths where such an initialization file might exist,
# we simply return the general OpenProject namespace for such files.
OpenProject::Inflector.rule do |_basename, abspath|
if abspath =~ /\/lib\/openproject-\w+.rb\z/
if /\/lib\/openproject-\w+.rb\z/.match?(abspath)
'OpenProject'
end
end
@@ -47,7 +47,7 @@ class AddGinTrgmIndexOnJournalsAndCustomValues < ActiveRecord::Migration[7.0]
def safe_enable_pg_trgm_extension
ActiveRecord::Base.connection.execute("CREATE EXTENSION IF NOT EXISTS pg_trgm WITH SCHEMA pg_catalog;")
rescue StandardError => e
raise unless e.message =~ /pg_trgm/
raise unless /pg_trgm/.match?(e.message)
# Rollback the transaction in order to recover from the error.
ActiveRecord::Base.connection.execute 'ROLLBACK'
+1 -1
View File
@@ -114,7 +114,7 @@ module OpenProject
# Returns the number of active rules password adheres to.
def self.size_active_rules_adhered_by(password)
active_rules.count do |name|
password =~ RULES[name] ? true : false
password&.match?(RULES[name]) ? true : false
end
end
@@ -129,7 +129,7 @@ module OpenProject
end
def target(path = '')
base = path.match(/\A\//) ? root_url : url
base = path.match?(/\A\//) ? root_url : url
"#{base}/#{path}"
end
+1 -1
View File
@@ -103,7 +103,7 @@ module OpenProject
return if doc.at_xpath('/info/entry/repository/uuid')
stderr.each_line do |l|
Rails.logger.error("SVN access error: #{l}") if l =~ /E\d+:/
Rails.logger.error("SVN access error: #{l}") if /E\d+:/.match?(l)
raise Exceptions::SCMUnauthorized.new if l.include?('E215004: Authentication failed')
end
end
@@ -64,7 +64,7 @@ module OpenProject::TextFormatting
filename = node['src'].downcase
# We only match a specific set of attributes as before
return unless filename =~ matched_filenames_regex
return unless filename&.match?(matched_filenames_regex)
# Try to find the attachment
if (attachment = attachments.detect { |att| att.filename.downcase == filename })
+2 -2
View File
@@ -44,7 +44,7 @@ module Redmine
# Returns false when the diff ends
def add_line(line)
if @parsing
if line =~ /^[^+\-\s@\\]/
if /^[^+\-\s@\\]/.match?(line)
@parsing = false
return false
elsif line =~ /^@@ (\+|-)(\d+)(,\d+)? (\+|-)(\d+)(,\d+)? @@/
@@ -117,7 +117,7 @@ module Redmine
true
else
write_offsets
if line[0, 1] =~ /\s/
if /\s/.match?(line[0, 1])
diff = Diff.new
diff.line_right = line[1..-1]
diff.nb_line_right = @line_num_r
+1 -1
View File
@@ -82,7 +82,7 @@ namespace :git do
puts local_branches.join("\n")
puts 'Proceed? (y/n)'
if STDIN.gets =~ /^y/i
if /^y/i.match?(STDIN.gets)
remote_branches.each do |b|
match = b.match(/^([^\/]+)\/(.+)/)
remote = match[1]
@@ -15,7 +15,7 @@ module ::Avatars
end
end
unless avatar.original_filename =~ /\.(jpe?g|gif|png)\z/i
unless /\.(jpe?g|gif|png)\z/i.match?(avatar.original_filename)
return error_result(I18n.t(:wrong_file_format))
end
@@ -86,7 +86,7 @@ module OpenProject::Backlogs::Hooks
end
if params[:copy_tasks]
params[:copy_tasks] += ':' if params[:copy_tasks] !~ /:/
params[:copy_tasks] += ':' if !/:/.match?(params[:copy_tasks])
action, id = *params[:copy_tasks].split(/:/)
story = (id.nil? ? nil : Story.find(Integer(id)))
@@ -86,7 +86,7 @@ class CostlogController < ApplicationController
@cost_entry.destroy
flash[:notice] = t(:notice_successful_delete)
if request.referer =~ /cost_reports/
if /cost_reports/.match?(request.referer)
redirect_to controller: '/cost_reports', action: :index
else
redirect_back fallback_location: work_package_path(@cost_entry.work_package)
@@ -68,7 +68,7 @@ module CostQuery::CustomFieldMixin
def remove_subclasses
module_parent.constants.each do |constant|
if constant.to_s.match /^CustomField\d+/
if constant.to_s.match? /^CustomField\d+/
module_parent.send(:remove_const, constant)
end
end
+2 -2
View File
@@ -1,6 +1,6 @@
#!/usr/bin/env ruby
if ARGV.join(" ") =~ /((-h)|(--help))/
if /((-h)|(--help))/.match?(ARGV.join(" "))
puts "Prints the self-contained OpenAPI 3.0 specification of OpenProject's APIv3"
puts
puts "usage: ./script/api/spec [--format JSON|yaml]"
@@ -15,7 +15,7 @@ require_relative '../../lib/api/open_api'
path = Pathname(__dir__).join("../../docs/api/apiv3/openapi-spec.yml")
spec = API::OpenAPI.assemble_spec path
format = ARGV.join(" ") =~ /((--format)|(-f))((=)|(\s+))ya?ml/ ? :yaml : :json
format = /((--format)|(-f))((=)|(\s+))ya?ml/.match?(ARGV.join(" ")) ? :yaml : :json
begin
if format == :yaml
+1 -1
View File
@@ -124,7 +124,7 @@ module Pages
elsif name == 'created_at'
select(human_operator, from: 'operator')
set_created_at_filter(human_operator, values)
elsif name =~ /cf_\d+/
elsif /cf_\d+/.match?(name)
select(human_operator, from: 'operator')
set_custom_field_filter(selected_filter, human_operator, values)
end
+1 -1
View File
@@ -135,7 +135,7 @@ class WithDirectUploads
.stub("https://" + OpenProject::Configuration.remote_storage_upload_host + ":443/", method: 'post')
.and_return(Proc.new do |_params, _headers, body, _url, _method|
{
code: body =~ /X-Amz-Signature/ ? 201 : 403, # check that the expected post to AWS was made with the form fields
code: /X-Amz-Signature/.match?(body) ? 201 : 403, # check that the expected post to AWS was made with the form fields
headers: {
'Access-Control-Allow-Methods' => 'POST',
'Access-Control-Allow-Origin' => '*'