Sha256: 8a46010f454ce3106efc5d92dd2e62382e3563c6b58b9bd69276d870072a2ee6

Contents?: true

Size: 1.46 KB

Versions: 6

Compression:

Stored size: 1.46 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Lint
      # This cop checks for the presence of `if`, `elsif` and `unless` branches without a body.
      # @example
      #   # bad
      #   if condition
      #   end
      #
      #   # bad
      #   unless condition
      #   end
      #
      #   # bad
      #   if condition
      #     do_something
      #   elsif other_condition
      #   end
      #
      #   # good
      #   if condition
      #     do_something
      #   end
      #
      #   # good
      #   unless condition
      #     do_something
      #   end
      #
      #   # good
      #   if condition
      #     do_something
      #   elsif other_condition
      #     do_something_else
      #   end
      #
      # @example AllowComments: true (default)
      #   # good
      #   if condition
      #     do_something
      #   elsif other_condition
      #     # noop
      #   end
      #
      # @example AllowComments: false
      #   # bad
      #   if condition
      #     do_something
      #   elsif other_condition
      #     # noop
      #   end
      #
      class EmptyConditionalBody < Base
        include CommentsHelp

        MSG = 'Avoid `%<keyword>s` branches without a body.'

        def on_if(node)
          return if node.body
          return if cop_config['AllowComments'] && contains_comments?(node)

          add_offense(node, message: format(MSG, keyword: node.keyword))
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
rubocop-1.29.1 lib/rubocop/cop/lint/empty_conditional_body.rb
rubocop-1.29.0 lib/rubocop/cop/lint/empty_conditional_body.rb
rubocop-1.28.2 lib/rubocop/cop/lint/empty_conditional_body.rb
rubocop-1.28.1 lib/rubocop/cop/lint/empty_conditional_body.rb
rubocop-1.28.0 lib/rubocop/cop/lint/empty_conditional_body.rb
rubocop-1.27.0 lib/rubocop/cop/lint/empty_conditional_body.rb