Rules / Style / prefer-is-not-empty
prefer-is-not-empty
Flags .length comparisons equivalent to a .isNotEmpty check.
Collections and strings expose an isNotEmpty getter that reads as plain
intent and, for lazy iterables, can avoid computing the full length.
Comparing length against a constant makes the reader infer the meaning, so
list.isNotEmpty should replace forms such as length != 0, length > 0,
and length >= 1, along with their operand-swapped mirrors (0 != length,
0 < length, 1 <= length). The rule matches any .length property access
against the relevant integer literal; it does not verify the receiver
actually offers isNotEmpty.
Invalid
example.dartdart
void bad() {
if (list.length != 0) return;
if (0 != items.length) return;
if (str.length > 0) return;
if (0 < map.length) return;
if (set.length >= 1) return;
if (1 <= names.length) return;
}
Use 'isNotEmpty' instead of comparing 'length' to 0.
1void bad() {
2 if (list.length != 0) return;
∙
Use 'isNotEmpty' instead of comparing 'length' to 0.
2 if (list.length != 0) return;
3 if (0 != items.length) return;
∙
Use 'isNotEmpty' instead of comparing 'length' to 0.
3 if (0 != items.length) return;
4 if (str.length > 0) return;
∙
Use 'isNotEmpty' instead of comparing 'length' to 0.
4 if (str.length > 0) return;
5 if (0 < map.length) return;
∙
Use 'isNotEmpty' instead of comparing 'length' to 0.
5 if (0 < map.length) return;
6 if (set.length >= 1) return;
∙
Use 'isNotEmpty' instead of comparing 'length' to 0.
6 if (set.length >= 1) return;
7 if (1 <= names.length) return;
∙
Valid
example.dartdart
void good() {
if (list.isNotEmpty) return;
if (items.length == 0) return;
if (str.length < 1) return;
if (map.length == 3) return;
if (set.length > 5) return;
if (count != 0) return;
}
How to configure
Set the severity of prefer-is-not-empty in your falcon.json:
falcon.jsonjson
{
"linter": {
"rules": {
"style": {
"prefer-is-not-empty": "error"
}
}
}
}