Use match? where we don't need MatchData

We're already running Performance/RegexpMatch cop, but it seems like the cop is not always =~ justice
This commit is contained in:
Akira Matsuda 2019-07-27 12:56:39 +09:00
parent e9651deea4
commit d1ffe59ab5
23 changed files with 28 additions and 28 deletions

View File

@ -162,7 +162,7 @@ module ActionDispatch
def valid_accept_header # :doc:
(xhr? && (accept.present? || content_mime_type)) ||
(accept.present? && accept !~ BROWSER_LIKE_ACCEPTS)
(accept.present? && !accept.match?(BROWSER_LIKE_ACCEPTS))
end
def use_accept_header # :doc:

View File

@ -133,7 +133,7 @@ module ActionDispatch
end
def named_host?(host)
IP_HOST_REGEXP !~ host
!IP_HOST_REGEXP.match?(host)
end
def normalize_protocol(protocol)

View File

@ -439,7 +439,7 @@ module ActionDispatch
# If host is not ip and matches domain regexp.
# (ip confirms to domain regexp so we explicitly check for ip)
options[:domain] = if (request.host !~ /^[\d.]+$/) && (request.host =~ domain_regexp)
options[:domain] = if !request.host.match?(/^[\d.]+$/) && (request.host =~ domain_regexp)
".#{$&}"
end
elsif options[:domain].is_a? Array

View File

@ -112,7 +112,7 @@ module ActionDispatch
end
def self.optional_format?(path, format)
format != false && path !~ OPTIONAL_FORMAT_REGEX
format != false && !path.match?(OPTIONAL_FORMAT_REGEX)
end
def initialize(set:, ast:, controller:, default_action:, to:, formatted:, via:, options_constraints:, anchor:, scope_params:, options:)
@ -1833,7 +1833,7 @@ module ActionDispatch
# and return nil in case it isn't. Otherwise, we pass the invalid name
# forward so the underlying router engine treats it and raises an exception.
if as.nil?
candidate unless candidate !~ /\A[_a-z]/i || has_named_route?(candidate)
candidate unless !candidate.match?(/\A[_a-z]/i) || has_named_route?(candidate)
else
candidate
end

View File

@ -544,8 +544,8 @@ class DebugExceptionsTest < ActionDispatch::IntegrationTest
@app = DevelopmentApp
Rails.stub :root, Pathname.new(".") do
cleaner = ActiveSupport::BacktraceCleaner.new.tap do |bc|
bc.add_silencer { |line| line =~ /method_that_raises/ }
bc.add_silencer { |line| line !~ %r{test/dispatch/debug_exceptions_test.rb} }
bc.add_silencer { |line| line.match?(/method_that_raises/) }
bc.add_silencer { |line| !line.match?(%r{test/dispatch/debug_exceptions_test.rb}) }
end
get "/framework_raises", headers: { "action_dispatch.backtrace_cleaner" => cleaner }
@ -596,7 +596,7 @@ class DebugExceptionsTest < ActionDispatch::IntegrationTest
@app = DevelopmentApp
Rails.stub :root, Pathname.new(".") do
cleaner = ActiveSupport::BacktraceCleaner.new.tap do |bc|
bc.add_silencer { |line| line !~ %r{test/dispatch/debug_exceptions_test.rb} }
bc.add_silencer { |line| !line.match?(%r{test/dispatch/debug_exceptions_test.rb}) }
end
get "/nested_exceptions", headers: { "action_dispatch.backtrace_cleaner" => cleaner }
@ -631,7 +631,7 @@ class DebugExceptionsTest < ActionDispatch::IntegrationTest
@app = DevelopmentApp
Rails.stub :root, Pathname.new(".") do
cleaner = ActiveSupport::BacktraceCleaner.new.tap do |bc|
bc.add_silencer { |line| line !~ %r{test/dispatch/debug_exceptions_test.rb} }
bc.add_silencer { |line| !line.match?(%r{test/dispatch/debug_exceptions_test.rb}) }
end
get "/actionable_error", headers: { "action_dispatch.backtrace_cleaner" => cleaner }

View File

