+

+ + class Ameba::Rule::Lint::NotNil + +

+ + + + + + + +

+ + + + Overview +

+ +

This rule is used to identify usages of not_nil! calls.

+

For example, this is considered a code smell:

+
names = %w[Alice Bob]
+alice = names.find { |name| name == "Alice" }.not_nil!
+

And can be written as this:

+
names = %w[Alice Bob]
+alice = names.find { |name| name == "Alice" }
+
+if alice
+  # ...
+end
+

YAML configuration example:

+
Lint/NotNil:
+  Enabled: true
+ + + + + +

+ + + + Included Modules +

+
    + +
  • Ameba::AST::Util
  • + +
  • YAML::Serializable
  • + +
  • YAML::Serializable::Strict
  • + +
+ + + + + + + + + + +

+ + + + Defined in: +

+ + + + ameba/rule/lint/not_nil.cr + + +
+ + + + + +

+ + + + Constant Summary +

+ +
+ +
+ MSG = "Avoid using `not_nil!`" +
+ + +
+ NOT_NIL_NAME = "not_nil!" +
+ + +
+ + + +

+ + + + Constructors +

+ + + + +

+ + + + Class Method Summary +

+ + + + +

+ + + + Instance Method Summary +

+ + + + + + + + + +

+ + + + Constructor Detail +

+ +
+
+ + def self.new(ctx : YAML::ParseContext, node : YAML::Nodes::Node) + + # +
+ +
+
+ +
+
+ +
+
+ + def self.new(config = nil) + + # +
+ +
+ +

This rule is used to identify usages of not_nil! calls.

+

For example, this is considered a code smell:

+
names = %w[Alice Bob]
+alice = names.find { |name| name == "Alice" }.not_nil!
+

And can be written as this:

+
names = %w[Alice Bob]
+alice = names.find { |name| name == "Alice" }
+
+if alice
+  # ...
+end
+

YAML configuration example:

+
Lint/NotNil:
+  Enabled: true
+
+ +
+
+ + [View source] + +
+
+ + + + +

+ + + + Class Method Detail +

+ +
+
+ + def self.parsed_doc : String? + + # +
+ +
+ +

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 : Bool + + # +
+ +
+
+ +
+
+ +
+
+ + def enabled=(enabled : Bool) + + # +
+ +
+
+ +
+
+ +
+
+ + def excluded : Array(String)? + + # +
+ +
+
+ +
+
+ +
+
+ + def excluded=(excluded : Array(String)?) + + # +
+ +
+
+ +
+
+ +
+
+ + def severity : Ameba::Severity + + # +
+ +
+
+ +
+
+ +
+
+ + def severity=(severity : Ameba::Severity) + + # +
+ +
+
+ +
+
+ +
+
+ + def test(source, node : Crystal::Call) + + # +
+ +
+
+ + [View source] + +
+
+ +
+
+ + def test(source) + + # +
+ +
+
+ + [View source] + +
+
+ + + + + +