Sha256: bf2f7966bd59b5c68e6b16e34bb002a15fdf1b87f76b0e59e7c733ec81533691

Contents?: true

Size: 1.43 KB

Versions: 69

Compression:

Stored size: 1.43 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
        MSG = 'Avoid `%<keyword>s` branches without a body.'

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

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

Version data entries

69 entries across 69 versions & 8 rubygems

Version Path
rubocop-1.22.0 lib/rubocop/cop/lint/empty_conditional_body.rb
rubocop-1.21.0 lib/rubocop/cop/lint/empty_conditional_body.rb
rubocop-1.20.0 lib/rubocop/cop/lint/empty_conditional_body.rb
rubocop-1.19.1 lib/rubocop/cop/lint/empty_conditional_body.rb
rails_mini_profiler-0.2.0 vendor/bundle/ruby/3.0.0/gems/rubocop-1.18.3/lib/rubocop/cop/lint/empty_conditional_body.rb
rubocop-1.19.0 lib/rubocop/cop/lint/empty_conditional_body.rb
rubocop-1.18.4 lib/rubocop/cop/lint/empty_conditional_body.rb
rubocop-1.18.3 lib/rubocop/cop/lint/empty_conditional_body.rb
rubocop-1.18.2 lib/rubocop/cop/lint/empty_conditional_body.rb
rubocop-1.18.1 lib/rubocop/cop/lint/empty_conditional_body.rb
rubocop-1.18.0 lib/rubocop/cop/lint/empty_conditional_body.rb
rubocop-1.17.0 lib/rubocop/cop/lint/empty_conditional_body.rb
rubocop-1.16.1 lib/rubocop/cop/lint/empty_conditional_body.rb
rubocop-1.16.0 lib/rubocop/cop/lint/empty_conditional_body.rb
rubocop-1.15.0 lib/rubocop/cop/lint/empty_conditional_body.rb
cocRb-0.1.0 .bundle/ruby/3.0.0/gems/rubocop-1.14.0/lib/rubocop/cop/lint/empty_conditional_body.rb
rubocop-1.14.0 lib/rubocop/cop/lint/empty_conditional_body.rb
rubocop-1.13.0 lib/rubocop/cop/lint/empty_conditional_body.rb
rubocop-1.12.1 lib/rubocop/cop/lint/empty_conditional_body.rb
rubocop-1.12.0 lib/rubocop/cop/lint/empty_conditional_body.rb