Class: RuboCop::Cop::Style::IfWithBooleanLiteralBranches
- 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 ifs 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 elsifs are not.
true
else
false
end
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
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
- #double_negative?(node) ⇒ Object
- #if_with_boolean_literal_branches?(node) ⇒ Object
- #on_if(node) ⇒ Object
Methods included from AutoCorrector
Methods inherited from Base
#active_support_extensions_enabled?, #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
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
Constructor Details
This class inherits a constructor from RuboCop::Cop::Base
Instance Method Details
#double_negative?(node) ⇒ Object
74 |
# File 'lib/rubocop/cop/style/if_with_boolean_literal_branches.rb', line 74 def_node_matcher :double_negative?, '(send (send _ :!) :!)' |
#if_with_boolean_literal_branches?(node) ⇒ Object
70 71 72 |
# File 'lib/rubocop/cop/style/if_with_boolean_literal_branches.rb', line 70 def_node_matcher :if_with_boolean_literal_branches?, <<~PATTERN (if #return_boolean_value? {(true) (false) | (false) (true)}) PATTERN |
#on_if(node) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/rubocop/cop/style/if_with_boolean_literal_branches.rb', line 76 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: (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 |