Class: RuboCop::Cop::Style::UnlessLogicalOperators

Inherits:
Base
  • Object
show all
Includes:
ConfigurableEnforcedStyle
Defined in:
lib/rubocop/cop/style/unless_logical_operators.rb

Overview

Checks for the use of logical operators in an ‘unless` condition. It discourages such code, as the condition becomes more difficult to read and understand.

This cop supports two styles:

  • ‘forbid_mixed_logical_operators` (default)

  • ‘forbid_logical_operators`

‘forbid_mixed_logical_operators` style forbids the use of more than one type of logical operators. This makes the `unless` condition easier to read because either all conditions need to be met or any condition need to be met in order for the expression to be truthy or falsey.

‘forbid_logical_operators` style forbids any use of logical operator. This makes it even more easy to read the `unless` condition as there is only one condition in the expression.

Examples:

EnforcedStyle: forbid_mixed_logical_operators (default)

# bad
return unless a || b && c
return unless a && b || c
return unless a && b and c
return unless a || b or c
return unless a && b or c
return unless a || b and c

# good
return unless a && b && c
return unless a || b || c
return unless a and b and c
return unless a or b or c
return unless a?

EnforcedStyle: forbid_logical_operators

# bad
return unless a || b
return unless a && b
return unless a or b
return unless a and b

# good
return unless a
return unless a?

Constant Summary collapse

FORBID_MIXED_LOGICAL_OPERATORS =
'Do not use mixed logical operators in an `unless`.'
FORBID_LOGICAL_OPERATORS =
'Do not use any logical operator in an `unless`.'

Constants inherited from Base

Base::RESTRICT_ON_SEND

Instance Attribute Summary

Attributes inherited from Base

#config, #processed_source

Instance Method Summary collapse

Methods included from ConfigurableEnforcedStyle

#alternative_style, #alternative_styles, #ambiguous_style_detected, #correct_style_detected, #detected_style, #detected_style=, #no_acceptable_style!, #no_acceptable_style?, #opposite_style_detected, #style, #style_configured?, #style_detected, #style_parameter_name, #supported_styles, #unexpected_style_detected

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, #initialize, #inspect, joining_forces, lint?, match?, #message, #offenses, #on_investigation_end, #on_new_investigation, #on_other_file, #parse, #parser_engine, #ready, #relevant_file?, requires_gem, 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

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

Instance Method Details

#and_with_or?(node) ⇒ Object



62
63
64
# File 'lib/rubocop/cop/style/unless_logical_operators.rb', line 62

def_node_matcher :and_with_or?, <<~PATTERN
  (if (and <`or ...> ) ...)
PATTERN

#logical_operator?(node) ⇒ Object



67
68
69
# File 'lib/rubocop/cop/style/unless_logical_operators.rb', line 67

def_node_matcher :logical_operator?, <<~PATTERN
  (if ({and or} ... ) ...)
PATTERN

#on_if(node) ⇒ Object



71
72
73
74
75
76
77
78
79
# File 'lib/rubocop/cop/style/unless_logical_operators.rb', line 71

def on_if(node)
  return unless node.unless?

  if style == :forbid_mixed_logical_operators && mixed_logical_operator?(node)
    add_offense(node, message: FORBID_MIXED_LOGICAL_OPERATORS)
  elsif style == :forbid_logical_operators && logical_operator?(node)
    add_offense(node, message: FORBID_LOGICAL_OPERATORS)
  end
end

#or_with_and?(node) ⇒ Object



57
58
59
# File 'lib/rubocop/cop/style/unless_logical_operators.rb', line 57

def_node_matcher :or_with_and?, <<~PATTERN
  (if (or <`and ...> ) ...)
PATTERN