Modify Rule S1155: Improve description for Dart

This commit is contained in:
Marharyta Nedzelska 2024-08-07 15:28:52 +02:00 committed by Marharyta
parent d6ad5e3c48
commit cea6f025f6

View File

@ -2,6 +2,26 @@
When you call `isEmpty` or `isNotEmpty`, it clearly communicates the code's intention, which is to check if the collection is empty. Using `.length == 0` for this purpose is less direct and makes the code slightly more complex.
The rule also raises issues if the comparisons don't make sense. For example, `length` is always 0 or higher, so you don't need to write the following conditions:
[source,dart]
----
void fun(List<int> myList) {
if (myList.length >= 0) { // Noncompliant, the condition is always true
// do something
}
if (myList.length < 0) { // Noncompliant, the condition is always false
// do something
}
}
----
=== Code examples
==== Noncompliant code example
[source,dart,diff-id=1,diff-type=noncompliant]
----
void fun(List<int> myList) {
@ -16,6 +36,7 @@ void fun(List<int> myList) {
}
----
==== Compliant solution
[source,dart,diff-id=1,diff-type=compliant]
----
@ -33,4 +54,23 @@ void fun(List<int> myList) {
== Resources
* https://dart.dev/tools/linter-rules/prefer_is_empty[Dart Lint rule]
* Dart Docs - https://dart.dev/tools/linter-rules/prefer_is_empty[Dart Linter rule]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
* Use 'isEmpty'/'isNotEmpty' instead of 'length' to test whether the collection is 'empty'/'not empty'.
* The comparison is always 'true'/'false' because the length is always greater than or equal to 0.
=== Highlighting
The condition of the `if` statement.
'''
endif::env-github,rspecator-view[]