[Go to site: main page, start]

Cop RuboCop docs

Cop/ActiveModelErrorsDirectManipulation

Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed
EnabledYesNo--

Cop that avoid direct manipulation of ActiveModel#errors hash, in preparation to upgrade to Rails 6.1

See https://gitlab.com/gitlab-org/gitlab/-/issues/225874

Cop/ActiveRecordAssociationReload

Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed
EnabledYesNo--

Cop that denylists the use of reload.

Cop/ActiveRecordDependent

Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed
EnabledYesNo--

Cop that prevents the use of dependent: ... in ActiveRecord models.

Examples

# bad
belongs_to :foo, dependent: :destroy

# good
belongs_to :foo # With database foreign key with cascading deletes

Cop/ActiveRecordSerialize

Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed
EnabledYesNo--

Cop that prevents the use of serialize in ActiveRecord models.

Examples

# bad
serialize :preferences

# good
# Column for each individual preference

Cop/AvoidBecomes

Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed
EnabledYesNo--

Cop that denylists the use of “.becomes(SomeConstant)”.

The use of becomes() will result in a new object being created, throwing away any eager loaded assocations. This in turn can cause N+1 query problems, even when a developer eager loaded all necessary associations.

See https://gitlab.com/gitlab-org/gitlab/-/issues/23182 for more information.

Cop/AvoidBreakFromStrongMemoize

Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed
EnabledYesNo--

Checks for break inside strong_memoize blocks. For more information see: https://gitlab.com/gitlab-org/gitlab-foss/issues/42889

Examples

# bad
strong_memoize(:result) do
  break if something

  do_an_heavy_calculation
end

# good
strong_memoize(:result) do
  next if something

  do_an_heavy_calculation
end

Cop/AvoidKeywordArgumentsInSidekiqWorkers

Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed
EnabledYesNo--

Cop that denylists keyword arguments usage in Sidekiq workers

Cop/AvoidReturnFromBlocks

Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed
EnabledYesNo--

Checks for return inside blocks. For more information see: https://gitlab.com/gitlab-org/gitlab-foss/issues/42889

Examples

# bad
call do
  return if something

  do_something_else
end

# good
call do
  break if something

  do_something_else
end

Cop/AvoidRouteRedirectLeadingSlash

Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed
EnabledYesAlways--

Checks for a leading ‘/’ in route redirects For more information see: https://gitlab.com/gitlab-org/gitlab-foss/issues/50645

Examples

# bad
root to: redirect('/-/autocomplete/users')

# good
root to: redirect('-/autocomplete/users')

Cop/BanCatchThrow

Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed
EnabledYesNo--

Bans the use of ‘catch/throw’, as exceptions are better for errors and they are equivalent to ‘goto’ for flow control, with all the problems that implies.

Examples

# bad
catch(:error) do
  throw(:error)
end

# good
begin
  raise StandardError
rescue StandardError => err
  # ...
end

Cop/CustomErrorClass

Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed
EnabledYesAlways--

Makes sure that custom error classes, when empty, are declared with Class.new.

Examples

# bad
class FooError < StandardError
end

# okish
class FooError < StandardError; end

# good
FooError = Class.new(StandardError)

Cop/DefaultScope

Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed
EnabledYesNo--

Cop that denylists the use of default_scope.

Cop/DestroyAll

Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed
EnabledYesNo--

Cop that denylists the use of destroy_all.

destroy_all loads all rows into memory and executes a DELETE for each individual row, which is inefficient. Use delete_all instead for better performance.

Examples

# bad
User.where(active: false).destroy_all
@users.destroy_all

# good
User.where(active: false).delete_all
@users.delete_all

Cop/ExperimentsTestCoverage

Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed
EnabledYesNo--

Check for test coverage for GitLab experiments.

Cop/FeatureFlagUsage

Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed
EnabledYesNo--

No documentation

Cop/FileDecompression

Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed
EnabledYesNo--

Check for symlinks when extracting files to avoid arbitrary file reading.

Cop/FilenameLength

Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed
EnabledYesNo--

No documentation

Cop/GemFetcher

Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed
EnabledYesNo--

Prevents usage of the git and github arguments to gem in a Gemfile in order to avoid additional points of failure beyond rubygems.org.

Examples

# bad
gem 'rack', git: 'https://github.com/rack/rack'

# good
gem "rack"

Cop/GroupPublicOrVisibleToUser

Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed
EnabledYesNo--

Cop that denylists the usage of Group.public_or_visible_to_user

Cop/IgnoredColumns

Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed
EnabledYesNo--

Cop that flags the usage of ActiveRecord::Base.ignored_columns= directly

Examples

# bad
class User < ApplicationRecord
  self.ignored_columns = [:name]
  self.ignored_columns += [:full_name]
end

# good
class User < ApplicationRecord
  ignore_column :name, remove_after: '2023-05-22', remove_with: '16.0'
  ignore_column :full_name, remove_after: '2023-05-22', remove_with: '16.0'
end

Cop/InBatches

Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed
EnabledYesNo--

Cop that prevents the use of in_batches

Examples

# bad
Foo.in_batches do |relation|
end

# good
Foo.each_batch do |relation|
end

Cop/IncludeSidekiqWorker

Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed
EnabledYesAlways--

