Sha256: ccef22db0e43802ee84b1184c79c60309d5f69293e62a3199c0ac74662f4bca4

Contents?: true

Size: 1.18 KB

Versions: 4

Compression:

Stored size: 1.18 KB

Contents

# encoding: utf-8

module Rubocop
  module Cop
    module Style
      # This cop checks for the presence of superfluous parentheses around the
      # condition of if/while/until.
      class ParenthesesAroundCondition < Cop
        ASGN_NODES = [:lvasgn, :ivasgn, :cvasgn, :gvasgn, :casgn]
        MSG = "Don't use parentheses around the condition of an " +
          'if/unless/while/until'

        def on_if(node)
          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, _body = *node

          if cond.type == :begin
            # allow safe assignment
            return if safe_assignment?(cond) && safe_assignment_allowed?

            add_offence(:convention, cond.loc.expression, MSG)
          end
        end

        def safe_assignment?(node)
          node.children.size == 1 && ASGN_NODES.include?(node.children[0].type)
        end

        def safe_assignment_allowed?
          ParenthesesAroundCondition.config['AllowSafeAssignment']
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rubocop-0.12.0 lib/rubocop/cop/style/parentheses_around_condition.rb
rubocop-0.11.1 lib/rubocop/cop/style/parentheses_around_condition.rb
rubocop-0.11.0 lib/rubocop/cop/style/parentheses_around_condition.rb
rubocop-0.10.0 lib/rubocop/cop/style/parentheses_around_condition.rb