Rubocop, How to Disable/Enable Cops on Blocks of Code

Rubocop, how to Disable/Enable cops on blocks of code

I answer my question because it is always very difficult for me to find the reference to this solution:

# rubocop:disable Metrics/MethodLength
def my_code
..
end
# rubocop:enable Metrics/MethodLength

Same for multiple cops:

# rubocop:disable Metrics/AbcSize, Metrics/MethodLength
def my_code
..
end
# rubocop:enable Metrics/AbcSize, Metrics/MethodLength

Documentation: https://docs.rubocop.org/rubocop/configuration.html#disabling-cops-within-source-code

Rubocop disable rule in AllCops

It feels to me like you are affected by this issue in Rubocop. I suggest upgrading Rubocop to a newer version.

How to disable 'warning no department given for 'COP' message on running rubocop

A qualified cop name is Department/CopName. For example, Style/Documentation is qualified and Documentation is unqualified.

The documentation indicates that:

Qualifying cop name with its type, e.g., Style, is recommended, but not necessary as long as the cop name is unique across all types.

But they show a warning for unqualified names. That happens here:

# RuboCop::Cop::Registry
def qualified_cop_name(name, path, shall_warn = true)
badge = Badge.parse(name)
if shall_warn && department_missing?(badge, name)
print_warning(name, path)
end
return name if registered?(badge)

potential_badges = qualify_badge(badge)

case potential_badges.size
when 0 then name # No namespace found. Deal with it later in caller.
when 1 then resolve_badge(badge, potential_badges.first, path)
else raise AmbiguousCopName.new(badge, path, potential_badges)
end
end

shall_warn is only false when the --auto-correct option is in use. There isn't a way to disable it currently.

The only way to silence the warning is to include the department for each cop in your config like:

Style/Documentation:
Enabled: false
Style/ClassAndModuleChildren:
Enabled: false
Metrics/LineLength:
Max: 120
Style/GuardClause:
Enabled: false
Style/IfUnlessModifier:
Enabled: false

If a Rubocop rule is disabled in-line does it need to be re-enabled

In-line config is applied to the given file only (just tested it).

Rails: rubocop disable Class has too many lines error

Try

class Xzy  # rubocop:disable Metrics/ClassLength

Rubocop command to disable Rails/SkipsModelValidations: avoid using update_all error

You can run the below command, which will generate a .rubocop_todo.yml file which will record and ignore specific offences for offending files:

rubocop --auto-gen-config --exclude-limit 999 --no-offense-counts

--auto-gen-config generates the yml file, the only non-optional flag to achieve what you are looking for.

--exclude-limit xxx is a number of offence before rubocop disables the check for the entire application.

--no-offense-counts does not record a count how many offences there are in the yml file.

Ensure you've thought about the consequences of ignoring linters; it is usually ill advised to not follow the linter advice. Be aware that this will disable checks for all offences in the spec tests.

Update

If you want to disable rubocop checks without generating a file, you can use a comment like so:

Company.where(cms_tag_id: webhook.dig('entity', 'id')).update_all(is_deleted: true) # rubocop:disable Rails/SkipsModelValidations


Related Topics



Leave a reply



Submit