Sha256: 856ac58367ccf1084618d60d4245100fd209b3c8eb39bdbecf777f15cce2f7b1

Contents?: true

Size: 1.57 KB

Versions: 26

Compression:

Stored size: 1.57 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Lint
      # This cop checks for places where binary operator has identical operands.
      #
      # It covers arithmetic operators: `+`, `-`, `*`, `/`, `%`, `**`;
      # comparison operators: `==`, `===`, `=~`, `>`, `>=`, `<`, `<=`;
      # bitwise operators: `|`, `^`, `&`, `<<`, `>>`;
      # boolean operators: `&&`, `||`
      # and "spaceship" operator - `<=>`.
      #
      # This cop is marked as unsafe as it does not consider side effects when calling methods
      # and thus can generate false positives:
      #   if wr.take_char == '\0' && wr.take_char == '\0'
      #
      # @example
      #   # bad
      #   x / x
      #   x.top >= x.top
      #
      #   if a.x != 0 && a.x != 0
      #     do_something
      #   end
      #
      #   def childs?
      #     left_child || left_child
      #   end
      #
      #   # good
      #   x + x
      #   1 << 1
      #
      class BinaryOperatorWithIdenticalOperands < Base
        MSG = 'Binary operator `%<op>s` has identical operands.'
        ALLOWED_MATH_OPERATORS = %i[+ * ** << >>].to_set.freeze

        def on_send(node)
          return unless node.binary_operation?

          lhs, operation, rhs = *node
          return if ALLOWED_MATH_OPERATORS.include?(node.method_name)

          add_offense(node, message: format(MSG, op: operation)) if lhs == rhs
        end

        def on_and(node)
          add_offense(node, message: format(MSG, op: node.operator)) if node.lhs == node.rhs
        end
        alias on_or on_and
      end
    end
  end
end

Version data entries

26 entries across 26 versions & 3 rubygems

Version Path
rubocop-1.21.0 lib/rubocop/cop/lint/binary_operator_with_identical_operands.rb
rubocop-1.20.0 lib/rubocop/cop/lint/binary_operator_with_identical_operands.rb
rubocop-1.19.1 lib/rubocop/cop/lint/binary_operator_with_identical_operands.rb
rails_mini_profiler-0.2.0 vendor/bundle/ruby/3.0.0/gems/rubocop-1.18.3/lib/rubocop/cop/lint/binary_operator_with_identical_operands.rb
rubocop-1.19.0 lib/rubocop/cop/lint/binary_operator_with_identical_operands.rb
rubocop-1.18.4 lib/rubocop/cop/lint/binary_operator_with_identical_operands.rb
rubocop-1.18.3 lib/rubocop/cop/lint/binary_operator_with_identical_operands.rb
rubocop-1.18.2 lib/rubocop/cop/lint/binary_operator_with_identical_operands.rb
rubocop-1.18.1 lib/rubocop/cop/lint/binary_operator_with_identical_operands.rb
rubocop-1.18.0 lib/rubocop/cop/lint/binary_operator_with_identical_operands.rb
rubocop-1.17.0 lib/rubocop/cop/lint/binary_operator_with_identical_operands.rb
rubocop-1.16.1 lib/rubocop/cop/lint/binary_operator_with_identical_operands.rb
rubocop-1.16.0 lib/rubocop/cop/lint/binary_operator_with_identical_operands.rb
rubocop-1.15.0 lib/rubocop/cop/lint/binary_operator_with_identical_operands.rb
cocRb-0.1.0 .bundle/ruby/3.0.0/gems/rubocop-1.14.0/lib/rubocop/cop/lint/binary_operator_with_identical_operands.rb
rubocop-1.14.0 lib/rubocop/cop/lint/binary_operator_with_identical_operands.rb
rubocop-1.13.0 lib/rubocop/cop/lint/binary_operator_with_identical_operands.rb
rubocop-1.12.1 lib/rubocop/cop/lint/binary_operator_with_identical_operands.rb
rubocop-1.12.0 lib/rubocop/cop/lint/binary_operator_with_identical_operands.rb
rubocop-1.11.0 lib/rubocop/cop/lint/binary_operator_with_identical_operands.rb