class

Ameba::Rule::Lint::DuplicateBranch

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

Checks that there are no repeated bodies within if/unless, case-when, case-in and rescue constructs.

This is considered invalid:

if foo
  do_foo
  do_something_else
elsif bar
  do_foo
  do_something_else
end

And this is valid:

if foo || bar
  do_foo
  do_something_else
end

With IgnoreLiteralBranches: true, branches are not registered as offenses if they return a basic literal value (string, symbol, integer, float, true, false, or nil), or return an array, hash, regexp or range that only contains one of the above basic literal values.

With IgnoreConstantBranches: true, branches are not registered as offenses if they return a constant value.

With IgnoreDuplicateElseBranch: true, in conditionals with multiple branches, duplicate 'else' branches are not registered as offenses.

YAML configuration example:

Lint/DuplicateBranch:
  Enabled: true
  IgnoreLiteralBranches: false
  IgnoreConstantBranches: false
  IgnoreDuplicateElseBranch: false

Constants

MSG = "Duplicate branch body detected"

Constructors

new(config = nil)

Checks that there are no repeated bodies within if/unless, case-when, case-in and rescue constructs.

This is considered invalid:

if foo
  do_foo
  do_something_else
elsif bar
  do_foo
  do_something_else
end

And this is valid:

if foo || bar
  do_foo
  do_something_else
end

With IgnoreLiteralBranches: true, branches are not registered as offenses if they return a basic literal value (string, symbol, integer, float, true, false, or nil), or return an array, hash, regexp or range that only contains one of the above basic literal values.

With IgnoreConstantBranches: true, branches are not registered as offenses if they return a constant value.

With IgnoreDuplicateElseBranch: true, in conditionals with multiple branches, duplicate 'else' branches are not registered as offenses.

YAML configuration example:

Lint/DuplicateBranch:
  Enabled: true
  IgnoreLiteralBranches: false
  IgnoreConstantBranches: false
  IgnoreDuplicateElseBranch: false
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

description
description=(description : String)
enabled=(enabled : Bool)
enabled?
excluded
excluded=(excluded : Set(String) | Nil)
ignore_constant_branches=(ignore_constant_branches : Bool)
ignore_constant_branches?
ignore_duplicate_else_branch=(ignore_duplicate_else_branch : Bool)
ignore_duplicate_else_branch?
ignore_literal_branches=(ignore_literal_branches : Bool)
ignore_literal_branches?
severity
severity=(severity : Ameba::Severity)
since_version
Source
since_version=(since_version : String)
test(source, node : Crystal::If | Crystal::Unless | Crystal::Case | Crystal::ExceptionHandler, ifs : Enumerable(Crystal::If) | Nil = nil)
Source
test(source)
Source