@ -21,7 +21,7 @@ module ActionDispatch
setup do
@cleaner = ActiveSupport::BacktraceCleaner.new
@cleaner.remove_filters!
@cleaner.add_silencer { |line| line !~ /^lib/ }
@cleaner.add_silencer { |line| !line.match?(/^lib/) }
end
test "#source_extracts fetches source fragments for every backtrace entry" do

View File

@ -689,7 +689,7 @@ module ActionView
end
def add_method_to_attributes!(html_options, method)
if method_not_get_method?(method) && html_options["rel"] !~ /nofollow/
if method_not_get_method?(method) && !html_options["rel"]&.match?(/nofollow/)
if html_options["rel"].blank?
html_options["rel"] = "nofollow"
else

View File

@ -395,7 +395,7 @@ module ActionView
end
def _normalize_layout(value)
value.is_a?(String) && value !~ /\blayouts/ ? "layouts/#{value}" : value
value.is_a?(String) && !value.match?(/\blayouts/) ? "layouts/#{value}" : value
end
# Returns the default layout for this controller.

View File

@ -6,7 +6,7 @@ module ActiveModel
def validate_each(record, attribute, value)
if options[:with]
regexp = option_call(record, :with)
record_error(record, attribute, :with, value) if value.to_s !~ regexp
record_error(record, attribute, :with, value) unless regexp.match?(value.to_s)
elsif options[:without]
regexp = option_call(record, :without)
record_error(record, attribute, :without, value) if regexp.match?(value.to_s)

View File

@ -17,7 +17,7 @@ module ActiveRecord
when false, nil then false
else
if !type_for_attribute(attr_name) { false }
if Numeric === value || value !~ /[^0-9]/
if Numeric === value || !value.match?(/[^0-9]/)
!value.to_i.zero?
else
return false if ActiveModel::Type::Boolean::FALSE_VALUES.include?(value)

View File

@ -275,7 +275,7 @@ module ActiveRecord
# hash and merges with the rest of the hash.
# Connection details inside of the "url" key win any merge conflicts
def resolve_hash_connection(spec)
if spec["url"] && spec["url"] !~ /^jdbc:/
if spec["url"] && !spec["url"].match?(/^jdbc:/)
connection_hash = resolve_url_connection(spec.delete("url"))
spec.merge!(connection_hash)
end

View File

@ -26,7 +26,7 @@ module ActiveRecord
payload[:exception] ||
payload[:cached] ||
IGNORED_PAYLOADS.include?(payload[:name]) ||
payload[:sql] !~ EXPLAINED_SQLS
!payload[:sql].match?(EXPLAINED_SQLS)
end
ActiveSupport::Notifications.subscribe("sql.active_record", new)

View File

@ -9,7 +9,7 @@ module Arel # :nodoc: all
# if need to select first records without ORDER BY and GROUP BY and without DISTINCT
# then can use simple ROWNUM in WHERE clause
if o.limit && o.orders.empty? && o.cores.first.groups.empty? && !o.offset && o.cores.first.set_quantifier.class.to_s !~ /Distinct/
if o.limit && o.orders.empty? && o.cores.first.groups.empty? && !o.offset && !o.cores.first.set_quantifier.class.to_s.match?(/Distinct/)
o.cores.last.wheres.push Nodes::LessThanOrEqual.new(
Nodes::SqlLiteral.new("ROWNUM"), o.limit.expr
)

View File

@ -64,7 +64,7 @@ class BelongsToAssociationsTest < ActiveRecord::TestCase
Client.find(3).firm
ensure
sql_log = ActiveRecord::SQLCounter.log
assert sql_log.all? { |sql| /order by/i !~ sql }, "ORDER BY was used in the query: #{sql_log}"
assert sql_log.all? { |sql| !/order by/i.match?(sql) }, "ORDER BY was used in the query: #{sql_log}"
end
def test_belongs_to_with_primary_key

View File

