<commit_before># encoding: utf-8
# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # Checks for empty else-clauses, possibly including comments and/or an
      # explicit `nil` depending on the EnforcedStyle.
      #
      # SupportedStyles:
      #
      # @example
      #   # good for all styles
      #   if condition
      #     statement
      #   else
      #     statement
      #   end
      #
      #   # good for all styles
      #   if condition
      #     statement
      #   end
      #
      # empty - warn only on empty else
      #   @example
      #   # bad
      #   if condition
      #     statement
      #   else
      #   end
      #
      #   # good
      #   if condition
      #     statement
      #   else
      #     nil
      #   end
      #
      # nil - warn on else with nil in it
      #   @example
      #   # bad
      #   if condition
      #     statement
      #   else
      #     nil
      #   end
      #
      #   # good
      #   if condition
      #     statement
      #   else
      #   end
      #
      # both - warn on empty else and else with nil in it
      #   @example
      #   # bad
      #   if condition
      #     statement
      #   else
      #     nil
      #   end
      #
      #   # bad
      #   if condition
      #     statement
      #   else
      #   end
      class EmptyElse < Cop
        include OnNormalIfUnless
        include ConfigurableEnforcedStyle

        MSG = 'Redundant `else`-clause.'.freeze

        def on_normal_if_unless(node)
          check(node, if_else_clause(node))
        end

        def on_case(node)
          check(node, case_else_clause(node))
        end

        private

        def check(node, else_clause)
          case style
          when :empty
            empty_check(node, else_clause)
          when :nil
            nil_check(node, else_clause)
          when :both
            both_check(node, else_clause)
          end
        end

        def empty_check(node, else_clause)
          add_offense(node, :else, MSG) if node.loc.else && else_clause.nil?
        end

        def nil_check(node, else_clause)
          return unless else_clause && else_clause.nil_type?
          add_offense(node, :else, MSG)
        end

        def both_check(node, else_clause)
          empty_check(node, else_clause)
          nil_check(node, else_clause)
        end

        def autocorrect(node)
          return false if autocorrect_forbidden?(node.type.to_s)

          lambda do |corrector|
            end_pos = if node.loc.end
                        node.loc.end.begin_pos
                      else
                        node.parent.loc.end.begin_pos
                      end

            range = Parser::Source::Range.new(node.source_range.source_buffer,
                                              node.loc.else.begin_pos,
                                              end_pos)
            corrector.remove(range)
          end
        end

        def autocorrect_forbidden?(type)
          [type, 'both'].include? missing_else_style
        end

        def missing_else_style
          missing_config = config.for_cop('Style/MissingElse')
          missing_config['Enabled'] ? missing_config['EnforcedStyle'] : nil
        end
      end
    end
  end
end<commit_msg>Reduce complexity in EmptyElse<commit_after># encoding: utf-8
# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # Checks for empty else-clauses, possibly including comments and/or an
      # explicit `nil` depending on the EnforcedStyle.
      #
      # SupportedStyles:
      #
      # @example
      #   # good for all styles
      #   if condition
      #     statement
      #   else
      #     statement
      #   end
      #
      #   # good for all styles
      #   if condition
      #     statement
      #   end
      #
      # empty - warn only on empty else
      #   @example
      #   # bad
      #   if condition
      #     statement
      #   else
      #   end
      #
      #   # good
      #   if condition
      #     statement
      #   else
      #     nil
      #   end
      #
      # nil - warn on else with nil in it
      #   @example
      #   # bad
      #   if condition
      #     statement
      #   else
      #     nil
      #   end
      #
      #   # good
      #   if condition
      #     statement
      #   else
      #   end
      #
      # both - warn on empty else and else with nil in it
      #   @example
      #   # bad
      #   if condition
      #     statement
      #   else
      #     nil
      #   end
      #
      #   # bad
      #   if condition
      #     statement
      #   else
      #   end
      class EmptyElse < Cop
        include OnNormalIfUnless
        include ConfigurableEnforcedStyle

        MSG = 'Redundant `else`-clause.'.freeze

        def on_normal_if_unless(node)
          check(node, if_else_clause(node))
        end

        def on_case(node)
          check(node, case_else_clause(node))
        end

        private

        def check(node, else_clause)
          case style
          when :empty
            empty_check(node, else_clause)
          when :nil
            nil_check(node, else_clause)
          when :both
            both_check(node, else_clause)
          end
        end

        def empty_check(node, else_clause)
          add_offense(node, :else, MSG) if node.loc.else && else_clause.nil?
        end

        def nil_check(node, else_clause)
          return unless else_clause && else_clause.nil_type?
          add_offense(node, :else, MSG)
        end

        def both_check(node, else_clause)
          empty_check(node, else_clause)
          nil_check(node, else_clause)
        end

        def autocorrect(node)
          return false if autocorrect_forbidden?(node.type.to_s)

          lambda do |corrector|
            end_pos = if node.loc.end
                        node.loc.end.begin_pos
                      else
                        node.parent.loc.end.begin_pos
                      end

            corrector.remove(range_between(node.loc.else.begin_pos, end_pos))
          end
        end

        def autocorrect_forbidden?(type)
          [type, 'both'].include? missing_else_style
        end

        def range_between(start_pos, end_pos)
          Parser::Source::Range.new(processed_source.buffer, start_pos, end_pos)
        end

        def missing_else_style
          missing_config = config.for_cop('Style/MissingElse')
          missing_config['Enabled'] ? missing_config['EnforcedStyle'] : nil
        end
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.0695364238410596, 'fraction_numerical': 0.00030102347983142685, 'mean_word_length': 1.3032928942807627, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '955677', 'n_tokens_mistral': 1960, 'n_tokens_neox': 1914, 'n_words': 452}