rspec/rules/S2471/dart/rule.adoc

63 lines
1.5 KiB
Plaintext

== Why is this an issue?
In Dart, there's no concept of "uninitialized memory". Everything must be initialized before use, otherwise a compile-time error is reported. In case of non-nullable type, it has to be explicitly initialized before use, and it can't be initialized with `null`. This is guaranteed by compiler. In case of non-nullable variable, it will be set to `null` implicitly. In both cases there is no need to initialize a variable with `null`.
=== Exceptions
In case of `final` and `const` variables or members, they have to be initialized explicitly, so using `null` there won't trigger this rule.
[source,dart]
----
const int? x = null;
----
=== Noncompliant code example
[source,dart]
----
void f() {
int? x = null;
g(x);
}
----
=== Compliant solution
[source,dart]
----
void f() {
int? x;
g(x);
}
----
== Resources
=== Documentation
* Dart Docs - https://dart.dev/tools/linter-rules/avoid_init_to_null[Dart Linter rule - avoid_init_to_null]
* Dart Docs - https://dart.dev/language/variables#default-values[Dart language - default values]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Redundant initialization to 'null'.
=== Highlighting
The entire initialization expression, including the identifier and the assigned value, but excluding the type: e.g. `i1 = null` in `int? i1 = null`.
'''
== Comments And Links
(visible only on this page)
endif::env-github,rspecator-view[]