diff options
author | Stan Hu <stanhu@gmail.com> | 2019-07-24 11:11:18 -0700 |
---|---|---|
committer | Stan Hu <stanhu@gmail.com> | 2019-07-24 11:47:58 -0700 |
commit | 3e001d29ccdb5a5b1ba223710525f8dc7ae844ee (patch) | |
tree | 5046754a8858795a02043a743e18fb8b05610f41 | |
parent | 0d538e44aff066372ecd9d10ac6786681bc347c9 (diff) | |
download | gitlab-ce-3e001d29ccdb5a5b1ba223710525f8dc7ae844ee.tar.gz |
Enable Rubocop Performance/InefficientHashSearchsh-enable-rubocop-hash-search
When used with a Hash, `.keys.include?` is bad because:
1. It performs a O(n) search instead of the efficient `.has_key?`
2. It clones all keys into separate array.
Closes https://gitlab.com/gitlab-org/gitlab-ce/issues/64975
-rw-r--r-- | .rubocop_todo.yml | 11 | ||||
-rw-r--r-- | app/controllers/concerns/sessionless_authentication.rb | 2 | ||||
-rw-r--r-- | app/models/note.rb | 2 | ||||
-rw-r--r-- | app/models/user_preference.rb | 2 | ||||
-rw-r--r-- | qa/spec/spec_helper.rb | 2 |
5 files changed, 4 insertions, 15 deletions
diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 41714eefa97..72de4f0b2aa 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -262,17 +262,6 @@ Naming/HeredocDelimiterNaming: Naming/RescuedExceptionsVariableName: Enabled: false -# Offense count: 6 -# Cop supports --auto-correct. -Performance/InefficientHashSearch: - Exclude: - - 'app/controllers/concerns/sessionless_authentication.rb' - - 'app/models/note.rb' - - 'app/models/user_preference.rb' - - 'ee/app/models/ee/project.rb' - - 'lib/gitlab/import_export/members_mapper.rb' - - 'qa/spec/spec_helper.rb' - # Offense count: 3 # Cop supports --auto-correct. Performance/ReverseEach: diff --git a/app/controllers/concerns/sessionless_authentication.rb b/app/controllers/concerns/sessionless_authentication.rb index 590eefc6dab..4304b8565ce 100644 --- a/app/controllers/concerns/sessionless_authentication.rb +++ b/app/controllers/concerns/sessionless_authentication.rb @@ -13,7 +13,7 @@ module SessionlessAuthentication end def sessionless_user? - current_user && !session.keys.include?('warden.user.user.key') + current_user && !session.key?('warden.user.user.key') end def sessionless_sign_in(user) diff --git a/app/models/note.rb b/app/models/note.rb index 5c31cff9816..3f182c1f099 100644 --- a/app/models/note.rb +++ b/app/models/note.rb @@ -292,7 +292,7 @@ class Note < ApplicationRecord end def special_role=(role) - raise "Role is undefined, #{role} not found in #{SpecialRole.values}" unless SpecialRole.values.include?(role) + raise "Role is undefined, #{role} not found in #{SpecialRole.values}" unless SpecialRole.value?(role) @special_role = role end diff --git a/app/models/user_preference.rb b/app/models/user_preference.rb index f1326f4c8cb..b236250c24e 100644 --- a/app/models/user_preference.rb +++ b/app/models/user_preference.rb @@ -26,7 +26,7 @@ class UserPreference < ApplicationRecord def set_notes_filter(filter_id, issuable) # No need to update the column if the value is already set. - if filter_id && NOTES_FILTERS.values.include?(filter_id) + if filter_id && NOTES_FILTERS.value?(filter_id) field = notes_filter_field_for(issuable) self[field] = filter_id diff --git a/qa/spec/spec_helper.rb b/qa/spec/spec_helper.rb index 21bfd2876a9..363980acc33 100644 --- a/qa/spec/spec_helper.rb +++ b/qa/spec/spec_helper.rb @@ -46,7 +46,7 @@ RSpec.configure do |config| if ENV['CI'] config.around do |example| - retry_times = example.metadata.keys.include?(:quarantine) ? 1 : 2 + retry_times = example.metadata.key?(:quarantine) ? 1 : 2 example.run_with_retry retry: retry_times end end |