Class: RuboCop::Cop::Style::InverseMethods

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
IgnoredNode, RangeHelp
Defined in:
lib/rubocop/cop/style/inverse_methods.rb

Overview

This cop check for usages of not (`not` or `!`) called on a method when an inverse of that method can be used instead. Methods that can be inverted by a not (`not` or `!`) should be defined in `InverseMethods` Methods that are inverted by inverting the return of the block that is passed to the method should be defined in `InverseBlocks`

Examples:

# bad
!foo.none?
!foo.any? { |f| f.even? }
!foo.blank?
!(foo == bar)
foo.select { |f| !f.even? }
foo.reject { |f| f != 7 }

# good
foo.none?
foo.blank?
foo.any? { |f| f.even? }
foo != bar
foo == bar
!!('foo' =~ /^\w+$/)
!(foo.class < Numeric) # Checking class hierarchy is allowed
# Blocks with guard clauses are ignored:
foo.select do |f|
  next if f.zero?
  f != 1
end

Constant Summary collapse

MSG =
'Use `%<inverse>s` instead of inverting `%<method>s`.'
CLASS_COMPARISON_METHODS =
%i[<= >= < >].freeze
EQUALITY_METHODS =
%i[== != =~ !~ <= >= < >].freeze
NEGATED_EQUALITY_METHODS =
%i[!= !~].freeze
CAMEL_CASE =
/[A-Z]+[a-z]+/.freeze

Constants inherited from Base

Base::RESTRICT_ON_SEND

Constants included from Util

Util::LITERAL_REGEX

Instance Attribute Summary

Attributes inherited from Base

#config, #processed_source

Class Method Summary collapse

Instance Method Summary collapse

Methods included from AutoCorrector

support_autocorrect?

Methods included from IgnoredNode

#ignore_node, #ignored_node?, #part_of_ignored_node?

Methods inherited from Base

#add_global_offense, #add_offense, 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, #initialize, joining_forces, lint?, match?, #offenses, #on_investigation_end, #on_new_investigation, #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 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

This class inherits a constructor from RuboCop::Cop::Base

Class Method Details

.autocorrect_incompatible_withObject


47
48
49
# File 'lib/rubocop/cop/style/inverse_methods.rb', line 47

def self.autocorrect_incompatible_with
  [Style::Not, Style::SymbolProc]
end

Instance Method Details

#on_block(node) ⇒ Object


80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/rubocop/cop/style/inverse_methods.rb', line 80

def on_block(node)
  inverse_block?(node) do |_method_call, method, block|
    return unless inverse_blocks.key?(method)
    return if negated?(node) && negated?(node.parent)
    return if node.each_node(:next).any?

    # Inverse method offenses inside of the block of an inverse method
    # offense, such as `y.reject { |key, _value| !(key =~ /c\d/) }`,
    # can cause auto-correction to apply improper corrections.
    ignore_node(block)
    add_offense(node, message: message(method, inverse_blocks[method])) do |corrector|
      correct_inverse_block(corrector, node)
    end
  end
end

#on_send(node) ⇒ Object


67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/rubocop/cop/style/inverse_methods.rb', line 67

def on_send(node)
  inverse_candidate?(node) do |_method_call, lhs, method, rhs|
    return unless inverse_methods.key?(method)
    return if negated?(node)
    return if part_of_ignored_node?(node)
    return if possible_class_hierarchy_check?(lhs, rhs, method)

    add_offense(node, message: message(method, inverse_methods[method])) do |corrector|
      correct_inverse_method(corrector, node)
    end
  end
end