Sha256: 8831faf86b4fdbd5669c2c8fc3106f29ab4a505ca2c84ae2503186d07491973e

Contents?: true

Size: 1.2 KB

Versions: 9

Compression:

Stored size: 1.2 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Lint
      # Checks for the presence of `when` branches without a body.
      #
      # @example
      #
      #   # bad
      #   case foo
      #   when bar
      #     do_something
      #   when baz
      #   end
      #
      #   # good
      #   case condition
      #   when foo
      #     do_something
      #   when bar
      #     nil
      #   end
      #
      # @example AllowComments: true (default)
      #
      #   # good
      #   case condition
      #   when foo
      #     do_something
      #   when bar
      #     # noop
      #   end
      #
      # @example AllowComments: false
      #
      #   # bad
      #   case condition
      #   when foo
      #     do_something
      #   when bar
      #     # do nothing
      #   end
      #
      class EmptyWhen < Base
        include CommentsHelp

        MSG = 'Avoid `when` branches without a body.'

        def on_case(node)
          node.when_branches.each do |when_node|
            next if when_node.body
            next if cop_config['AllowComments'] && contains_comments?(when_node)

            add_offense(when_node)
          end
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
rubocop-1.70.0 lib/rubocop/cop/lint/empty_when.rb
rubocop-1.69.2 lib/rubocop/cop/lint/empty_when.rb
rubocop-1.69.1 lib/rubocop/cop/lint/empty_when.rb
rubocop-1.69.0 lib/rubocop/cop/lint/empty_when.rb
rubocop-1.68.0 lib/rubocop/cop/lint/empty_when.rb
rubocop-1.67.0 lib/rubocop/cop/lint/empty_when.rb
rubocop-1.66.1 lib/rubocop/cop/lint/empty_when.rb
rubocop-1.66.0 lib/rubocop/cop/lint/empty_when.rb
rubocop-1.65.1 lib/rubocop/cop/lint/empty_when.rb