Sha256: 5e73457ec6676f2522696c63313626e905029c8c4d4bcda6c036ce3e7ce7ac53

Contents?: true

Size: 1.31 KB

Versions: 40

Compression:

Stored size: 1.31 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Lint
      # This cop 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.
      # Auto-correction 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

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

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

Version data entries

40 entries across 40 versions & 6 rubygems

Version Path
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/lint/lambda_without_literal_block.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/lint/lambda_without_literal_block.rb
rubocop-1.26.1 lib/rubocop/cop/lint/lambda_without_literal_block.rb
op_connect-0.1.2 vendor/bundle/ruby/3.1.0/gems/rubocop-1.26.0/lib/rubocop/cop/lint/lambda_without_literal_block.rb
rubocop-1.26.0 lib/rubocop/cop/lint/lambda_without_literal_block.rb
rubocop-1.25.1 lib/rubocop/cop/lint/lambda_without_literal_block.rb
rubocop-1.25.0 lib/rubocop/cop/lint/lambda_without_literal_block.rb
phillipug-foodie-0.1.0 .vendor/ruby/3.0.0/gems/rubocop-1.24.0/lib/rubocop/cop/lint/lambda_without_literal_block.rb
rubocop-1.24.1 lib/rubocop/cop/lint/lambda_without_literal_block.rb
rubocop-1.24.0 lib/rubocop/cop/lint/lambda_without_literal_block.rb
rubocop-1.23.0 lib/rubocop/cop/lint/lambda_without_literal_block.rb
rubocop-1.22.3 lib/rubocop/cop/lint/lambda_without_literal_block.rb
rubocop-1.22.2 lib/rubocop/cop/lint/lambda_without_literal_block.rb
rubocop-1.22.1 lib/rubocop/cop/lint/lambda_without_literal_block.rb
rubocop-1.22.0 lib/rubocop/cop/lint/lambda_without_literal_block.rb
rubocop-1.21.0 lib/rubocop/cop/lint/lambda_without_literal_block.rb
rubocop-1.20.0 lib/rubocop/cop/lint/lambda_without_literal_block.rb
rubocop-1.19.1 lib/rubocop/cop/lint/lambda_without_literal_block.rb
rails_mini_profiler-0.2.0 vendor/bundle/ruby/3.0.0/gems/rubocop-1.18.3/lib/rubocop/cop/lint/lambda_without_literal_block.rb
rubocop-1.19.0 lib/rubocop/cop/lint/lambda_without_literal_block.rb