Sha256: fd8c49c7ffa9bb4ee526a798bc398b13776ce8fa6a29bb7bdc1f48199c485498

Contents?: true

Size: 911 Bytes

Versions: 77

Compression:

Stored size: 911 Bytes

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Lint
      # This cop checks that there are no repeated conditions used in if 'elsif'.
      #
      # @example
      #   # bad
      #   if x == 1
      #     do_something
      #   elsif x == 1
      #     do_something_else
      #   end
      #
      #   # good
      #   if x == 1
      #     do_something
      #   elsif x == 2
      #     do_something_else
      #   end
      #
      class DuplicateElsifCondition < Base
        MSG = 'Duplicate `elsif` condition detected.'

        def on_if(node)
          previous = []
          while node.if? || node.elsif?
            condition = node.condition
            add_offense(condition) if previous.include?(condition)
            previous << condition
            node = node.else_branch
            break unless node&.if_type?
          end
        end
      end
    end
  end
end

Version data entries

77 entries across 77 versions & 9 rubygems

Version Path
rubocop-1.4.0 lib/rubocop/cop/lint/duplicate_elsif_condition.rb
rubocop-1.3.1 lib/rubocop/cop/lint/duplicate_elsif_condition.rb
rubocop-1.3.0 lib/rubocop/cop/lint/duplicate_elsif_condition.rb
rubocop-1.2.0 lib/rubocop/cop/lint/duplicate_elsif_condition.rb
rubocop-1.1.0 lib/rubocop/cop/lint/duplicate_elsif_condition.rb
rubocop-1.0.0 lib/rubocop/cop/lint/duplicate_elsif_condition.rb
rubocop-0.93.1 lib/rubocop/cop/lint/duplicate_elsif_condition.rb
rubocop-0.93.0 lib/rubocop/cop/lint/duplicate_elsif_condition.rb
rubocop-0.92.0 lib/rubocop/cop/lint/duplicate_elsif_condition.rb
rubocop-0.91.1 lib/rubocop/cop/lint/duplicate_elsif_condition.rb
rubocop-0.91.0 lib/rubocop/cop/lint/duplicate_elsif_condition.rb
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.90.0/lib/rubocop/cop/lint/duplicate_elsif_condition.rb
rubocop-0.90.0 lib/rubocop/cop/lint/duplicate_elsif_condition.rb
rubocop-0.89.1 lib/rubocop/cop/lint/duplicate_elsif_condition.rb
rubocop-0.89.0 lib/rubocop/cop/lint/duplicate_elsif_condition.rb
rubocop-0.88.0 lib/rubocop/cop/lint/duplicate_elsif_condition.rb
rbhint-0.87.1.rc1 lib/rubocop/cop/lint/duplicate_elsif_condition.rb