Cop that makes sure workers include ApplicationWorker, not Sidekiq::Worker.

Cop/InjectEnterpriseEditionModule

Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed
EnabledYesAlways--

Cop that denylists the injecting of extension specific modules before any lines which are not already injecting another module. It allows multiple module injections as long as they’re all at the end.

Cop/LineBreakAfterGuardClauses

Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed
EnabledYesAlways--

Ensures a line break after guard clauses.

Examples

# bad
return unless condition
do_stuff

# good
return unless condition

do_stuff

# bad
raise if condition
do_stuff

# good
raise if condition

do_stuff

Multiple guard clauses are allowed without
line break.

# good
return unless condition_a
return unless condition_b

do_stuff

Guard clauses in case statement are allowed without
line break.

# good
case model
  when condition_a
    return true unless condition_b
  when
    ...
end

Guard clauses before end are allowed without
line break.

# good
if condition_a
  do_something
else
  do_something_else
  return unless condition
end

do_something_more

Cop/LineBreakAroundConditionalBlock

Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed
EnabledYesAlways--

Ensures a line break around conditional blocks.

Examples

# bad
do_something
if condition
  do_extra_stuff
end
do_something_more

# good
do_something

if condition
  do_extra_stuff
end

do_something_more

# bad
do_something
unless condition
  do_extra_stuff
end

do_something_more

# good
def a_method
  if condition
    do_something
  end
end

# good
on_block do
  if condition
    do_something
  end
end

Cop/PolymorphicAssociations

Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed
EnabledYesNo--

Cop that prevents the use of polymorphic associations

Examples

# bad
class Comment < ApplicationRecord
  belongs_to :commentable, polymorphic: true
end

# good
class Comment < ApplicationRecord
  belongs_to :post
  belongs_to :article
end

Cop/PreferClassMethodsOverModule

Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed
EnabledYesAlways--

Enforces the use of ‘class_methods’ instead of ‘module ClassMethods’ for activesupport concerns. For more information see: https://gitlab.com/gitlab-org/gitlab-foss/issues/50414

Examples

# bad
module Foo
  extend ActiveSupport::Concern

  module ClassMethods
    def a_class_method
    end
  end
end

# good
module Foo
  extend ActiveSupport::Concern

  class_methods do
    def a_class_method
    end
  end
end

Cop/ProjectPathHelper

Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed
EnabledYesAlways--

No documentation

Cop/PutGroupRoutesUnderScope

Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed
EnabledYesNo--

Checks for a group routes outside ‘/-/’ scope. For more information see: https://gitlab.com/gitlab-org/gitlab/issues/29572

Cop/PutProjectRoutesUnderScope

Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed
EnabledYesNo--

Checks for a project routes outside ‘/-/’ scope. For more information see: https://gitlab.com/gitlab-org/gitlab/issues/29572

Cop/RedirectWithStatus

Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed
EnabledYesNo--

Prevents usage of ‘redirect_to’ in actions ‘destroy’ and ‘destroy_all’ without specifying ‘status’.

See https://gitlab.com/gitlab-org/gitlab-ce/issues/31840

Examples

# bad

def destroy
  redirect_to root_path
end

def destroy_all
  redirect_to root_path, alert: 'Oh no!'
end

# good

def destroy
  redirect_to root_path, status: 302
end

def destroy_all
  redirect_to root_path, alert: 'Oh no!', status: 302
end

def show
  redirect_to root_path
end

Cop/RedisQueueUsage

Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed
EnabledYesNo--

This class complements Rubocop::Cop::SidekiqRedisCall by disallowing the use of Gitlab::Redis::Queues with the exception of initialising Sidekiq and monitoring.

Cop/SafeParams

Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed
EnabledYesNo--

No documentation

Cop/SidekiqApiUsage

Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed
EnabledYesNo--

No documentation

Cop/SidekiqOptionsQueue

Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed
EnabledYesNo--

Cop that prevents manually setting a queue in Sidekiq workers.

Cop/SidekiqRedisCall

Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed
EnabledYesNo--

Cop that prevents manually setting a queue in Sidekiq workers.

Cop/StaticTranslationDefinition

Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed
EnabledYesNo--

This cop flags translation definitions in static scopes because changing locales has no effect and won’t translate this text again.

See https://docs.gitlab.com/ee/development/i18n/externalization.html#keep-translations-dynamic

Examples

# bad
class MyExample
  # Constant
  Translation = _('A translation.')

  # Class scope
  field :foo, title: _('A title')

  validates :title, :presence, message: _('is missing')

  # Memoized
  def self.translations
    @cached ||= { text: _('A translation.') }
  end

  included do # or prepended or class_methods
    self.error_message = _('Something went wrong.')
  end
end

# good
class MyExample
  # Keep translations dynamic.
  Translation = -> { _('A translation.') }
  # OR
  def translation
    _('A translation.')
  end

  field :foo, title: -> { _('A title') }

  validates :title, :presence, message: -> { _('is missing') }

  def self.translations
    { text: _('A translation.') }
  end

  included do # or prepended or class_methods
    self.error_message = -> { _('Something went wrong.') }
  end
end

Cop/UserAdmin

Enabled by defaultSafeSupports autocorrectionVersion AddedVersion Changed
EnabledYesNo--

Cop that rejects the usage of User#admin?