Sha256: 55bdafcca037df713ffae2f46669a3c3c110b8da4e5b46ca377dc25012d8e331

Contents?: true

Size: 1.04 KB

Versions: 40

Compression:

Stored size: 1.04 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Lint
      # This cop checks to make sure safe navigation isn't used with `empty?` in
      # a conditional.
      #
      # While the safe navigation operator is generally a good idea, when
      # checking `foo&.empty?` in a conditional, `foo` being `nil` will actually
      # do the opposite of what the author intends.
      #
      # @example
      #   # bad
      #   return if foo&.empty?
      #   return unless foo&.empty?
      #
      #   # good
      #   return if foo && foo.empty?
      #   return unless foo && foo.empty?
      #
      class SafeNavigationWithEmpty < Cop
        MSG = 'Avoid calling `empty?` with the safe navigation operator ' \
          'in conditionals.'

        def_node_matcher :safe_navigation_empty_in_conditional?, <<~PATTERN
          (if (csend (send ...) :empty?) ...)
        PATTERN

        def on_if(node)
          return unless safe_navigation_empty_in_conditional?(node)

          add_offense(node)
        end
      end
    end
  end
end

Version data entries

40 entries across 29 versions & 5 rubygems

Version Path
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.79.0/lib/rubocop/cop/lint/safe_navigation_with_empty.rb
files.com-1.0.1 vendor/bundle/ruby/2.5.0/gems/rubocop-0.85.1/lib/rubocop/cop/lint/safe_navigation_with_empty.rb
rbhint-0.85.1.rc1 lib/rubocop/cop/lint/safe_navigation_with_empty.rb
rubocop-0.85.1 lib/rubocop/cop/lint/safe_navigation_with_empty.rb
rbhint-0.8.5.rc1 lib/rubocop/cop/lint/safe_navigation_with_empty.rb
rubocop-0.85.0 lib/rubocop/cop/lint/safe_navigation_with_empty.rb
rubocop-0.84.0 lib/rubocop/cop/lint/safe_navigation_with_empty.rb
rubocop-0.83.0 lib/rubocop/cop/lint/safe_navigation_with_empty.rb
rubocop-0.82.0 lib/rubocop/cop/lint/safe_navigation_with_empty.rb
rubocop-0.81.0 lib/rubocop/cop/lint/safe_navigation_with_empty.rb
rubocop-0.80.1 lib/rubocop/cop/lint/safe_navigation_with_empty.rb
rubocop-0.80.0 lib/rubocop/cop/lint/safe_navigation_with_empty.rb
grape-extra_validators-1.0.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.79.0/lib/rubocop/cop/lint/safe_navigation_with_empty.rb
grape-extra_validators-1.0.0 vendor/bundle/ruby/2.4.0/gems/rubocop-0.79.0/lib/rubocop/cop/lint/safe_navigation_with_empty.rb
rubocop-0.79.0 lib/rubocop/cop/lint/safe_navigation_with_empty.rb
rubocop-0.78.0 lib/rubocop/cop/lint/safe_navigation_with_empty.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.77.0/lib/rubocop/cop/lint/safe_navigation_with_empty.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.74.0/lib/rubocop/cop/lint/safe_navigation_with_empty.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.75.0/lib/rubocop/cop/lint/safe_navigation_with_empty.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.75.1/lib/rubocop/cop/lint/safe_navigation_with_empty.rb