class RuboCop::Cop::Lint::RequireParentheses
This cop checks for expressions where there is a call to a predicate method with at least one argument, where no parentheses are used around the parameter list, and a boolean operator, && or ||, is used in the last argument.
The idea behind warning for these constructs is that the user might be under the impression that the return value from the method call is an operand of &&/||.
@example
if day.is? :tuesday && month == :jan ... end
Constants
- MSG
Public Instance Methods
on_send(node)
click to toggle source
# File lib/rubocop/cop/lint/require_parentheses.rb, line 26 def on_send(node) _receiver, method_name, *args = *node return if parentheses?(node) return if args.empty? if ternary?(args.first) check_ternary(args.first, node) elsif predicate?(method_name) # We're only checking predicate methods. There would be false # positives otherwise. check_send(args.last, node) end end
Private Instance Methods
check_send(arg, node)
click to toggle source
# File lib/rubocop/cop/lint/require_parentheses.rb, line 54 def check_send(arg, node) add_offense(node, :expression) if offense?(arg) end
check_ternary(arg, node)
click to toggle source
# File lib/rubocop/cop/lint/require_parentheses.rb, line 43 def check_ternary(arg, node) condition, = *arg return unless offense?(condition) expr = node.source_range range = Parser::Source::Range.new(expr.source_buffer, expr.begin_pos, condition.source_range.end_pos) add_offense(range, range) end
offense?(node)
click to toggle source
# File lib/rubocop/cop/lint/require_parentheses.rb, line 62 def offense?(node) [:and, :or].include?(node.type) end
predicate?(method_name)
click to toggle source
# File lib/rubocop/cop/lint/require_parentheses.rb, line 58 def predicate?(method_name) method_name.to_s.end_with?('?') end