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 “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
trueelsif bar > baz
trueelsif qux > quux # Single
elsifis warned, but two or more ‘elsif`s are not.trueelse
falseend
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, #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_gem_version, #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
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?, "(if #return_boolean_value? <true false>)\n" |
#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: (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 |