class

Ameba::Rule::Performance::ChainedCallWithNoBang

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

This rule is used to identify usage of chained calls not utilizing the bang method variants.

For example, this is considered inefficient:

names = %w[Alice Bob]
chars = names
  .flat_map(&.chars)
  .uniq
  .sort

And can be written as this:

names = %w[Alice Bob]
chars = names
  .flat_map(&.chars)
  .uniq!
  .sort!

YAML configuration example:

Performance/ChainedCallWithNoBang:
  Enabled: true
  CallNames:
    - uniq
    - unstable_sort
    - sort
    - sort_by
    - shuffle
    - reverse

Constants

ALLOCATING_METHOD_NAMES = ["keys", "values", "values_at", "map", "map_with_index", "flat_map", "compact_map", "flatten", "compact", "select", "reject", "sample", "group_by", "chunks", "tally", "merge", "combinations", "repeated_combinations", "permutations", "repeated_permutations", "transpose", "invert", "split", "chars", "lines", "captures", "named_captures", "clone"] of ::String

All these methods allocate a new object

MSG = "Use bang method variant `%s!` after chained `%s` call"

Constructors

This rule is used to identify usage of chained calls not utilizing the bang method variants.

For example, this is considered inefficient:

names = %w[Alice Bob]
chars = names
  .flat_map(&.chars)
  .uniq
  .sort

And can be written as this:

names = %w[Alice Bob]
chars = names
  .flat_map(&.chars)
  .uniq!
  .sort!

YAML configuration example:

Performance/ChainedCallWithNoBang:
  Enabled: true
  CallNames:
    - uniq
    - unstable_sort
    - sort
    - sort_by
    - shuffle
    - reverse
new(config = nil)

This rule is used to identify usage of chained calls not utilizing the bang method variants.

For example, this is considered inefficient:

names = %w[Alice Bob]
chars = names
  .flat_map(&.chars)
  .uniq
  .sort

And can be written as this:

names = %w[Alice Bob]
chars = names
  .flat_map(&.chars)
  .uniq!
  .sort!

YAML configuration example:

Performance/ChainedCallWithNoBang:
  Enabled: true
  CallNames:
    - uniq
    - unstable_sort
    - sort
    - sort_by
    - shuffle
    - reverse
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

call_names
call_names=(call_names : Array(String))
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::Call)
Source
test(source)
Source