Sha256: c8614228ef3d0f512bf237d64b7f39b6565b83080e1f91d835819eff6d43f782

Contents?: true

Size: 1.7 KB

Versions: 38

Compression:

Stored size: 1.7 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Lint
      # 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
      #
      #   # bad
      #
      #   if day.is? :tuesday && month == :jan
      #     # ...
      #   end
      #
      # @example
      #
      #   # good
      #
      #   if day.is?(:tuesday) && month == :jan
      #     # ...
      #   end
      class RequireParentheses < Base
        include RangeHelp

        MSG = 'Use parentheses in the method call to avoid confusion about precedence.'

        def on_send(node)
          return if !node.arguments? || node.parenthesized?

          if node.first_argument.if_type? && node.first_argument.ternary?
            check_ternary(node.first_argument, node)
          elsif node.predicate_method?
            check_predicate(node.last_argument, node)
          end
        end
        alias on_csend on_send

        private

        def check_ternary(ternary, node)
          return unless ternary.condition.operator_keyword?

          range = range_between(node.source_range.begin_pos, ternary.condition.source_range.end_pos)

          add_offense(range)
        end

        def check_predicate(predicate, node)
          return unless predicate.operator_keyword?

          add_offense(node)
        end
      end
    end
  end
end

Version data entries

38 entries across 38 versions & 6 rubygems

Version Path
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/lint/require_parentheses.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/lint/require_parentheses.rb
rubocop-1.29.1 lib/rubocop/cop/lint/require_parentheses.rb
rubocop-1.29.0 lib/rubocop/cop/lint/require_parentheses.rb
rubocop-1.28.2 lib/rubocop/cop/lint/require_parentheses.rb
rubocop-1.28.1 lib/rubocop/cop/lint/require_parentheses.rb
rubocop-1.28.0 lib/rubocop/cop/lint/require_parentheses.rb
rubocop-1.27.0 lib/rubocop/cop/lint/require_parentheses.rb
rubocop-1.26.1 lib/rubocop/cop/lint/require_parentheses.rb
op_connect-0.1.2 vendor/bundle/ruby/3.1.0/gems/rubocop-1.26.0/lib/rubocop/cop/lint/require_parentheses.rb
rubocop-1.26.0 lib/rubocop/cop/lint/require_parentheses.rb
rubocop-1.25.1 lib/rubocop/cop/lint/require_parentheses.rb
rubocop-1.25.0 lib/rubocop/cop/lint/require_parentheses.rb
phillipug-foodie-0.1.0 .vendor/ruby/3.0.0/gems/rubocop-1.24.0/lib/rubocop/cop/lint/require_parentheses.rb
rubocop-1.24.1 lib/rubocop/cop/lint/require_parentheses.rb
rubocop-1.24.0 lib/rubocop/cop/lint/require_parentheses.rb
rubocop-1.23.0 lib/rubocop/cop/lint/require_parentheses.rb
rubocop-1.22.3 lib/rubocop/cop/lint/require_parentheses.rb
rubocop-1.22.2 lib/rubocop/cop/lint/require_parentheses.rb
rubocop-1.22.1 lib/rubocop/cop/lint/require_parentheses.rb