Sha256: e1fe033c49411dc3984ee4e1a0ff36114b0b9680aac8f8c0dfb9dc78465f67f5

Contents?: true

Size: 1.54 KB

Versions: 45

Compression:

Stored size: 1.54 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Lint
      # Checks uses of lambda without a literal block.
      # It emulates the following warning in Ruby 3.0:
      #
      #   % ruby -vwe 'lambda(&proc {})'
      #   ruby 3.0.0p0 (2020-12-25 revision 95aff21468) [x86_64-darwin19]
      #   -e:1: warning: lambda without a literal block is deprecated; use the proc without
      #   lambda instead
      #
      # This way, proc object is never converted to lambda.
      # Autocorrection replaces with compatible proc argument.
      #
      # @example
      #
      #   # bad
      #   lambda(&proc { do_something })
      #   lambda(&Proc.new { do_something })
      #
      #   # good
      #   proc { do_something }
      #   Proc.new { do_something }
      #   lambda { do_something } # If you use lambda.
      #
      class LambdaWithoutLiteralBlock < Base
        extend AutoCorrector

        MSG = 'lambda without a literal block is deprecated; use the proc without lambda instead.'
        RESTRICT_ON_SEND = %i[lambda].freeze

        # @!method lambda_with_symbol_proc?(node)
        def_node_matcher :lambda_with_symbol_proc?, <<~PATTERN
          (send nil? :lambda (block_pass (sym _)))
        PATTERN

        def on_send(node)
          if node.parent&.block_type? || !node.first_argument || lambda_with_symbol_proc?(node)
            return
          end

          add_offense(node) do |corrector|
            corrector.replace(node, node.first_argument.source.delete('&'))
          end
        end
      end
    end
  end
end

Version data entries

45 entries across 41 versions & 6 rubygems

Version Path
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/lint/lambda_without_literal_block.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/lint/lambda_without_literal_block.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/lint/lambda_without_literal_block.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.36.0/lib/rubocop/cop/lint/lambda_without_literal_block.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.35.1/lib/rubocop/cop/lint/lambda_without_literal_block.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.31.2/lib/rubocop/cop/lint/lambda_without_literal_block.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.31.2/lib/rubocop/cop/lint/lambda_without_literal_block.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.35.1/lib/rubocop/cop/lint/lambda_without_literal_block.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.36.0/lib/rubocop/cop/lint/lambda_without_literal_block.rb
fluent-plugin-google-cloud-logging-on-prem-0.1.0 vendor/ruby/3.1.0/gems/rubocop-1.50.2/lib/rubocop/cop/lint/lambda_without_literal_block.rb
rubocop-1.50.2 lib/rubocop/cop/lint/lambda_without_literal_block.rb
rubocop-1.50.1 lib/rubocop/cop/lint/lambda_without_literal_block.rb
rubocop-1.50.0 lib/rubocop/cop/lint/lambda_without_literal_block.rb
rubocop-1.49.0 lib/rubocop/cop/lint/lambda_without_literal_block.rb
call_your_name-0.1.0 vendor/bundle/ruby/3.1.0/gems/rubocop-1.48.1/lib/rubocop/cop/lint/lambda_without_literal_block.rb
rubocop-1.48.1 lib/rubocop/cop/lint/lambda_without_literal_block.rb
rubocop-1.48.0 lib/rubocop/cop/lint/lambda_without_literal_block.rb
rubocop-1.47.0 lib/rubocop/cop/lint/lambda_without_literal_block.rb
zilla-0.2.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.46.0/lib/rubocop/cop/lint/lambda_without_literal_block.rb
rubocop-1.46.0 lib/rubocop/cop/lint/lambda_without_literal_block.rb