class RuboCop::Cop::Style::RedundantConditional
This cop checks for redundant returning of true/false in conditionals.
@example
# bad x == y ? true : false # bad if x == y true else false end # good x == y # bad x == y ? false : true # good x != y
Constants
- COMPARISON_OPERATORS
- MSG
Public Instance Methods
autocorrect(node)
click to toggle source
# File lib/rubocop/cop/style/redundant_conditional.rb, line 41 def autocorrect(node) lambda do |corrector| corrector.replace(node.loc.expression, replacement_condition(node)) end end
on_if(node)
click to toggle source
# File lib/rubocop/cop/style/redundant_conditional.rb, line 35 def on_if(node) return unless offense?(node) add_offense(node) end
Private Instance Methods
configured_indentation_width()
click to toggle source
Calls superclass method
RuboCop::Cop::Alignment#configured_indentation_width
# File lib/rubocop/cop/style/redundant_conditional.rb, line 91 def configured_indentation_width super || 2 end
indented_else_node(expression, node)
click to toggle source
# File lib/rubocop/cop/style/redundant_conditional.rb, line 87 def indented_else_node(expression, node) "else\n#{indentation(node)}#{expression}" end
invert_expression?(node)
click to toggle source
# File lib/rubocop/cop/style/redundant_conditional.rb, line 77 def invert_expression?(node) ( (node.if? || node.elsif? || node.ternary?) && redundant_condition_inverted?(node) ) || ( node.unless? && redundant_condition?(node) ) end
message(node)
click to toggle source
# File lib/rubocop/cop/style/redundant_conditional.rb, line 49 def message(node) replacement = replacement_condition(node) msg = node.elsif? ? "\n#{replacement}" : replacement format(MSG, msg: msg) end
offense?(node)
click to toggle source
# File lib/rubocop/cop/style/redundant_conditional.rb, line 64 def offense?(node) return if node.modifier_form? redundant_condition?(node) || redundant_condition_inverted?(node) end
replacement_condition(node)
click to toggle source
# File lib/rubocop/cop/style/redundant_conditional.rb, line 70 def replacement_condition(node) condition = node.condition.source expression = invert_expression?(node) ? "!(#{condition})" : condition node.elsif? ? indented_else_node(expression, node) : expression end