class

Ameba::Rule::Lint::TrailingRescueException

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

A rule that prohibits the misconception about how trailing rescue statements work, preventing Paths (exception class names or otherwise) from being used as the trailing value. The value after the trailing rescue statement is the value to use if an exception occurs, not the exception class to rescue from.

For example, this is considered invalid - if an exception occurs, response will be assigned with the value of IO::Error instead of nil:

response = HTTP::Client.get("http://www.example.com") rescue IO::Error

And should instead be written as this in order to capture only IO::Error exceptions:

response = begin
  HTTP::Client.get("http://www.example.com")
rescue IO::Error
  "default value"
end

Or to rescue all exceptions (instead of just IO::Error):

response = HTTP::Client.get("http://www.example.com") rescue "default value"

YAML configuration example:

Lint/TrailingRescueException:
  Enabled: true

Constants

MSG = "Use a block variant of `rescue` to filter by the exception type"

Constructors

new(config = nil)

A rule that prohibits the misconception about how trailing rescue statements work, preventing Paths (exception class names or otherwise) from being used as the trailing value. The value after the trailing rescue statement is the value to use if an exception occurs, not the exception class to rescue from.

For example, this is considered invalid - if an exception occurs, response will be assigned with the value of IO::Error instead of nil:

response = HTTP::Client.get("http://www.example.com") rescue IO::Error

And should instead be written as this in order to capture only IO::Error exceptions:

response = begin
  HTTP::Client.get("http://www.example.com")
rescue IO::Error
  "default value"
end

Or to rescue all exceptions (instead of just IO::Error):

response = HTTP::Client.get("http://www.example.com") rescue "default value"

YAML configuration example:

Lint/TrailingRescueException:
  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::ExceptionHandler)
Source