avoid-unnecessary-length-check
Warns when the if statement's condition has a collection length check that can be removed.
If the if statement has only an inner for-loop, it won't be executed when there are no elements in the collection. In such cases the condition can be removed.
Example
❌ Bad:
void fn(List<int> arr) {
// LINT: This length check is unnecessary because the inner loop will not be executed when there are no elements.
// Consider removing this length check.
if (arr.isNotEmpty) {
for (final item in arr) {
// ...
}
}
// LINT: This length check is unnecessary because the inner loop will not be executed when there are no elements.
// Consider removing this length check.
if (arr.length != 0) {
arr.forEach((_) {
// ...
});
}
}
✅ Good:
void fn(List<int> arr) {
for (final item in arr) {
// ...
}
if (arr.isNotEmpty) {
arr.forEach((_) {
// ...
});
// some other code
}
}