Skip to content

prefer_contains

v0.4.0 Warning Fix Collection & Type

Comparing .indexOf() to -1 is a common pattern for checking whether an element exists in a collection or a substring exists in a string. The .contains() method expresses this intent more clearly and directly.

.contains() communicates “does this exist?” more clearly than .indexOf() != -1. It is also less error-prone since there is no magic number to get wrong. This rule catches both indexOf(x) == -1 and indexOf(x) != -1 patterns, including reversed operand order.

See also: Iterable.contains

void example() {
final list = [1, 2, 3];
// Use .contains() instead of .indexOf() == -1
final notFound = list.indexOf(1) == -1;
// Use .contains() instead of .indexOf() != -1
final found = list.indexOf(1) != -1;
// Also reversed comparisons
final reversed = -1 == list.indexOf(1);
// Works on strings too
final s = 'hello';
final hasA = s.indexOf('a') != -1;
}
void example() {
final list = [1, 2, 3];
final notFound = !list.contains(1);
final found = list.contains(1);
// Comparing to specific index positions is fine
final isFirst = list.indexOf(1) == 0;
final isThird = list.indexOf(1) == 2;
// Using indexOf for its return value is fine
final idx = list.indexOf(1);
}

To disable this rule:

plugins:
many_lints:
diagnostics:
prefer_contains: false