diff --git a/.rubocop.yml b/.rubocop.yml index 6b42dc1e5..c6fc94bdc 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -690,16 +690,20 @@ Performance/IoReadlines: Reference: 'https://docs.gitlab.com/ee/development/performance.html#reading-from-files-and-other-data-sources' Enabled: true -Performance/RedundantEqualityComparisonBlock: - Description: >- - Checks for uses `Enumerable#all?`, `Enumerable#any?`, `Enumerable#one?`, - or `Enumerable#none?` are compared with `===` or similar methods in block. +Performance/MapCompact: + Description: 'Use `filter_map` instead of `collection.map(&:do_something).compact`.' Enabled: true Performance/OpenStruct: Description: 'Use `Struct` instead of `OpenStruct`.' Enabled: true +Performance/RedundantEqualityComparisonBlock: + Description: >- + Checks for uses `Enumerable#all?`, `Enumerable#any?`, `Enumerable#one?`, + or `Enumerable#none?` are compared with `===` or similar methods in block. + Enabled: true + Performance/RedundantSortBlock: Description: 'Use `sort` instead of `sort { |a, b| a <=> b }`.' Enabled: true @@ -716,6 +720,10 @@ Performance/ReverseFirst: Description: 'Use `last(n).reverse` instead of `reverse.first(n)`.' Enabled: true +Performance/SelectMap: + Description: 'Use `filter_map` instead of `ary.select(&:foo).map(&:bar)`.' + Enabled: true + Performance/SortReverse: Description: 'Use `sort.reverse` instead of `sort { |a, b| b <=> a }`.' Enabled: true diff --git a/Gemfile b/Gemfile index a522571a2..dbe5385d4 100644 --- a/Gemfile +++ b/Gemfile @@ -150,7 +150,7 @@ group :development, :test do gem "pry-byebug", "~> 3.8" # Combine 'pry' with 'byebug'. Adds 'step', 'next', 'finish', 'continue' and 'break' commands to control execution gem "rspec-rails", "~> 5.0" # rspec-rails is a testing framework for Rails 3+ gem "rubocop", "~> 1.13", require: false # Automatic Ruby code style checking tool - gem "rubocop-performance", "~> 1.10", require: false # A collection of RuboCop cops to check for performance optimizations in Ruby code + gem "rubocop-performance", "~> 1.11", require: false # A collection of RuboCop cops to check for performance optimizations in Ruby code gem "rubocop-rails", "~> 2.9", require: false # Automatic Rails code style checking tool gem "rubocop-rspec", "~> 2.2", require: false # Code style checking for RSpec files gem "sassc-rails", "~> 2.1.2" # Integrate SassC-Ruby into Rails diff --git a/Gemfile.lock b/Gemfile.lock index 18fbc021f..108f45173 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -672,8 +672,8 @@ GEM unicode-display_width (>= 1.4.0, < 3.0) rubocop-ast (1.4.1) parser (>= 2.7.1.5) - rubocop-performance (1.10.2) - rubocop (>= 0.90.0, < 2.0) + rubocop-performance (1.11.0) + rubocop (>= 1.7.0, < 2.0) rubocop-ast (>= 0.4.0) rubocop-rails (2.9.1) activesupport (>= 4.2.0) @@ -967,7 +967,7 @@ DEPENDENCIES rspec-rails (~> 5.0) rspec-retry (~> 0.6) rubocop (~> 1.13) - rubocop-performance (~> 1.10) + rubocop-performance (~> 1.11) rubocop-rails (~> 2.9) rubocop-rspec (~> 2.2) ruby-prof (~> 1.4) diff --git a/app/services/broadcasts/welcome_notification/generator.rb b/app/services/broadcasts/welcome_notification/generator.rb index e753ed278..f3c18ce4e 100644 --- a/app/services/broadcasts/welcome_notification/generator.rb +++ b/app/services/broadcasts/welcome_notification/generator.rb @@ -131,9 +131,9 @@ module Broadcasts end def find_auth_broadcast - missing_identities = Authentication::Providers.enabled.map do |provider| + missing_identities = Authentication::Providers.enabled.filter_map do |provider| identities.exists?(provider: provider) ? nil : "#{provider}_connect" - end.compact + end Broadcast.active.find_by!(title: "Welcome Notification: #{missing_identities.first}") end diff --git a/app/services/search/base.rb b/app/services/search/base.rb index 30bfa79d7..16d7ab4ce 100644 --- a/app/services/search/base.rb +++ b/app/services/search/base.rb @@ -117,7 +117,7 @@ module Search def process_hashes(indexing_hashes) indexing_hashes.in_groups_of(1000, false).flat_map do |hashes| - indexing_chunks(hashes).select(&:any?).map { |chunk| Search::Client.bulk(body: chunk) } + indexing_chunks(hashes).filter_map { |chunk| Search::Client.bulk(body: chunk) } end end diff --git a/app/services/search/reading_list.rb b/app/services/search/reading_list.rb index e57adb59c..cc696a438 100644 --- a/app/services/search/reading_list.rb +++ b/app/services/search/reading_list.rb @@ -67,12 +67,12 @@ module Search def parse_and_order_articles(articles) # Combines reaction and article data to create hashes that contain the fields # the reading list view needs. Ensures articles are returned in order of reaction ID - reading_list_article_ids.map do |article_id, reaction_id| + reading_list_article_ids.filter_map do |article_id, reaction_id| found_article_doc = articles[article_id] next unless found_article_doc { "id" => reaction_id, "user_id" => user.id, "reactable" => articles[article_id] } - end.compact + end end end end diff --git a/spec/requests/notifications_spec.rb b/spec/requests/notifications_spec.rb index f2ae0ee78..1dd20fdd6 100644 --- a/spec/requests/notifications_spec.rb +++ b/spec/requests/notifications_spec.rb @@ -432,7 +432,7 @@ RSpec.describe "NotificationsIndex", type: :request do get notifications_path(filter: :org, org_id: other_org.id) notifications = controller.instance_variable_get(:@notifications) - expect(notifications.map(&:organization_id).compact.size).to eq(0) + expect(notifications.filter_map(&:organization_id).size).to eq(0) end it "does render notifications belonging to other orgs if admin" do diff --git a/vendor/cache/rubocop-performance-1.10.2.gem b/vendor/cache/rubocop-performance-1.10.2.gem deleted file mode 100644 index 5e6395005..000000000 Binary files a/vendor/cache/rubocop-performance-1.10.2.gem and /dev/null differ diff --git a/vendor/cache/rubocop-performance-1.11.0.gem b/vendor/cache/rubocop-performance-1.11.0.gem new file mode 100644 index 000000000..dc821ab54 Binary files /dev/null and b/vendor/cache/rubocop-performance-1.11.0.gem differ