prefer-contains
Same as prefer_contains in Dart lints
- Group
- Performance
- Path
lint/performance/prefer-contains- Default
- On (recommended)
- Origin
- Dart lints
- Rule
- 334 of 512
Prefers contains to comparing the result of indexOf with a constant.
The rule looks at a
comparison (==, !=, <, <=, >, >=) of an Iterable or String
receiver's indexOf(...) call (optionally behind as casts) with an
integer constant <= 0 on the other side: an integer literal, a negated
literal or identifier, or an identifier naming an int constant. When the
call has exactly two arguments the second (the start index) must be the
constant 0. Comparisons with -1 (==, !=, <= and > after
normalizing the call to the left) and 0 (>=, <) are membership tests
that contains states directly; comparisons that are always true or false
(< -1, >= -1, or any comparison with a value below -1) are reported
too. A constant on the right is preferred: when the right operand is an
integer constant the left operand is not considered.
Invalid
bool missing(List<String> values, String value) {
return values.indexOf(value) == -1;
}
bool present(String text, String value) {
return text.indexOf(value) >= 0;
}
bool defaultValue([bool found = 'abc'.indexOf('a') >= 0]) => found;
bool swappedPresent(List<String> values, String value) {
return -1 < values.indexOf(value);
}
bool swappedMissing(String text, String value) {
return 0 <= text.indexOf(value);
}
bool otherForms(String text, String value) {
if (text.indexOf(value) < 0) return false;
if (text.indexOf(value) <= -1) return false;
if (-1 >= text.indexOf(value)) return false;
return text.indexOf(value) != -1;
}
bool hexBounds(String text, String value) {
if (text.indexOf(value) != -0x1) return true;
return text.indexOf(value) >= 0x0;
}
class SearchState {
final bool found;
SearchState(String text) : found = text.indexOf('x') >= 0;
bool get fromGetter {
String text = 'abc';
return text.indexOf('a') >= 0;
}
set values(List<String> values) {
values.indexOf('x') >= 0;
}
bool operator ==(Object other) {
String text = other.toString();
return text.indexOf('x') >= 0;
}
}
void scoped(List<String> values, List<String> texts) {
bool local(String text) => text.indexOf('x') >= 0;
for (String text in texts) {
text.indexOf('x') >= 0;
}
try {
throw 'x';
} on String catch (text) {
text.indexOf('x') >= 0;
}
final (String text,) = ('abc',);
text.indexOf('x') >= 0;
}
const minusOne = -1;
const zero = 0;
bool startArguments(String text, String value) {
if (text.indexOf(value, 0) >= 0) return true;
if (text.indexOf(value, zero) >= 0) return true;
return text.indexOf('x', 1, 2) >= 0;
}
bool constants(List<int> values, String text, List<int>? maybe, Object other) {
if (values.indexOf(1) == minusOne) return true;
if (text.indexOf('x') >= zero) return true;
if (maybe?.indexOf(1) == -1) return true;
if ((other as List<int>).indexOf(1) == -1) return true;
return 0 > values.indexOf(1);
}
bool alwaysTrueOrFalse(String text) {
if (text.indexOf('x') < -1) return true;
if (text.indexOf('x') >= -1) return true;
if (text.indexOf('x') == -2) return true;
return text.indexOf('x') != -2;
}bool missing(List<String> values, String value) { return values.indexOf(value) == -1;bool present(String text, String value) { return text.indexOf(value) >= 0;}bool defaultValue([bool found = 'abc'.indexOf('a') >= 0]) => found;bool swappedPresent(List<String> values, String value) { return -1 < values.indexOf(value);bool swappedMissing(String text, String value) { return 0 <= text.indexOf(value);bool otherForms(String text, String value) { if (text.indexOf(value) < 0) return false; if (text.indexOf(value) < 0) return false; if (text.indexOf(value) <= -1) return false; if (text.indexOf(value) <= -1) return false; if (-1 >= text.indexOf(value)) return false; if (-1 >= text.indexOf(value)) return false; return text.indexOf(value) != -1;bool hexBounds(String text, String value) { if (text.indexOf(value) != -0x1) return true; if (text.indexOf(value) != -0x1) return true; return text.indexOf(value) >= 0x0; final bool found; SearchState(String text) : found = text.indexOf('x') >= 0; String text = 'abc'; return text.indexOf('a') >= 0; set values(List<String> values) { values.indexOf('x') >= 0; String text = other.toString(); return text.indexOf('x') >= 0;void scoped(List<String> values, List<String> texts) { bool local(String text) => text.indexOf('x') >= 0; for (String text in texts) { text.indexOf('x') >= 0; } on String catch (text) { text.indexOf('x') >= 0; final (String text,) = ('abc',); text.indexOf('x') >= 0;bool startArguments(String text, String value) { if (text.indexOf(value, 0) >= 0) return true; if (text.indexOf(value, 0) >= 0) return true; if (text.indexOf(value, zero) >= 0) return true; if (text.indexOf(value, zero) >= 0) return true; return text.indexOf('x', 1, 2) >= 0;bool constants(List<int> values, String text, List<int>? maybe, Object other) { if (values.indexOf(1) == minusOne) return true; if (values.indexOf(1) == minusOne) return true; if (text.indexOf('x') >= zero) return true; if (text.indexOf('x') >= zero) return true; if (maybe?.indexOf(1) == -1) return true; if (maybe?.indexOf(1) == -1) return true; if ((other as List<int>).indexOf(1) == -1) return true; if ((other as List<int>).indexOf(1) == -1) return true; return 0 > values.indexOf(1);bool alwaysTrueOrFalse(String text) { if (text.indexOf('x') < -1) return true; if (text.indexOf('x') < -1) return true; if (text.indexOf('x') >= -1) return true; if (text.indexOf('x') >= -1) return true; if (text.indexOf('x') == -2) return true; if (text.indexOf('x') == -2) return true; return text.indexOf('x') != -2;Valid
bool present(List<String> values, String value) => values.contains(value);
class Search { int indexOf(Object value) => 0; }
bool custom(Search value) => value.indexOf('x') == -1;
bool listStart(List<String> values, String value) => values.indexOf(value, 1) >= 0;
bool stringNonzeroStart(String text, String value) => text.indexOf(value, 2) >= 0;
const minusOneDouble = -1.0;
bool doubleConstant(String text) => text.indexOf('x') == minusOneDouble;
bool assigned(String text) {
int index;
return (index = text.indexOf('x')) != -1;
}
bool positive(String text) => text.indexOf('x') > 0;
class List<E> {
int indexOf(E value) => 0;
}
bool ambiguousList(List<String> values, String value) => values.indexOf(value) >= 0;Configure prefer-contains
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"performance": {
"prefer-contains": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/performance/prefer-contains: <reason>More performance rules
- prefer-correct-edge-insets-constructor
prefer-correct-edge-insets-constructorrecommendedflutterFlags an
EdgeInsets.only(...)that a more specific constructor expresses better. - prefer-declaring-const-constructor
prefer_declaring_const_constructorflutterFlags a generative constructor that is legally eligible to be
const. - prefer-foreach
prefer_foreachPrefers
forEachto afor-in loop that only passes each element to a function. - prefer-return-await
Await returned SDK Futures inside a try body protected by a catch clause.
- prefer-single-setstateflutter
Merge consecutive Flutter State.setState statements in the same block.
- prefer-use-callbackflutter_hooks
Prefer
useCallbackfor synchronous memoized function literals. - prefer-use-memoflutter_hooks
Prefer memoizing freshly allocated collection values used as hook keys.
- prefer-using-list-viewflutter
Suggests considering
ListViewfor aSingleChildScrollViewcontaining a plain Flutter Column with at least two unconditional children in a list literal.