rspec/rules/S7055/dart/rule.adoc
Antonio Aversa d41b77b623
DART-213 Modify rules S1161, S7055: update descriptions and messages (#4799)
* DART-213 Modify rule S1161: update message details

* S7055: Update description

* Update rule.adoc
2025-03-19 15:28:12 +00:00

91 lines
2.1 KiB
Plaintext

== Why is this an issue?
In Dart, you can use wildcard names, that are names consisting on underscores only: `++_++`, `++__++`, `++___++`, ...
Usually, wildcard names should be used to name the variable/parameter that won't be directly used but needs to be declared.
For example, as counter in the for-loop:
`for (var _ in [1, 2, 3]) doSomething();`
or as a field, in the pattern:
[source,dart]
----
void sayHello(Person p) {
switch (p) {
case (name, _):
print('Hello, $name!');
}
}
----
In Dart 3.6 and below, it is possible to read `++_++` wildcard variables, for example, `print(_);`.
However, such behavior has changed in Dart 3.7, making `++_++` variables non-binding and breaking the code, where those variables are used.
[source,dart]
----
void sayHello(String _) {
print('Hello, $_!'); // Compiler error in Dart 3.7 and above: _ is non-binding
}
----
To avoid problems during upgrades from Dart 3.6-, it's recommended to refactor the code, to not use wildcard variables.
=== What is the potential impact?
Once the breaking change is introduced, you will need to update all the code using wildcard variables or parameters to be able to upgrade to the latest Dart language versions.
== How to fix it
If the variable/parameter are supposed to be read, then give them a proper name.
=== Code examples
==== Noncompliant code example
[source,dart,diff-id=1,diff-type=noncompliant]
----
void sayHello(String __) {
print('Hello, $__!'); // Noncompliant in Dart 3.6 and below
}
----
==== Compliant solution
[source,dart,diff-id=1,diff-type=compliant]
----
void sayHello(String name) {
print('Hello, $name!');
}
----
== Resources
=== Documentation
* Dart Docs - https://dart.dev/tools/linter-rules/no_wildcard_variable_uses[Dart Linter rule - no_wildcard_variable_uses]
* Dart Docs - https://dart.dev/language/pattern-types#wildcard[wildcard pattern]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
* The referenced identifier is a wildcard.
=== Highlighting
Wildcard identifier
'''
== Comments And Links
(visible only on this page)
endif::env-github,rspecator-view[]