Sha256: e2207688da20bb4bc4805002a40159cfb6df3680c2dd70c8b938bf3509006f22

Contents?: true

Size: 1.51 KB

Versions: 26

Compression:

Stored size: 1.51 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.top >= x.top
      #
      #   if a.x != 0 && a.x != 0
      #     do_something
      #   end
      #
      #   def childs?
      #     left_child || left_child
      #   end
      #
      class BinaryOperatorWithIdenticalOperands < Base
        MSG = 'Binary operator `%<op>s` has identical operands.'
        MATH_OPERATORS = %i[+ - * / % ** << >> | ^].to_set.freeze

        def on_send(node)
          return unless node.binary_operation?

          lhs, operation, rhs = *node
          return if MATH_OPERATORS.include?(node.method_name) && lhs.basic_literal?

          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
plaid-14.13.0 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/lint/binary_operator_with_identical_operands.rb
plaid-14.12.1 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/lint/binary_operator_with_identical_operands.rb
plaid-14.12.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/lint/binary_operator_with_identical_operands.rb
plaid-14.11.1 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/lint/binary_operator_with_identical_operands.rb
plaid-14.10.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/lint/binary_operator_with_identical_operands.rb
plaid-14.7.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/lint/binary_operator_with_identical_operands.rb
rubocop-1.6.1 lib/rubocop/cop/lint/binary_operator_with_identical_operands.rb
rubocop-1.6.0 lib/rubocop/cop/lint/binary_operator_with_identical_operands.rb
rubocop-1.5.2 lib/rubocop/cop/lint/binary_operator_with_identical_operands.rb
rubocop-1.5.1 lib/rubocop/cop/lint/binary_operator_with_identical_operands.rb
rubocop-1.5.0 lib/rubocop/cop/lint/binary_operator_with_identical_operands.rb
rubocop-1.4.2 lib/rubocop/cop/lint/binary_operator_with_identical_operands.rb
rubocop-1.4.1 lib/rubocop/cop/lint/binary_operator_with_identical_operands.rb
rubocop-1.4.0 lib/rubocop/cop/lint/binary_operator_with_identical_operands.rb
rubocop-1.3.1 lib/rubocop/cop/lint/binary_operator_with_identical_operands.rb
rubocop-1.3.0 lib/rubocop/cop/lint/binary_operator_with_identical_operands.rb
rubocop-1.2.0 lib/rubocop/cop/lint/binary_operator_with_identical_operands.rb
rubocop-1.1.0 lib/rubocop/cop/lint/binary_operator_with_identical_operands.rb
rubocop-1.0.0 lib/rubocop/cop/lint/binary_operator_with_identical_operands.rb
rubocop-0.93.1 lib/rubocop/cop/lint/binary_operator_with_identical_operands.rb