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

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
AllowedMethods
Defined in:
lib/rubocop/cop/style/if_with_boolean_literal_branches.rb

Overview

This cop 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 negative. However, auto-correction is unsafe because there is no guarantee that all predicate methods will return boolean value. Those methods can be allowed with `AllowedMethods` config.

Examples:

# bad
if foo == bar
  true
else
  false
end

# bad
foo == bar ? true : false

# good
foo == bar

AllowedMethods: ['nonzero?']

# good
num.nonzero? ? true : false

Constant Summary collapse

MSG =
'Remove redundant %<keyword>s with boolean literal branches.'
MSG_FOR_ELSIF =
'Use `else` instead of redundant `elsif` with boolean literal branches.'

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

#double_negative?(node) ⇒ Object


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

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

#if_with_boolean_literal_branches?(node) ⇒ Object


38
39
40
# File 'lib/rubocop/cop/style/if_with_boolean_literal_branches.rb', line 38

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

#on_if(node) ⇒ Object


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/rubocop/cop/style/if_with_boolean_literal_branches.rb', line 44

def on_if(node)
  return unless if_with_boolean_literal_branches?(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