Class: RuboCop::Cop::Lint::RedundantCopDisableDirective

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
RangeHelp
Defined in:
lib/rubocop/cop/lint/redundant_cop_disable_directive.rb

Overview

Detects instances of rubocop:disable comments that can be removed without causing any offenses to be reported. It’s implemented as a cop in that it inherits from the Cop base class and calls add_offense. The unusual part of its implementation is that it doesn’t have any on_* methods or an investigate method. This means that it doesn’t take part in the investigation phase when the other cops do their work. Instead, it waits until it’s called in a later stage of the execution. The reason it can’t be implemented as a normal cop is that it depends on the results of all other cops to do its work.

Examples:

# bad
# rubocop:disable Layout/LineLength
x += 1
# rubocop:enable Layout/LineLength

# good
x += 1

Constant Summary collapse

COP_NAME =
'Lint/RedundantCopDisableDirective'
DEPARTMENT_MARKER =
'DEPARTMENT'

Constants inherited from Base

Base::RESTRICT_ON_SEND

Instance Attribute Summary collapse

Attributes inherited from Base

#config, #processed_source

Instance Method Summary collapse

Methods included from AutoCorrector

support_autocorrect?

Methods inherited from Base

#active_support_extensions_enabled?, #add_global_offense, #add_offense, #always_autocorrect?, autocorrect_incompatible_with, badge, #begin_investigation, #callbacks_needed, callbacks_needed, #config_to_allow_offenses, #config_to_allow_offenses=, #contextual_autocorrect?, #cop_config, #cop_name, cop_name, department, documentation_url, exclude_from_registry, #excluded_file?, #external_dependency_checksum, inherited, #inspect, joining_forces, lint?, match?, #offenses, #on_investigation_end, #on_other_file, #parse, #parser_engine, #ready, #relevant_file?, support_autocorrect?, support_multiple_source?, #target_rails_version, #target_ruby_version

Methods included from ExcludeLimit

#exclude_limit

Methods included from AutocorrectLogic

#autocorrect?, #autocorrect_enabled?, #autocorrect_requested?, #autocorrect_with_disable_uncorrectable?, #correctable?, #disable_uncorrectable?, #safe_autocorrect?

Methods included from IgnoredNode

#ignore_node, #ignored_node?, #part_of_ignored_node?

Methods included from Util

silence_warnings

Constructor Details

#initialize(config = nil, options = nil, offenses = nil) ⇒ RedundantCopDisableDirective

Returns a new instance of RedundantCopDisableDirective.



37
38
39
40
# File 'lib/rubocop/cop/lint/redundant_cop_disable_directive.rb', line 37

def initialize(config = nil, options = nil, offenses = nil)
  @offenses_to_check = offenses
  super(config, options)
end

Instance Attribute Details

#offenses_to_checkObject

Returns the value of attribute offenses_to_check.



35
36
37
# File 'lib/rubocop/cop/lint/redundant_cop_disable_directive.rb', line 35

def offenses_to_check
  @offenses_to_check
end

Instance Method Details

#on_new_investigationObject



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rubocop/cop/lint/redundant_cop_disable_directive.rb', line 42

def on_new_investigation
  return unless offenses_to_check

  redundant_cops = Hash.new { |h, k| h[k] = Set.new }

  each_redundant_disable do |comment, redundant_cop|
    redundant_cops[comment].add(redundant_cop)
  end

  add_offenses(redundant_cops)
  super
end