class RuboCop::Cop::Lint::EmptyBlock
Proc.new { }
proc { }
(callable || placeholder).call
end
placeholder = lambda do
allow(subject).to receive(:callable).and_return(-> {})
# bad
@example AllowEmptyLambdas: false
Proc.new { }
proc { }
(callable || placeholder).call
end
placeholder = lambda do
allow(subject).to receive(:callable).and_return(-> {})
# good
@example AllowEmptyLambdas: true (default)
items.each { |item| } # TODO: implement later (inline comment)
end
# TODO: implement later (inner comment)
items.each do |item|
# bad
@example AllowComments: false
items.each { |item| } # TODO: implement later (inline comment)
end
# TODO: implement later (inner comment)
items.each do |item|
# good
@example AllowComments: true (default)
items.each { |item| puts item }
# good
items.each { |item| }
# bad
@example
applies to procs.
empty lambdas and procs is called ‘AllowEmptyLambdas`, even though it also
NOTE: For backwards compatibility, the configuration that allows/disallows
Empty lambdas and procs are ignored by default.
be clearer what we’re aiming for.
Such empty blocks are typically an oversight or we should provide a comment
Checks for blocks without a body.
def allow_comment?(node)
def allow_comment?(node) return false unless processed_source.contains_comment?(node.source_range) line_comment = processed_source.comment_at_line(node.source_range.line) !line_comment || !comment_disables_cop?(line_comment.source) end
def allow_empty_lambdas?
def allow_empty_lambdas? cop_config['AllowEmptyLambdas'] end
def comment_disables_cop?(comment)
def comment_disables_cop?(comment) regexp_pattern = "# rubocop : (disable|todo) ([^,],)* (all|#{cop_name})" Regexp.new(regexp_pattern.gsub(' ', '\s*')).match?(comment) end
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler return if node.body return if allow_empty_lambdas? && node.lambda_or_proc? return if cop_config['AllowComments'] && allow_comment?(node) add_offense(node) end