rspec/rules/S3257/dart/rule.adoc
Marharyta c3aa4b07c5
Add more Dart rules (#4062)
* Modify rule S6582: Add Dart language

* Modify rule S6606: Add Dart language

* Modify rule S6207: Add Dart language

* Modify rule S1116: Add Dart example with empty "else"

* Modify rule S927: Add Dart language

* Modify rule S1155: Add Dart language

* Modify rule S2933: Add Dart language

* Modify rule S2971: Add Dart language

* Modify rule S4123: Add Dart language

* Modify rule S120: Add Dart language

* Modify rule S1679: Add Dart language

* Modify rule S2159: Add Dart language

* Modify rule S3257: Add Dart language

* Modify rule S6619: Add Dart language

* Modify rule S3562: Add Dart language

* Modify rule S3240: Add Dart language

* Modify rule S5416: Add Dart language

* Modify rule S2175: Add Dart language

* Modify rule S3962: Add Dart language

* Modify rule S2471: Add Dart language

* Modify rule S3512: Add Dart language

* Modify rule S2432: Add Dart language
2024-07-18 13:20:47 +02:00

32 lines
1.1 KiB
Plaintext

== Why is this an issue?
Dart supports type inference, a mechanism that automatically infers the type of a variable based on its initial value. This means that if you initialize a variable with a particular value, Dart will assume that this variable should always hold that type of value.
Unnecessarily verbose declarations and initializations of collections make it harder to read the code and should be simplified. Therefore, type annotations should be omitted from collection declarations when they can be easily inferred from the initialized or defaulted value.
== How to fix it
Omit explicit type annotations in collection declarations whenever the type can be inferred from the context.
=== Code examples
==== Noncompliant code example
[source,dart,diff-id=1,diff-type=noncompliant]
----
var numbers = Set<int>();
var occurrences = Map<String, int>();
----
==== Compliant solution
[source,dart,diff-id=1,diff-type=compliant]
----
var numbers = <int>{};
var occurrences = <String, int>{};
----
== Resources
* https://dart.dev/tools/linter-rules/prefer_collection_literals[Dart Lint rule]