123456789_123456789_123456789_123456789_123456789_

Class: RuboCop::Cop::Style::IfWithBooleanLiteralBranches

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, ::RuboCop::Cop::AutoCorrector, ::RuboCop::Cop::Base, ::RuboCop::ExcludeLimit, NodePattern::Macros, RuboCop::AST::Sexp
Instance Chain:
Inherits: RuboCop::Cop::Base
Defined in: lib/rubocop/cop/style/if_with_boolean_literal_branches.rb

Overview

Checks for redundant if with boolean literal branches. It checks only conditions to return boolean value (true or false) for safe detection. The conditions to be checked are comparison methods, predicate methods, and double negation (!!). nonzero? method is allowed by default. These are customizable with ::RuboCop::Cop::AllowedMethods option.

This cop targets only `if`s with a single elsif or else branch. The following code will be allowed, because it has two elsif branches:

if foo
  true
elsif bar > baz
  true
elsif qux > quux # Single {elsif} is warned, but two or more `elsif`s are not.
  true
else
  false
end

Examples:

# bad
if foo == bar
  true
else
  false
end

# bad
foo == bar ? true : false

# good
foo == bar

# bad
if foo.do_something?
  true
else
  false
end

# good (but potentially an unsafe correction)
foo.do_something?

AllowedMethods: ['nonzero?'] (default)

# good
num.nonzero? ? true : false

Cop Safety Information:

  • Autocorrection is unsafe because there is no guarantee that all predicate methods will return a boolean value. Those methods can be allowed with ::RuboCop::Cop::AllowedMethods config.

Constant Summary

::RuboCop::Cop::Base - Inherited

EMPTY_OFFENSES, RESTRICT_ON_SEND

Class Attribute Summary

::RuboCop::Cop::AutoCorrector - Extended

::RuboCop::Cop::Base - Inherited

.gem_requirements, .lint?,
.support_autocorrect?

Returns if class supports autocorrect.

.support_multiple_source?

Override if your cop should be called repeatedly for multiple investigations Between calls to on_new_investigation and on_investigation_end, the result of processed_source will remain constant.

Class Method Summary

::RuboCop::Cop::Base - Inherited

.autocorrect_incompatible_with

List of cops that should not try to autocorrect at the same time as this cop.

.badge

Naming.

.callbacks_needed, .cop_name, .department,
.documentation_url

Returns a url to view this cops documentation online.

.exclude_from_registry

Call for abstract Cop classes.

.inherited,
.joining_forces

Override and return the Force class(es) you need to join.

.match?

Returns true if the cop name or the cop namespace matches any of the given names.

.new,
.requires_gem

Register a version requirement for the given gem name.

.restrict_on_send

::RuboCop::ExcludeLimit - Extended

exclude_limit

Sets up a configuration option to have an exclude limit tracked.

transform

Instance Attribute Summary

Instance Method Summary

::RuboCop::Cop::AllowedMethods - Included

::RuboCop::Cop::Base - Inherited

#add_global_offense

Adds an offense that has no particular location.

#add_offense

Adds an offense on the specified range (or node with an expression) Unless that offense is disabled for this range, a corrector will be yielded to provide the cop the opportunity to autocorrect the offense.

#begin_investigation

Called before any investigation.

#callbacks_needed,
#cop_config

Configuration Helpers.

#cop_name, #excluded_file?,
#external_dependency_checksum

This method should be overridden when a cop’s behavior depends on state that lives outside of these locations:

#inspect,
#message

Gets called if no message is specified when calling add_offense or add_global_offense Cops are discouraged to override this; instead pass your message directly.

#name

Alias for Base#cop_name.

#offenses,
#on_investigation_end

Called after all on_…​

#on_new_investigation

Called before all on_…​

#on_other_file

Called instead of all on_…​

#parse

There should be very limited reasons for a Cop to do it’s own parsing.

#parser_engine,
#ready

Called between investigations.

#relevant_file?,
#target_gem_version