@ -41,7 +41,7 @@ class HasOneAssociationsTest < ActiveRecord::TestCase
companies(:first_firm).account
ensure
sql_log = ActiveRecord::SQLCounter.log
assert sql_log.all? { |sql| /order by/i !~ sql }, "ORDER BY was used in the query: #{sql_log}"
assert sql_log.all? { |sql| !/order by/i.match?(sql) }, "ORDER BY was used in the query: #{sql_log}"
end
def test_has_one_cache_nils

View File

@ -15,7 +15,7 @@ class NameError
# We should use original_message message instead.
message = respond_to?(:original_message) ? original_message : self.message
if /undefined local variable or method/ !~ message
unless /undefined local variable or method/.match?(message)
$1 if /((::)?([A-Z]\w*)(::[A-Z]\w*)*)$/ =~ message
end
end

View File

@ -5,7 +5,7 @@ require "active_support/core_ext/hash/deep_merge"
module ActiveSupport
class OptionMerger #:nodoc:
instance_methods.each do |method|
undef_method(method) if method !~ /^(__|instance_eval|class|object_id)/
undef_method(method) unless method.match?(/^(__|instance_eval|class|object_id)/)
end
def initialize(context, options)

View File

@ -174,7 +174,7 @@ class MessageEncryptorTest < ActiveSupport::TestCase
def test_on_rotation_can_be_passed_at_the_constructor_level
older_message = ActiveSupport::MessageEncryptor.new(secrets[:older], "older sign").encrypt_and_sign(encoded: "message")
rotated = false
rotated = rotated = false # double assigning to suppress "assigned but unused variable" warning
encryptor = ActiveSupport::MessageEncryptor.new(@secret, on_rotation: proc { rotated = true })
encryptor.rotate secrets[:older], "older sign"
@ -188,7 +188,7 @@ class MessageEncryptorTest < ActiveSupport::TestCase
def test_on_rotation_option_takes_precedence_over_the_one_given_in_constructor
older_message = ActiveSupport::MessageEncryptor.new(secrets[:older], "older sign").encrypt_and_sign(encoded: "message")
rotated = false
rotated = rotated = false # double assigning to suppress "assigned but unused variable" warning
encryptor = ActiveSupport::MessageEncryptor.new(@secret, on_rotation: proc { rotated = true })
encryptor.rotate secrets[:older], "older sign"

View File

@ -44,7 +44,7 @@ class CodeStatistics #:nodoc:
Dir.foreach(directory) do |file_name|
path = "#{directory}/#{file_name}"
if File.directory?(path) && (/^\./ !~ file_name)
if File.directory?(path) && !(/^\./.match?(file_name))
stats.add(calculate_directory_statistics(path, pattern))
elsif file_name&.match?(pattern)
stats.add_by_file_path(path)

View File

@ -71,7 +71,7 @@ class CodeStatisticsCalculator #:nodoc:
@classes += 1 if patterns[:class] && line =~ patterns[:class]
@methods += 1 if patterns[:method] && line =~ patterns[:method]
if line !~ /^\s*$/ && (patterns[:line_comment].nil? || line !~ patterns[:line_comment])
if !line.match?(/^\s*$/) && (patterns[:line_comment].nil? || !line.match?(patterns[:line_comment]))
@code_lines += 1
end
end

View File

@ -52,7 +52,7 @@ module Rails
def inherited(base) #:nodoc:
super
if base.name && base.name !~ /Base$/
if base.name && !base.name.match?(/Base$/)
Rails::Command.subclasses << base
end
end

View File

@ -362,7 +362,7 @@ module Rails
base.called_from = begin
call_stack = caller_locations.map { |l| l.absolute_path || l.path }
File.dirname(call_stack.detect { |p| p !~ %r[railties[\w.-]*/lib/rails|rack[\w.-]*/lib/rack] })
File.dirname(call_stack.detect { |p| !p.match?(%r[railties[\w.-]*/lib/rails|rack[\w.-]*/lib/rack]) })
end
end

View File

@ -233,7 +233,7 @@ module Rails
# Invoke source_root so the default_source_root is set.
base.source_root
if base.name && base.name !~ /Base$/
if base.name && !base.name.match?(/Base$/)
Rails::Generators.subclasses << base
Rails::Generators.templates_path.each do |path|