class RuboCop::Cop::Style::ParenthesesAroundCondition
This cop checks for the presence of superfluous parentheses around the condition of if/unless/while/until.
Public Instance Methods
on_if(node)
click to toggle source
# File lib/rubocop/cop/style/parentheses_around_condition.rb, line 13 def on_if(node) return if ternary_op?(node) process_control_op(node) end
on_until(node)
click to toggle source
# File lib/rubocop/cop/style/parentheses_around_condition.rb, line 22 def on_until(node) process_control_op(node) end
on_while(node)
click to toggle source
# File lib/rubocop/cop/style/parentheses_around_condition.rb, line 18 def on_while(node) process_control_op(node) end
Private Instance Methods
message(node)
click to toggle source
# File lib/rubocop/cop/style/parentheses_around_condition.rb, line 50 def message(node) kw = node.loc.keyword.source article = kw == 'while' ? 'a' : 'an' "Don't use parentheses around the condition of #{article} `#{kw}`." end
modifier_op?(node)
click to toggle source
# File lib/rubocop/cop/style/parentheses_around_condition.rb, line 42 def modifier_op?(node) return false if ternary_op?(node) return true if node.type == :rescue [:if, :while, :until].include?(node.type) && node.loc.end.nil? end
process_control_op(node)
click to toggle source
# File lib/rubocop/cop/style/parentheses_around_condition.rb, line 28 def process_control_op(node) cond, _body = *node return unless cond.type == :begin # handle `if (something rescue something_else) ...` return if modifier_op?(cond.children.first) # check if there's any whitespace between the keyword and the cond return if parens_required?(node.children.first) # allow safe assignment return if safe_assignment?(cond) && safe_assignment_allowed? add_offense(cond, :expression, message(node)) end