class

Ameba::Rule::Style::RedundantNext

Inherits Ameba::AST::Util < YAML::Serializable < Ameba::Rule::Base < Ameba::Config::RuleConfig < Reference < Object

A rule that disallows redundant next expressions. A next keyword allows a block to skip to the next iteration early, however, it is considered redundant in cases where it is the last expression in a block or combines into the node which is the last in a block.

For example, this is considered invalid:

block do |v|
  next v + 1
end
block do |v|
  case v
  when .nil?
    next "nil"
  when .blank?
    next "blank"
  else
    next "empty"
  end
end

And has to be written as the following:

block do |v|
  v + 1
end
block do |v|
  case arg
  when .nil?
    "nil"
  when .blank?
    "blank"
  else
    "empty"
  end
end

Configuration params

  1. allow_multi_next, default: true

Allows end-user to configure whether to report or not the next statements which yield tuple literals i.e.

block do
  next a, b
end

If this param equals to false, the block above will be forced to be written as:

block do
  {a, b}
end
  1. allow_empty_next, default: true

Allows end-user to configure whether to report or not the next statements without arguments. Sometimes such statements are used to yield the nil value explicitly.

block do
  @foo = :empty
  next
end

If this param equals to false, the block above will be forced to be written as:

block do
  @foo = :empty
  nil
end

YAML config example

Style/RedundantNext:
  Enabled: true
  AllowMultiNext: true
  AllowEmptyNext: true

Constants

MSG = "Redundant `next` detected"

Constructors

new(config = nil)

A rule that disallows redundant next expressions. A next keyword allows a block to skip to the next iteration early, however, it is considered redundant in cases where it is the last expression in a block or combines into the node which is the last in a block.

For example, this is considered invalid:

block do |v|
  next v + 1
end
block do |v|
  case v
  when .nil?
    next "nil"
  when .blank?
    next "blank"
  else
    next "empty"
  end
end

And has to be written as the following:

block do |v|
  v + 1
end
block do |v|
  case arg
  when .nil?
    "nil"
  when .blank?
    "blank"
  else
    "empty"
  end
end

Configuration params

  1. allow_multi_next, default: true

Allows end-user to configure whether to report or not the next statements which yield tuple literals i.e.

block do
  next a, b
end

If this param equals to false, the block above will be forced to be written as:

block do
  {a, b}
end
  1. allow_empty_next, default: true

Allows end-user to configure whether to report or not the next statements without arguments. Sometimes such statements are used to yield the nil value explicitly.

block do
  @foo = :empty
  next
end

If this param equals to false, the block above will be forced to be written as:

block do
  @foo = :empty
  nil
end

YAML config example

Style/RedundantNext:
  Enabled: true
  AllowMultiNext: true
  AllowEmptyNext: true
Source
new(*, __context_for_yaml_serializable ctx : YAML::ParseContext, __node_for_yaml_serializable node : YAML::Nodes::Node)

Class methods

deprecated?

Returns true if this rule is deprecated, false otherwise.

deprecation_reason

Returns the deprecation reason for this rule, if there is any.

documentation_url

Returns the documentation URL for this rule.

Ameba::Rule::Lint::Syntax.documentation_url
# => "https://crystal-ameba.org/api/master/Ameba/Rule/Lint/Syntax.html"
parsed_doc

Returns the documentation for this rule, if there is any.

module Ameba
  # This is a test rule.
  # Does nothing.
  class Rule::MyRule < Rule::Base
    def test(source)
    end
  end
end

Ameba::Rule::MyRule.parsed_doc # => "This is a test rule.\nDoes nothing."
to_json_schema(builder : JSON::Builder) : Nil
Source

Instance methods

allow_empty_next=(allow_empty_next : Bool)
allow_empty_next?
allow_multi_next=(allow_multi_next : Bool)
allow_multi_next?
description
description=(description : String)
enabled=(enabled : Bool)
enabled?
excluded
excluded=(excluded : Set(String) | Nil)
severity
severity=(severity : Ameba::Severity)
since_version
Source
since_version=(since_version : String)
test(source, node : Crystal::Next, visitor : AST::RedundantControlExpressionVisitor)
Source
test(source, node : Crystal::Block)
Source