diff --git a/.rubocop.yml b/.rubocop.yml index 81bd763e05..ee1c150bd9 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -312,5 +312,17 @@ Performance/DeleteSuffix: Performance/OpenStruct: Enabled: true +Performance/InefficientHashSearch: + Enabled: true + +Performance/ConstantRegexp: + Enabled: true + +Performance/RedundantStringChars: + Enabled: true + +Performance/StringInclude: + Enabled: true + Minitest/UnreachableAssertion: Enabled: true diff --git a/actionpack/lib/action_controller/metal/http_authentication.rb b/actionpack/lib/action_controller/metal/http_authentication.rb index 439ffd5c99..efb70eeb86 100644 --- a/actionpack/lib/action_controller/metal/http_authentication.rb +++ b/actionpack/lib/action_controller/metal/http_authentication.rb @@ -502,11 +502,14 @@ module ActionController array_params.each { |param| (param[1] || +"").gsub! %r/^"|"$/, "" } end + WHITESPACED_AUTHN_PAIR_DELIMITERS = /\s*#{AUTHN_PAIR_DELIMITERS}\s*/ + private_constant :WHITESPACED_AUTHN_PAIR_DELIMITERS + # This method takes an authorization body and splits up the key-value # pairs by the standardized :, ;, or \t # delimiters defined in +AUTHN_PAIR_DELIMITERS+. def raw_params(auth) - _raw_params = auth.sub(TOKEN_REGEX, "").split(/\s*#{AUTHN_PAIR_DELIMITERS}\s*/) + _raw_params = auth.sub(TOKEN_REGEX, "").split(WHITESPACED_AUTHN_PAIR_DELIMITERS) if !_raw_params.first&.start_with?(TOKEN_KEY) _raw_params[0] = "#{TOKEN_KEY}#{_raw_params.first}" diff --git a/actionpack/lib/action_dispatch/testing/assertions/response.rb b/actionpack/lib/action_dispatch/testing/assertions/response.rb index fa6f35e67e..3256efc2af 100644 --- a/actionpack/lib/action_dispatch/testing/assertions/response.rb +++ b/actionpack/lib/action_dispatch/testing/assertions/response.rb @@ -30,7 +30,7 @@ module ActionDispatch def assert_response(type, message = nil) message ||= generate_response_message(type) - if RESPONSE_PREDICATES.keys.include?(type) + if RESPONSE_PREDICATES.key?(type) assert @response.public_send(RESPONSE_PREDICATES[type]), message else assert_equal AssertionResponse.new(type).code, @response.response_code, message @@ -93,7 +93,7 @@ module ActionDispatch end def code_with_name(code_or_name) - if RESPONSE_PREDICATES.values.include?("#{code_or_name}?".to_sym) + if RESPONSE_PREDICATES.value?("#{code_or_name}?".to_sym) code_or_name = RESPONSE_PREDICATES.invert["#{code_or_name}?".to_sym] end diff --git a/actionview/lib/action_view/template.rb b/actionview/lib/action_view/template.rb index d912109221..5cae79973c 100644 --- a/actionview/lib/action_view/template.rb +++ b/actionview/lib/action_view/template.rb @@ -193,6 +193,9 @@ module ActionView @source.to_s end + LEADING_ENCODING_REGEXP = /\A#{ENCODING_FLAG}/ + private_constant :LEADING_ENCODING_REGEXP + # This method is responsible for properly setting the encoding of the # source. Until this point, we assume that the source is BINARY data. # If no additional information is supplied, we assume the encoding is @@ -211,7 +214,7 @@ module ActionView # Look for # encoding: *. If we find one, we'll encode the # String in that encoding, otherwise, we'll use the # default external encoding. - if source.sub!(/\A#{ENCODING_FLAG}/, "") + if source.sub!(LEADING_ENCODING_REGEXP, "") encoding = magic_encoding = $1 else encoding = Encoding.default_external diff --git a/activerecord/lib/active_record/encryption/encryptable_record.rb b/activerecord/lib/active_record/encryption/encryptable_record.rb index 04e84c44c8..d268b924f2 100644 --- a/activerecord/lib/active_record/encryption/encryptable_record.rb +++ b/activerecord/lib/active_record/encryption/encryptable_record.rb @@ -61,7 +61,7 @@ module ActiveRecord # Given a attribute name, it returns the name of the source attribute when it's a preserved one. def source_attribute_from_preserved_attribute(attribute_name) - attribute_name.to_s.sub(ORIGINAL_ATTRIBUTE_PREFIX, "") if /^#{ORIGINAL_ATTRIBUTE_PREFIX}/.match?(attribute_name) + attribute_name.to_s.sub(ORIGINAL_ATTRIBUTE_PREFIX, "") if attribute_name.start_with?(ORIGINAL_ATTRIBUTE_PREFIX) end private diff --git a/activerecord/lib/active_record/test_fixtures.rb b/activerecord/lib/active_record/test_fixtures.rb index 1ad2228478..fa6cdb71c0 100644 --- a/activerecord/lib/active_record/test_fixtures.rb +++ b/activerecord/lib/active_record/test_fixtures.rb @@ -59,7 +59,7 @@ module ActiveRecord unless fixture_set_names.empty? self.fixture_sets = fixture_sets.dup fixture_set_names.each do |fs_name| - key = fs_name.match?(%r{/}) ? -fs_name.to_s.tr("/", "_") : fs_name + key = fs_name.to_s.include?("/") ? -fs_name.to_s.tr("/", "_") : fs_name key = -key.to_s if key.is_a?(Symbol) fs_name = -fs_name.to_s if fs_name.is_a?(Symbol) fixture_sets[key] = fs_name diff --git a/ci/test_run.rb b/ci/test_run.rb index c3ba53ea7a..c270f46b2f 100644 --- a/ci/test_run.rb +++ b/ci/test_run.rb @@ -35,7 +35,7 @@ class TestRun end def completed? - @qunit_testresult.text =~ /Tests completed/ + @qunit_testresult.text.include?("Tests completed") end def result diff --git a/guides/rails_guides/epub.rb b/guides/rails_guides/epub.rb index 149d69fa77..7a8c9e431a 100644 --- a/guides/rails_guides/epub.rb +++ b/guides/rails_guides/epub.rb @@ -49,7 +49,7 @@ module Epub # :nodoc: end def is_name_invalid(name) - (/[0-9]/ === name.chars.first) + name.match?(/\A\d/) end def fix_file_names(output_dir) diff --git a/railties/lib/rails/generators/rails/benchmark/benchmark_generator.rb b/railties/lib/rails/generators/rails/benchmark/benchmark_generator.rb index 7c76d6551c..fe83931947 100644 --- a/railties/lib/rails/generators/rails/benchmark/benchmark_generator.rb +++ b/railties/lib/rails/generators/rails/benchmark/benchmark_generator.rb @@ -6,6 +6,7 @@ module Rails module Generators class BenchmarkGenerator < NamedBase IPS_GEM_NAME = "benchmark-ips" + IPS_GEM_USED_REGEXP = /gem.*\b#{IPS_GEM_NAME}\b.*/ argument :reports, type: :array, default: ["before", "after"] @@ -21,7 +22,7 @@ module Rails def ips_installed? in_root do - return File.read("Gemfile").match?(/gem.*\b#{IPS_GEM_NAME}\b.*/) + return File.read("Gemfile").match?(IPS_GEM_USED_REGEXP) end end end diff --git a/tasks/release.rb b/tasks/release.rb index add25605f1..f63b01e170 100644 --- a/tasks/release.rb +++ b/tasks/release.rb @@ -314,7 +314,7 @@ module Announcement end def rc? - @version =~ /rc/ + @version.include?("rc") end end end