Cops
In RuboCop lingo the various checks performed on the code are called cops. Each cop is responsible for detecting one particular offense. There are several cop departments, grouping the cops by class of offense.
Many of the Style and Layout cops have configuration options, allowing them to
enforce different coding conventions. See Configuring Cops
for details on Enabled, Severity, AutoCorrect, AllowedMethods, and other settings.
You can also load custom cops.
Departments
Style
Style cops check for stylistic consistency of your code. Many of them are
based on the Ruby Style Guide. This is the largest
department and covers everything from preferred hash syntax to method naming
conventions. Most Style cops support multiple EnforcedStyle options so you
can adapt them to your team’s preferences.
Layout
Layout cops inspect your code for consistent use of indentation, alignment,
and white space. They deal exclusively with formatting — changing Layout cops
never affects program behavior. Running rubocop -x applies only Layout
autocorrections.
Lint
Lint cops check for ambiguities and possible errors in your code.
RuboCop implements, in a portable way, all built-in MRI lint checks
(ruby -wc) and adds a lot of extra lint checks of its own.
You can run only the Lint cops like this:
$ rubocop -l
The -l/--lint option can be used together with --only to run all the
enabled Lint cops plus a selection of other cops.
Disabling Lint cops is generally a bad idea.
Metrics
Metrics cops deal with properties of the source code that can be measured,
such as class length, method length, and cyclomatic complexity. They have a
configuration parameter called Max and when running
rubocop --auto-gen-config, this parameter will be set to the highest value
found for the inspected code.
Naming
Naming cops check for naming issues in your code, such as method names, constant names, file names, and predicate prefixes. Like Style cops, many support multiple enforced styles.
Cop metadata
Each cop in config/default.yml has metadata that helps you understand its status:
-
VersionAdded— the RuboCop version that introduced the cop -
VersionChanged— the last version that changed the cop’s behavior or defaults -
Enabled— whether the cop is enabled by default -
SafeAutoCorrect— whether the autocorrection is safe (won’t change behavior)
Cop-related errors are silenced by default but can be surfaced using the
--raise-cop-error option, which is useful for debugging.