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

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 `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:

source,ruby

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

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

#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?, #offenses, #on_investigation_end, #on_new_investigation, #on_other_file, #parse, #parser_engine, #ready, #relevant_file?, requires_gem, #string_literals_frozen_by_default?, 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

#double_negative?(node) ⇒ Object



73
# 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) ⇒ Object



69
70
71
# 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

#on_if(node) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# 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