From 570079f8ced28e4f50e3336ce756490ff9d4ae26 Mon Sep 17 00:00:00 2001
From: Matt Jankowski <matt@jankowski.online>
Date: Tue, 2 May 2023 12:07:16 -0400
Subject: [PATCH] Fix Performance/TimesMap cop (#24789)
---
.rubocop_todo.yml | 9 ---------
spec/controllers/api/v1/blocks_controller_spec.rb | 8 ++++----
spec/controllers/api/v1/mutes_controller_spec.rb | 8 ++++----
spec/lib/feed_manager_spec.rb | 10 +++++-----
spec/lib/request_pool_spec.rb | 2 +-
spec/models/account_spec.rb | 2 +-
6 files changed, 15 insertions(+), 24 deletions(-)
diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml
index cac72400fa..17b0a33a0a 100644
--- a/.rubocop_todo.yml
+++ b/.rubocop_todo.yml
@@ -365,15 +365,6 @@ Performance/StartWith:
Exclude:
- 'app/lib/extractor.rb'
-# This cop supports unsafe autocorrection (--autocorrect-all).
-Performance/TimesMap:
- Exclude:
- - 'spec/controllers/api/v1/blocks_controller_spec.rb'
- - 'spec/controllers/api/v1/mutes_controller_spec.rb'
- - 'spec/lib/feed_manager_spec.rb'
- - 'spec/lib/request_pool_spec.rb'
- - 'spec/models/account_spec.rb'
-
# This cop supports unsafe autocorrection (--autocorrect-all).
Performance/UnfreezeString:
Exclude:
diff --git a/spec/controllers/api/v1/blocks_controller_spec.rb b/spec/controllers/api/v1/blocks_controller_spec.rb
index a746389ca2..fe71531865 100644
--- a/spec/controllers/api/v1/blocks_controller_spec.rb
+++ b/spec/controllers/api/v1/blocks_controller_spec.rb
@@ -13,13 +13,13 @@ RSpec.describe Api::V1::BlocksController, type: :controller do
describe 'GET #index' do
it 'limits according to limit parameter' do
- 2.times.map { Fabricate(:block, account: user.account) }
+ Array.new(2) { Fabricate(:block, account: user.account) }
get :index, params: { limit: 1 }
expect(body_as_json.size).to eq 1
end
it 'queries blocks in range according to max_id' do
- blocks = 2.times.map { Fabricate(:block, account: user.account) }
+ blocks = Array.new(2) { Fabricate(:block, account: user.account) }
get :index, params: { max_id: blocks[1] }
@@ -28,7 +28,7 @@ RSpec.describe Api::V1::BlocksController, type: :controller do
end
it 'queries blocks in range according to since_id' do
- blocks = 2.times.map { Fabricate(:block, account: user.account) }
+ blocks = Array.new(2) { Fabricate(:block, account: user.account) }
get :index, params: { since_id: blocks[0] }
@@ -37,7 +37,7 @@ RSpec.describe Api::V1::BlocksController, type: :controller do
end
it 'sets pagination header for next path' do
- blocks = 2.times.map { Fabricate(:block, account: user.account) }
+ blocks = Array.new(2) { Fabricate(:block, account: user.account) }
get :index, params: { limit: 1, since_id: blocks[0] }
expect(response.headers['Link'].find_link(%w(rel next)).href).to eq api_v1_blocks_url(limit: 1, max_id: blocks[1])
end
diff --git a/spec/controllers/api/v1/mutes_controller_spec.rb b/spec/controllers/api/v1/mutes_controller_spec.rb
index 122d9d1c56..feaa486c00 100644
--- a/spec/controllers/api/v1/mutes_controller_spec.rb
+++ b/spec/controllers/api/v1/mutes_controller_spec.rb
@@ -13,13 +13,13 @@ RSpec.describe Api::V1::MutesController, type: :controller do
describe 'GET #index' do
it 'limits according to limit parameter' do
- 2.times.map { Fabricate(:mute, account: user.account) }
+ Array.new(2) { Fabricate(:mute, account: user.account) }
get :index, params: { limit: 1 }
expect(body_as_json.size).to eq 1
end
it 'queries mutes in range according to max_id' do
- mutes = 2.times.map { Fabricate(:mute, account: user.account) }
+ mutes = Array.new(2) { Fabricate(:mute, account: user.account) }
get :index, params: { max_id: mutes[1] }
@@ -28,7 +28,7 @@ RSpec.describe Api::V1::MutesController, type: :controller do
end
it 'queries mutes in range according to since_id' do
- mutes = 2.times.map { Fabricate(:mute, account: user.account) }
+ mutes = Array.new(2) { Fabricate(:mute, account: user.account) }
get :index, params: { since_id: mutes[0] }
@@ -37,7 +37,7 @@ RSpec.describe Api::V1::MutesController, type: :controller do
end
it 'sets pagination header for next path' do
- mutes = 2.times.map { Fabricate(:mute, account: user.account) }
+ mutes = Array.new(2) { Fabricate(:mute, account: user.account) }
get :index, params: { limit: 1, since_id: mutes[0] }
expect(response.headers['Link'].find_link(%w(rel next)).href).to eq api_v1_mutes_url(limit: 1, max_id: mutes[1])
end
diff --git a/spec/lib/feed_manager_spec.rb b/spec/lib/feed_manager_spec.rb
index 418bdf0898..f25b7169cc 100644
--- a/spec/lib/feed_manager_spec.rb
+++ b/spec/lib/feed_manager_spec.rb
@@ -188,7 +188,7 @@ RSpec.describe FeedManager do
it 'trims timelines if they will have more than FeedManager::MAX_ITEMS' do
account = Fabricate(:account)
status = Fabricate(:status)
- members = FeedManager::MAX_ITEMS.times.map { |count| [count, count] }
+ members = Array.new(FeedManager::MAX_ITEMS) { |count| [count, count] }
redis.zadd("feed:home:#{account.id}", members)
FeedManager.instance.push_to_home(account, status)
@@ -233,7 +233,7 @@ RSpec.describe FeedManager do
it 'does not save a new reblog of a recently-reblogged status' do
account = Fabricate(:account)
reblogged = Fabricate(:status)
- reblogs = 2.times.map { Fabricate(:status, reblog: reblogged) }
+ reblogs = Array.new(2) { Fabricate(:status, reblog: reblogged) }
# The first reblog will be accepted
FeedManager.instance.push_to_home(account, reblogs.first)
@@ -262,7 +262,7 @@ RSpec.describe FeedManager do
it 'does not save a new reblog of a multiply-reblogged-then-unreblogged status' do
account = Fabricate(:account)
reblogged = Fabricate(:status)
- reblogs = 3.times.map { Fabricate(:status, reblog: reblogged) }
+ reblogs = Array.new(3) { Fabricate(:status, reblog: reblogged) }
# Accept the reblogs
FeedManager.instance.push_to_home(account, reblogs[0])
@@ -278,7 +278,7 @@ RSpec.describe FeedManager do
it 'saves a new reblog of a long-ago-reblogged status' do
account = Fabricate(:account)
reblogged = Fabricate(:status)
- reblogs = 2.times.map { Fabricate(:status, reblog: reblogged) }
+ reblogs = Array.new(2) { Fabricate(:status, reblog: reblogged) }
# The first reblog will be accepted
FeedManager.instance.push_to_home(account, reblogs.first)
@@ -459,7 +459,7 @@ RSpec.describe FeedManager do
it 'leaves a multiply-reblogged status if another reblog was in feed' do
reblogged = Fabricate(:status)
- reblogs = 3.times.map { Fabricate(:status, reblog: reblogged) }
+ reblogs = Array.new(3) { Fabricate(:status, reblog: reblogged) }
reblogs.each do |reblog|
FeedManager.instance.push_to_home(receiver, reblog)
diff --git a/spec/lib/request_pool_spec.rb b/spec/lib/request_pool_spec.rb
index 63dc9c5dd2..395268fe43 100644
--- a/spec/lib/request_pool_spec.rb
+++ b/spec/lib/request_pool_spec.rb
@@ -33,7 +33,7 @@ describe RequestPool do
subject
- threads = 20.times.map do |_i|
+ threads = Array.new(20) do |_i|
Thread.new do
20.times do
subject.with('http://example.com') do |http_client|
diff --git a/spec/models/account_spec.rb b/spec/models/account_spec.rb
index 458b2ce52a..8c6c10c544 100644
--- a/spec/models/account_spec.rb
+++ b/spec/models/account_spec.rb
@@ -902,7 +902,7 @@ RSpec.describe Account, type: :model do
describe 'recent' do
it 'returns a relation of accounts sorted by recent creation' do
- matches = 2.times.map { Fabricate(:account) }
+ matches = Array.new(2) { Fabricate(:account) }
expect(Account.where('id > 0').recent).to match_array(matches)
end
end
--
GitLab