class

Ameba::Rule::Style::GuardClause

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

Use a guard clause instead of wrapping the code inside a conditional expression

# bad
def test
  if something
    work
  end
end

# good
def test
  return unless something

  work
end

# also good
def test
  work if something
end

# bad
if something
  raise "exception"
else
  ok
end

# good
raise "exception" if something
ok

# bad
if something
  foo || raise("exception")
else
  ok
end

# good
foo || raise("exception") if something
ok

YAML configuration example:

Style/GuardClause:
  Enabled: true

Constants

MSG = "Use a guard clause (`%s`) instead of wrapping the code inside a conditional expression"

Constructors

new(config = nil)

Use a guard clause instead of wrapping the code inside a conditional expression

# bad
def test
  if something
    work
  end
end

# good
def test
  return unless something

  work
end

# also good
def test
  work if something
end

# bad
if something
  raise "exception"
else
  ok
end

# good
raise "exception" if something
ok

# bad
if something
  foo || raise("exception")
else
  ok
end

# good
foo || raise("exception") if something
ok

YAML configuration example:

Style/GuardClause:
  Enabled: 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

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::Def)
Source
test(source, node : Crystal::If | Crystal::Unless)
Source
test(source)
Source