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
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/lint/duplicate_elsif_condition.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/lint/duplicate_elsif_condition.rb
rubocop-1.29.1 lib/rubocop/cop/lint/duplicate_elsif_condition.rb
rubocop-1.29.0 lib/rubocop/cop/lint/duplicate_elsif_condition.rb
rubocop-1.28.2 lib/rubocop/cop/lint/duplicate_elsif_condition.rb
rubocop-1.28.1 lib/rubocop/cop/lint/duplicate_elsif_condition.rb
rubocop-1.28.0 lib/rubocop/cop/lint/duplicate_elsif_condition.rb
rubocop-1.27.0 lib/rubocop/cop/lint/duplicate_elsif_condition.rb
rubocop-1.26.1 lib/rubocop/cop/lint/duplicate_elsif_condition.rb
op_connect-0.1.2 vendor/bundle/ruby/3.1.0/gems/rubocop-1.26.0/lib/rubocop/cop/lint/duplicate_elsif_condition.rb
rubocop-1.26.0 lib/rubocop/cop/lint/duplicate_elsif_condition.rb
plaid-14.13.0 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/lint/duplicate_elsif_condition.rb
plaid-14.12.1 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/lint/duplicate_elsif_condition.rb
rubocop-1.25.1 lib/rubocop/cop/lint/duplicate_elsif_condition.rb
rubocop-1.25.0 lib/rubocop/cop/lint/duplicate_elsif_condition.rb
plaid-14.12.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/lint/duplicate_elsif_condition.rb
plaid-14.11.1 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/lint/duplicate_elsif_condition.rb
phillipug-foodie-0.1.0 .vendor/ruby/3.0.0/gems/rubocop-1.24.0/lib/rubocop/cop/lint/duplicate_elsif_condition.rb
rubocop-1.24.1 lib/rubocop/cop/lint/duplicate_elsif_condition.rb
rubocop-1.24.0 lib/rubocop/cop/lint/duplicate_elsif_condition.rb