<commit_before># frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # Identifies places where `fetch(key) { value }`
      # can be replaced by `fetch(key, value)`.
      #
      # In such cases `fetch(key, value)` method is faster
      # than `fetch(key) { value }`.
      #
      # @safety
      #   This cop is unsafe because it cannot be guaranteed that the receiver
      #   does not have a different implementation of `fetch`.
      #
      # @example SafeForConstants: false (default)
      #   # bad
      #   hash.fetch(:key) { 5 }
      #   hash.fetch(:key) { true }
      #   hash.fetch(:key) { nil }
      #   array.fetch(5) { :value }
      #   ENV.fetch(:key) { 'value' }
      #
      #   # good
      #   hash.fetch(:key, 5)
      #   hash.fetch(:key, true)
      #   hash.fetch(:key, nil)
      #   array.fetch(5, :value)
      #   ENV.fetch(:key, 'value')
      #
      # @example SafeForConstants: true
      #   # bad
      #   ENV.fetch(:key) { VALUE }
      #
      #   # good
      #   ENV.fetch(:key, VALUE)
      #
      class RedundantFetchBlock < Base
        include FrozenStringLiteral
        include RangeHelp
        extend AutoCorrector

        MSG = 'Use `%<good>s` instead of `%<bad>s`.'

        # @!method redundant_fetch_block_candidate?(node)
        def_node_matcher :redundant_fetch_block_candidate?, <<~PATTERN
          (block
            $(send _ :fetch _)
            (args)
            ${nil? #basic_literal? #const_type?})
        PATTERN

        def on_block(node)
          redundant_fetch_block_candidate?(node) do |send, body|
            return if should_not_check?(send, body)

            range = fetch_range(send, node)
            good = build_good_method(send, body)<commit_msg>Disable InternalAffairs/NumblockHandler for Style/RedundantFetchBlock<commit_after># frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # Identifies places where `fetch(key) { value }`
      # can be replaced by `fetch(key, value)`.
      #
      # In such cases `fetch(key, value)` method is faster
      # than `fetch(key) { value }`.
      #
      # @safety
      #   This cop is unsafe because it cannot be guaranteed that the receiver
      #   does not have a different implementation of `fetch`.
      #
      # @example SafeForConstants: false (default)
      #   # bad
      #   hash.fetch(:key) { 5 }
      #   hash.fetch(:key) { true }
      #   hash.fetch(:key) { nil }
      #   array.fetch(5) { :value }
      #   ENV.fetch(:key) { 'value' }
      #
      #   # good
      #   hash.fetch(:key, 5)
      #   hash.fetch(:key, true)
      #   hash.fetch(:key, nil)
      #   array.fetch(5, :value)
      #   ENV.fetch(:key, 'value')
      #
      # @example SafeForConstants: true
      #   # bad
      #   ENV.fetch(:key) { VALUE }
      #
      #   # good
      #   ENV.fetch(:key, VALUE)
      #
      class RedundantFetchBlock < Base
        include FrozenStringLiteral
        include RangeHelp
        extend AutoCorrector

        MSG = 'Use `%<good>s` instead of `%<bad>s`.'

        # @!method redundant_fetch_block_candidate?(node)
        def_node_matcher :redundant_fetch_block_candidate?, <<~PATTERN
          (block
            $(send _ :fetch _)
            (args)
            ${nil? #basic_literal? #const_type?})
        PATTERN

        def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
          redundant_fetch_block_candidate?(node) do |send, body|
            return if should_not_check?(send, body)

            range = fetch_range(send, node)
            good = build_good_method(send, body)
{'fraction_non_alphanumeric': 0.1155958264689731, 'fraction_numerical': 0.002196595277320154, 'mean_word_length': 1.981178396072013, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1451878', 'n_tokens_mistral': 1167, 'n_tokens_neox': 1139, 'n_words': 268}