Sha256: d0c0b650e1424ea2cecb6b99ba310f321a78a01a5215942a5f1994dffbefe3ba

Contents?: true

Size: 1.39 KB

Versions: 6

Compression:

Stored size: 1.39 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # This cop checks for the presence of superfluous parentheses around the
      # condition of if/unless/while/until.
      class ParenthesesAroundCondition < Cop
        include SafeAssignment
        include Parentheses

        def on_if(node)
          return if node.ternary?

          process_control_op(node)
        end

        def on_while(node)
          process_control_op(node)
        end

        def on_until(node)
          process_control_op(node)
        end

        private

        def process_control_op(node)
          cond = node.condition

          return unless cond.begin_type?
          return if cond.children.empty?
          return if modifier_op?(cond.children.first)
          return if parens_required?(node.children.first)
          return if safe_assignment?(cond) && safe_assignment_allowed?

          add_offense(cond, :expression, message(node))
        end

        def modifier_op?(node)
          return false if node.if_type? && node.ternary?
          return true if node.rescue_type?

          MODIFIER_NODES.include?(node.type) &&
            node.modifier_form?
        end

        def message(node)
          kw = node.keyword
          article = kw == 'while' ? 'a' : 'an'
          "Don't use parentheses around the condition of #{article} `#{kw}`."
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
rubocop-0.49.1 lib/rubocop/cop/style/parentheses_around_condition.rb
rubocop-0.49.0 lib/rubocop/cop/style/parentheses_around_condition.rb
rubocop-0.48.1 lib/rubocop/cop/style/parentheses_around_condition.rb
rubocop-0.48.0 lib/rubocop/cop/style/parentheses_around_condition.rb
rubocop-0.47.1 lib/rubocop/cop/style/parentheses_around_condition.rb
rubocop-0.47.0 lib/rubocop/cop/style/parentheses_around_condition.rb