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

This cop 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'

Constants inherited from Base

Base::RESTRICT_ON_SEND

Constants included from Util

Util::LITERAL_REGEX

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

#add_global_offense, #add_offense, autocorrect_incompatible_with, badge, #callbacks_needed, callbacks_needed, #config_to_allow_offenses, #config_to_allow_offenses=, #cop_config, cop_name, #cop_name, department, documentation_url, exclude_from_registry, #excluded_file?, #external_dependency_checksum, inherited, joining_forces, lint?, match?, #message, #offenses, #on_investigation_end, #on_other_file, #ready, #relevant_file?, support_autocorrect?, support_multiple_source?, #target_rails_version, #target_ruby_version

Methods included from AutocorrectLogic

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

Methods included from IgnoredNode

#ignore_node, #ignored_node?, #part_of_ignored_node?

Methods included from Util

add_parentheses, args_begin, args_end, begins_its_line?, comment_line?, comment_lines?, double_quotes_required?, escape_string, first_part_of_call_chain, indent, interpret_string_escapes, line_range, needs_escaping?, on_node, parentheses?, same_line?, to_string_literal, to_supported_styles, trim_string_interporation_escape_character

Methods included from PathUtil

absolute?, hidden_dir?, hidden_file?, hidden_file_in_not_hidden_dir?, match_path?, maybe_hidden_file?, relative_path, smart_path

Constructor Details

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

Returns a new instance of RedundantCopDisableDirective.


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

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


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

def offenses_to_check
  @offenses_to_check
end

Instance Method Details

#on_new_investigationObject


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

def on_new_investigation
  return unless offenses_to_check

  cop_disabled_line_ranges = processed_source.disabled_line_ranges

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

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

  add_offenses(redundant_cops)
  super
end