Returns a gems locked versions (i.e.

#target_rails_version, #target_ruby_version, #annotate, #apply_correction, #attempt_correction,
#callback_argument

Reserved for Cop::Cop.

#complete_investigation

Called to complete an investigation.

#correct, #current_corrector,
#current_offense_locations

Reserved for Commissioner:

#current_offenses, #currently_disabled_lines, #custom_severity, #default_severity, #disable_uncorrectable, #enabled_line?, #file_name_matches_any?, #find_message, #find_severity, #range_for_original, #range_from_node_or_range,
#reset_investigation

Actually private methods.

#use_corrector

::RuboCop::Cop::AutocorrectLogic - Included

::RuboCop::Cop::IgnoredNode - Included

Constructor Details

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

Instance Method Details

#assume_boolean_value?(condition) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/if_with_boolean_literal_branches.rb', line 135

def assume_boolean_value?(condition)
  return false unless condition.send_type?
  return false if allowed_method?(condition.method_name)

  condition.comparison_method? || condition.predicate_method? || double_negative?(condition)
end

#double_negative?(node)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/if_with_boolean_literal_branches.rb', line 73

def_node_matcher :double_negative?, '(send (send _ :!) :!)'

#if_with_boolean_literal_branches?(node)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/if_with_boolean_literal_branches.rb', line 69

def_node_matcher :if_with_boolean_literal_branches?, <<~PATTERN
  (if #return_boolean_value? {(true) (false) | (false) (true)})
PATTERN

#message(node, keyword) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/if_with_boolean_literal_branches.rb', line 113

def message(node, keyword)
  if node.elsif?
    MSG_FOR_ELSIF
  else
    format(MSG, keyword: keyword)
  end
end

#multiple_elsif?(node) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/if_with_boolean_literal_branches.rb', line 95

def multiple_elsif?(node)
  return false unless (parent = node.parent)

  parent.if_type? && parent.elsif?
end

#offense_range_with_keyword(node, condition) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/if_with_boolean_literal_branches.rb', line 101

def offense_range_with_keyword(node, condition)
  if node.ternary?
    range = condition.source_range.end.join(node.source_range.end)

    [range, 'ternary operator']
  else
    keyword = node.loc.keyword

    [keyword, "`#{keyword.source}`"]
  end
end

#on_if(node)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/if_with_boolean_literal_branches.rb', line 75

def on_if(node)
  return if !if_with_boolean_literal_branches?(node) || multiple_elsif?(node)

  condition = node.condition
  range, keyword = offense_range_with_keyword(node, condition)

  add_offense(range, message: message(node, keyword)) do |corrector|
    replacement = replacement_condition(node, condition)

    if node.elsif?
      corrector.insert_before(node, "else\n")
      corrector.replace(node, "#{indent(node.if_branch)}#{replacement}")
    else
      corrector.replace(node, replacement)
    end
  end
end

#opposite_condition?(node) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/if_with_boolean_literal_branches.rb', line 152

def opposite_condition?(node)
  (!node.unless? && node.if_branch.false_type?) ||
    (node.unless? && node.if_branch.true_type?)
end

#replacement_condition(node, condition) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/if_with_boolean_literal_branches.rb', line 142

def replacement_condition(node, condition)
  bang = '!' if opposite_condition?(node)

  if bang && require_parentheses?(condition)
    "#{bang}(#{condition.source})"
  else
    "#{bang}#{condition.source}"
  end
end

#require_parentheses?(condition) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/if_with_boolean_literal_branches.rb', line 157

def require_parentheses?(condition)
  condition.operator_keyword? || (condition.send_type? && condition.comparison_method?)
end

#return_boolean_value?(condition) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/if_with_boolean_literal_branches.rb', line 121

def return_boolean_value?(condition)
  return false unless condition

  if condition.begin_type?
    return_boolean_value?(condition.children.first)
  elsif condition.or_type?
    return_boolean_value?(condition.lhs) && return_boolean_value?(condition.rhs)
  elsif condition.and_type?
    return_boolean_value?(condition.rhs)
  else
    assume_boolean_value?(condition)
  end
end