Commit 4951b552 authored by Thong Kuah's avatar Thong Kuah

Merge branch '228858-resolve-style-numericpredicate-exceptions' into 'master'

[RUN AS-IF-FOSS] Resolve Style/NumericPredicate Cop exceptions

Closes #228858

See merge request gitlab-org/gitlab!38704
parents 92571fb2 c58ce73c
......@@ -443,6 +443,11 @@ RSpec/HaveGitlabHttpStatus:
Style/MultilineWhenThen:
Enabled: false
# We use EnforcedStyle of comparison here due to it being better
# performing code as seen in https://gitlab.com/gitlab-org/gitlab/-/merge_requests/36221#note_375659681
Style/NumericPredicate:
EnforcedStyle: comparison
Style/FloatDivision:
Enabled: false
......
......@@ -596,66 +596,6 @@ Style/Next:
Style/NumericLiteralPrefix:
Enabled: false
# Offense count: 130
# Cop supports --auto-correct.
# Configuration parameters: AutoCorrect, EnforcedStyle, IgnoredMethods.
# SupportedStyles: predicate, comparison
# We use EnforcedStyle of comparison here due to it being better
# performing code as seen https://gitlab.com/gitlab-org/gitlab/-/merge_requests/36221#note_375659681
Style/NumericPredicate:
EnforcedStyle: comparison
Exclude:
- 'app/controllers/concerns/issuable_collections.rb'
- 'app/controllers/concerns/paginated_collection.rb'
- 'app/helpers/graph_helper.rb'
- 'app/helpers/timeboxes_helper.rb'
- 'app/models/ci/pipeline.rb'
- 'app/models/ci/stage.rb'
- 'app/models/concerns/update_project_statistics.rb'
- 'app/models/merge_request_diff.rb'
- 'app/models/milestone.rb'
- 'app/models/network/graph.rb'
- 'app/models/postgresql/replication_slot.rb'
- 'app/models/project.rb'
- 'app/models/suggestion.rb'
- 'app/models/user.rb'
- 'app/serializers/merge_request_widget_entity.rb'
- 'app/services/boards/issues/move_service.rb'
- 'app/services/cohorts_service.rb'
- 'app/services/discussions/resolve_service.rb'
- 'app/services/issues/reorder_service.rb'
- 'app/services/notes/create_service.rb'
- 'app/services/packages/nuget/metadata_extraction_service.rb'
- 'app/services/packages/nuget/search_service.rb'
- 'app/services/projects/auto_devops/disable_service.rb'
- 'app/services/projects/update_pages_service.rb'
- 'app/services/search_service.rb'
- 'app/workers/admin_email_worker.rb'
- 'app/workers/gitlab/import/advance_stage.rb'
- 'config/initializers/validate_puma.rb'
- 'ee/app/controllers/security/projects_controller.rb'
- 'ee/app/graphql/mutations/instance_security_dashboard/remove_project.rb'
- 'ee/app/helpers/ee/timeboxes_helper.rb'
- 'ee/app/models/ci/minutes/quota.rb'
- 'ee/app/models/ee/ci/runner.rb'
- 'ee/app/models/geo_node_status.rb'
- 'ee/app/models/license.rb'
- 'ee/app/models/namespace_statistics.rb'
- 'ee/app/services/ee/issues/base_service.rb'
- 'ee/app/services/ee/merge_requests/approval_service.rb'
- 'ee/app/services/ee/quick_actions/target_service.rb'
- 'ee/app/services/elastic/indexing_control_service.rb'
- 'ee/app/services/geo/hashed_storage_migration_service.rb'
- 'ee/app/services/geo/prune_event_log_service.rb'
- 'ee/app/services/security/waf_anomaly_summary_service.rb'
- 'ee/app/services/update_build_minutes_service.rb'
- 'ee/app/workers/geo/container_repository_sync_dispatch_worker.rb'
- 'ee/app/workers/geo/file_download_dispatch_worker.rb'
- 'ee/app/workers/geo/registry_sync_worker.rb'
- 'ee/app/workers/geo/repository_shard_sync_worker.rb'
- 'ee/app/workers/geo/repository_verification/primary/shard_worker.rb'
- 'ee/app/models/ee/project.rb'
# Offense count: 117
# Cop supports --auto-correct.
Style/ParallelAssignment:
......
......@@ -65,7 +65,7 @@ module IssuableCollections
def page_count_for_relation(relation, row_count)
limit = relation.limit_value.to_f
return 1 if limit.zero?
return 1 if limit == 0
(row_count.to_f / limit).ceil
end
......
......@@ -6,7 +6,7 @@ module PaginatedCollection
private
def redirect_out_of_range(collection, total_pages = collection.total_pages)
return false if total_pages.zero?
return false if total_pages == 0
out_of_range = collection.current_page > total_pages
......
......@@ -17,7 +17,7 @@ module GraphHelper
end
def success_ratio(counts)
return 100 if counts[:failed].zero?
return 100 if counts[:failed] == 0
ratio = (counts[:success].to_f / (counts[:success] + counts[:failed])) * 100
ratio.to_i
......
......@@ -155,7 +155,7 @@ module TimeboxesHelper
opened = milestone.opened_issues_count
closed = milestone.closed_issues_count
return _("Issues") if total.zero?
return _("Issues") if total == 0
content = []
......@@ -187,7 +187,7 @@ module TimeboxesHelper
def milestone_releases_tooltip_text(milestone)
count = milestone.releases.count
return _("Releases") if count.zero?
return _("Releases") if count == 0
n_("%{releases} release", "%{releases} releases", count) % { releases: count }
end
......
......@@ -409,7 +409,7 @@ module Ci
def legacy_stage(name)
stage = Ci::LegacyStage.new(self, name: name)
stage unless stage.statuses_count.zero?
stage unless stage.statuses_count == 0
end
def ref_exists?
......@@ -658,7 +658,7 @@ module Ci
end
def has_warnings?
number_of_warnings.positive?
number_of_warnings > 0
end
def number_of_warnings
......@@ -821,7 +821,7 @@ module Ci
return unless started_at
seconds = (started_at - created_at).to_i
seconds unless seconds.zero?
seconds unless seconds == 0
end
def update_duration
......
......@@ -113,7 +113,7 @@ module Ci
end
def has_warnings?
number_of_warnings.positive?
number_of_warnings > 0
end
def number_of_warnings
......
......@@ -75,7 +75,7 @@ module UpdateProjectStatistics
end
def schedule_update_project_statistic(delta)
return if delta.zero?
return if delta == 0
return if project.nil?
run_after_commit do
......
......@@ -631,7 +631,7 @@ class MergeRequestDiff < ApplicationRecord
def save_diffs
new_attributes = {}
if compare.commits.size.zero?
if compare.commits.empty?
new_attributes[:state] = :empty
else
diff_collection = compare.diffs(Commit.max_diff_options)
......
......@@ -127,7 +127,7 @@ class Milestone < ApplicationRecord
end
def can_be_closed?
active? && issues.opened.count.zero?
active? && issues.opened.count == 0
end
def author_id
......
......@@ -211,7 +211,7 @@ module Network
# Visit branching chains
leaves.each do |l|
parents = l.parents(@map).select {|p| p.space.zero?}
parents = l.parents(@map).select {|p| p.space == 0}
parents.each do |p|
place_chain(p, l.time)
end
......@@ -266,14 +266,14 @@ module Network
def take_left_leaves(raw_commit)
commit = @map[raw_commit.id]
leaves = []
leaves.push(commit) if commit.space.zero?
leaves.push(commit) if commit.space == 0
loop do
return leaves if commit.parents(@map).count.zero?
return leaves if commit.parents(@map).count == 0
commit = commit.parents(@map).first
return leaves unless commit.space.zero?
return leaves unless commit.space == 0
leaves.push(commit)
end
......
......@@ -33,7 +33,7 @@ module Postgresql
# If too many replicas are falling behind too much, the availability of a
# GitLab instance might suffer. To prevent this from happening we require
# at least 1 replica to have data recent enough.
if sizes.any? && too_great.positive?
if sizes.any? && too_great > 0
(sizes.length - too_great) <= 1
else
false
......
......@@ -1121,7 +1121,7 @@ class Project < ApplicationRecord
limit = creator.projects_limit
error =
if limit.zero?
if limit == 0
_('Personal project creation is not allowed. Please contact your administrator with questions')
else
_('Your project limit is %{limit} projects! Please contact your administrator to increase it')
......
......@@ -61,7 +61,7 @@ class Suggestion < ApplicationRecord
end
def single_line?
lines_above.zero? && lines_below.zero?
lines_above == 0 && lines_below == 0
end
def target_line
......
......@@ -962,7 +962,7 @@ class User < ApplicationRecord
def require_ssh_key?
count = Users::KeysCountService.new(self).count
count.zero? && Gitlab::ProtocolAccess.allowed?('ssh')
count == 0 && Gitlab::ProtocolAccess.allowed?('ssh')
end
# rubocop: enable CodeReuse/ServiceClass
......
......@@ -137,7 +137,7 @@ class MergeRequestWidgetEntity < Grape::Entity
merge_request.source_branch_exists? &&
merge_request.source_project&.uses_default_ci_config? &&
!merge_request.source_project.has_ci? &&
merge_request.commits_count.positive? &&
merge_request.commits_count > 0 &&
can?(current_user, :read_build, merge_request.source_project) &&
can?(current_user, :create_pipeline, merge_request.source_project)
end
......
......@@ -130,7 +130,7 @@ module Boards
def move_between_ids(move_params)
ids = [move_params[:move_after_id], move_params[:move_before_id]]
.map(&:to_i)
.map { |m| m.positive? ? m : nil }
.map { |m| m > 0 ? m : nil }
ids.any? ? ids : nil
end
......
......@@ -63,7 +63,7 @@ class CohortsService
overall_total = month_totals.first
month_totals.map do |total|
{ total: total, percentage: total.zero? ? 0 : 100 * total / overall_total }
{ total: total, percentage: total == 0 ? 0 : 100 * total / overall_total }
end
end
......
......@@ -56,7 +56,7 @@ module Discussions
def process_auto_merge
return unless merge_request
return unless @resolved_count.positive?
return unless @resolved_count > 0
return unless discussions_ready_to_merge?
AutoMergeProcessWorker.perform_async(merge_request.id)
......
......@@ -40,7 +40,7 @@ module Issues
def move_between_ids
ids = [params[:move_after_id], params[:move_before_id]]
.map(&:to_i)
.map { |m| m.positive? ? m : nil }
.map { |m| m > 0 ? m : nil }
ids.any? ? ids : nil
end
......
......@@ -72,7 +72,7 @@ module Notes
end
def do_commands(note, update_params, message, only_commands)
return if quick_actions_service.commands_executed_count.to_i.zero?
return if quick_actions_service.commands_executed_count.to_i == 0
if update_params.present?
quick_actions_service.apply_updates(update_params, note)
......
......@@ -42,7 +42,7 @@ module Packages
def valid_package_file?
package_file &&
package_file.package&.nuget? &&
package_file.file.size.positive?
package_file.file.size > 0 # rubocop:disable Style/ZeroLengthPredicate
end
def extract_metadata(file)
......
......@@ -21,8 +21,8 @@ module Packages
@search_term = search_term
@options = DEFAULT_OPTIONS.merge(options)
raise ArgumentError, 'negative per_page' if per_page.negative?
raise ArgumentError, 'negative padding' if padding.negative?
raise ArgumentError, 'negative per_page' if per_page < 0
raise ArgumentError, 'negative padding' if padding < 0
end
def execute
......
......@@ -23,7 +23,7 @@ module Projects
# for more context.
# rubocop: disable CodeReuse/ActiveRecord
def first_pipeline_failure?
auto_devops_pipelines.success.limit(1).count.zero? &&
auto_devops_pipelines.success.limit(1).count == 0 &&
auto_devops_pipelines.failed.limit(1).count.nonzero?
end
# rubocop: enable CodeReuse/ActiveRecord
......
......@@ -136,7 +136,7 @@ module Projects
def max_size
max_pages_size = max_size_from_settings
return ::Gitlab::Pages::MAX_SIZE if max_pages_size.zero?
return ::Gitlab::Pages::MAX_SIZE if max_pages_size == 0
max_pages_size
end
......
......@@ -70,7 +70,7 @@ class SearchService
def per_page
per_page_param = params[:per_page].to_i
return DEFAULT_PER_PAGE unless per_page_param.positive?
return DEFAULT_PER_PAGE unless per_page_param > 0
[MAX_PER_PAGE, per_page_param].min
end
......
......@@ -18,7 +18,7 @@ class AdminEmailWorker # rubocop:disable Scalability/IdempotentWorker
# rubocop: disable CodeReuse/ActiveRecord
def send_repository_check_mail
repository_check_failed_count = Project.where(last_repository_check_failed: true).count
return if repository_check_failed_count.zero?
return if repository_check_failed_count == 0
RepositoryCheckMailer.notify(repository_check_failed_count).deliver_now
end
......
......@@ -41,7 +41,7 @@ module Gitlab
# complete the work fast enough.
waiter.wait(BLOCKING_WAIT_TIME)
next unless waiter.jobs_remaining.positive?
next unless waiter.jobs_remaining > 0
new_waiters[waiter.key] = waiter.jobs_remaining
end
......
# frozen_string_literal: true
if Gitlab::Runtime.puma? && ::Puma.cli_config.options[:workers].to_i.zero?
if Gitlab::Runtime.puma? && ::Puma.cli_config.options[:workers].to_i == 0
raise 'Puma is only supported in Cluster-mode: workers > 0'
end
......@@ -25,7 +25,7 @@ module Security
end
def destroy
if !remove_project.zero?
if remove_project != 0
head :ok
else
head :no_content
......
......@@ -18,7 +18,7 @@ module Mutations
result = remove_project(extract_project_id(id))
{
errors: result.zero? ? ['The project does not belong to your dashboard or you don\'t have permission to perform this action'] : []
errors: result == 0 ? ['The project does not belong to your dashboard or you don\'t have permission to perform this action'] : []
}
end
......
......@@ -44,7 +44,7 @@ module EE
end
def milestone_weight_tooltip_text(weight)
if weight.zero?
if weight == 0
_("Weight")
else
_("Weight %{weight}") % { weight: weight }
......
......@@ -25,7 +25,7 @@ module Ci
def monthly_percent_used
return 0 unless namespace.shared_runners_minutes_limit_enabled?
return 0 if monthly_minutes.zero?
return 0 if monthly_minutes == 0
100 * monthly_minutes_used.to_i / monthly_minutes
end
......@@ -38,7 +38,7 @@ module Ci
def purchased_percent_used
return 0 unless namespace.shared_runners_minutes_limit_enabled?
return 0 if purchased_minutes.zero?
return 0 if purchased_minutes == 0
100 * purchased_minutes_used.to_i / purchased_minutes
end
......@@ -75,11 +75,11 @@ module Ci
end
def no_minutes_purchased?
purchased_minutes.zero?
purchased_minutes == 0
end
def any_minutes_purchased?
purchased_minutes.positive?
purchased_minutes > 0
end
# TODO: maps to NamespaceStatistics#shared_runners_minutes(include_extra: true)
......
......@@ -26,7 +26,7 @@ module EE
def visibility_levels_without_minutes_quota
::Gitlab::VisibilityLevel.options.values.reject do |visibility_level|
minutes_cost_factor(visibility_level).positive?
minutes_cost_factor(visibility_level) > 0
end
end
......
......@@ -631,7 +631,7 @@ module EE
def adjourned_deletion?
feature_available?(:adjourned_deletion_for_projects_and_groups) &&
::Gitlab::CurrentSettings.deletion_adjourned_period.positive? &&
::Gitlab::CurrentSettings.deletion_adjourned_period > 0 &&
group_deletion_mode_configured?
end
......
......@@ -356,7 +356,7 @@ class GeoNodeStatus < ApplicationRecord
@percentage_methods << method_name
define_method(method_name) do
return 0 if self[total].to_i.zero?
return 0 if self[total].to_i == 0
(self[count].to_f / self[total].to_f) * 100.0
end
......
......@@ -543,9 +543,9 @@ class License < ApplicationRecord
return if restricted_user_count >= prior_historical_max
end
user_count = prior_historical_max.zero? ? current_active_users_count : prior_historical_max
user_count = prior_historical_max == 0 ? current_active_users_count : prior_historical_max
add_limit_error(current_period: prior_historical_max.zero?, user_count: user_count)
add_limit_error(current_period: prior_historical_max == 0, user_count: user_count)
end
def check_trueup
......
......@@ -18,7 +18,7 @@ class NamespaceStatistics < ApplicationRecord
limit = namespace.actual_shared_runners_minutes_limit(include_extra: false)
extra_limit = namespace.extra_shared_runners_minutes_limit.to_i
return 0 if extra_limit.zero? || shared_runners_minutes <= limit
return 0 if extra_limit == 0 || shared_runners_minutes <= limit
shared_runners_minutes - limit
end
......
......@@ -26,7 +26,7 @@ module EE
def find_epic(issue)
id = params.delete(:epic_id)
return if id.to_i.zero?
return if id.to_i == 0
group = issue.project.group
return unless group.present?
......
......@@ -28,7 +28,7 @@ module EE
override :execute_approval_hooks
def execute_approval_hooks(merge_request, current_user)
if merge_request.approvals_left.zero?
if merge_request.approvals_left == 0
notification_service.async.approve_mr(merge_request, current_user)
execute_hooks(merge_request, 'approved')
else
......
......@@ -4,7 +4,7 @@ module EE
module QuickActions
module TargetService
def execute(type, type_id)
return epic(type_id) if type.casecmp('epic').zero?
return epic(type_id) if type.casecmp('epic') == 0
super
end
......
......@@ -65,7 +65,7 @@ module Elastic
remove_jobs_from_waiting_queue(redis, jobs_with_scores)
end
redis.del(redis_set_key, redis_score_key) if queue_size.zero?
redis.del(redis_set_key, redis_score_key) if queue_size == 0
end
end
......
......@@ -46,7 +46,7 @@ module Geo
end
def from_legacy_storage?
old_storage_version.nil? || old_storage_version.zero?
old_storage_version.nil? || old_storage_version == 0
end
def move_repository
......
......@@ -46,7 +46,7 @@ module Geo
deleted = relation.delete_with_limit(limit)
if deleted.positive?
if deleted > 0
log_info('Rows pruned from Geo Event log',
relation: relation.name,
rows_deleted: deleted,
......
......@@ -33,7 +33,7 @@ module Security
return { total_traffic: nginx_total_requests, total_anomalous_traffic: modsec_total_requests } if totals_only
anomalous_traffic_count = nginx_total_requests.zero? ? 0 : (modsec_total_requests / nginx_total_requests).round(2)
anomalous_traffic_count = nginx_total_requests == 0 ? 0 : (modsec_total_requests / nginx_total_requests).round(2)
{
total_traffic: nginx_total_requests,
......
......@@ -15,7 +15,7 @@ class UpdateBuildMinutesService < BaseService
cost_factor = build.runner.minutes_cost_factor(project.visibility_level)
duration_with_cost_factor = (build.duration * cost_factor).to_i
return unless duration_with_cost_factor.positive?
return unless duration_with_cost_factor > 0
ProjectStatistics.update_counters(project_statistics,
shared_runners_seconds: duration_with_cost_factor)
......
......@@ -39,7 +39,7 @@ module Geo
resources = find_container_repository_ids_not_synced(batch_size: db_retrieve_batch_size)
remaining_capacity = db_retrieve_batch_size - resources.size
if remaining_capacity.zero?
if remaining_capacity == 0
resources
else
resources + find_retryable_container_registry_ids(batch_size: remaining_capacity)
......
......@@ -36,7 +36,7 @@ module Geo
resources = find_unsynced_jobs(batch_size: db_retrieve_batch_size)
remaining_capacity = db_retrieve_batch_size - resources.count
if remaining_capacity.zero?
if remaining_capacity == 0
resources
else
resources + find_low_priority_jobs(batch_size: remaining_capacity)
......
......@@ -35,7 +35,7 @@ module Geo
resources = find_unsynced_jobs(batch_size: db_retrieve_batch_size)
remaining_capacity = db_retrieve_batch_size - resources.count
if remaining_capacity.zero?
if remaining_capacity == 0
resources
else
resources + find_low_priority_jobs(batch_size: remaining_capacity)
......
......@@ -67,7 +67,7 @@ module Geo
resources = find_project_ids_not_synced(except_ids: scheduled_project_ids, batch_size: db_retrieve_batch_size)
remaining_capacity = db_retrieve_batch_size - resources.size
if remaining_capacity.zero?
if remaining_capacity == 0
resources
else
resources + find_project_ids_updated_recently(except_ids: scheduled_project_ids + resources, batch_size: remaining_capacity)
......
......@@ -49,11 +49,11 @@ module Geo
def load_pending_resources
resources = find_never_verified_project_ids(batch_size: db_retrieve_batch_size)
remaining_capacity = db_retrieve_batch_size - resources.size
return resources if remaining_capacity.zero?
return resources if remaining_capacity == 0
resources += find_recently_updated_project_ids(batch_size: remaining_capacity)
remaining_capacity = db_retrieve_batch_size - resources.size
return resources if remaining_capacity.zero?
return resources if remaining_capacity == 0
resources + find_project_ids_to_reverify(batch_size: remaining_capacity)
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment