class Ameba::Rule::Style::GuardClause

Overview

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

Included Modules

Defined in:

ameba/rule/style/guard_clause.cr

Constant Summary

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

Constructors

Class Method Summary

Instance Method Summary

Instance methods inherited from module Ameba::AST::Util

abort?(node) abort?, control_exp_code(node : Crystal::ControlExpression, code_lines) control_exp_code, dynamic_literal?(node) : Bool dynamic_literal?, exit?(node) exit?, flow_command?(node, in_loop) flow_command?, flow_expression?(node, in_loop = false) flow_expression?, literal?(node) : Bool literal?, loop?(node) loop?, name_end_location(node) name_end_location, name_location(node) name_location, name_size(node) name_size, node_source(node, code_lines) node_source, path_named?(node, name) : Bool path_named?, raise?(node) raise?, source_between(loc, end_loc, code_lines) : String | Nil source_between, static_literal?(node) : Bool static_literal?

Instance methods inherited from class Ameba::Rule::Base

==(other) ==, catch(source : Source) catch, excluded?(source) excluded?, group group, hash hash, name name, special? special?, test(source : Source, node : Crystal::ASTNode, *opts)
test(source : Source)
test

Class methods inherited from class Ameba::Rule::Base

default_severity : Ameba::Severity default_severity

Macros inherited from class Ameba::Rule::Base

issue_for(*args, **kwargs, &block) issue_for

Macros inherited from module Ameba::Config::RuleConfig

properties(&block) properties

Constructor Detail

def self.new(ctx : YAML::ParseContext, node : YAML::Nodes::Node) #

def self.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

[View source]

Class Method Detail

def self.parsed_doc : String | Nil #

Returns documentation for this rule, if there is any.

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

MyRule.parsed_doc # => "This is a test rule.\nDoes nothing."

Instance Method Detail

def description : String #

def description=(description : String) #

def enabled=(enabled : Bool) #

def enabled? : Bool #

def excluded : Array(String) | Nil #

def excluded=(excluded : Array(String) | Nil) #

def guard_clause_source(source, guard_clause, parent) #

[View source]
def severity : Ameba::Severity #

def severity=(severity : Ameba::Severity) #

def test(source, node : Crystal::Def) #

[View source]
def test(source, node : Crystal::If | Crystal::Unless) #

[View source]
def test(source) #

[View source]