use-is-even-rather-than-modulo
Same as use_is_even_rather_than_modulo in Dart lints
- Group
- Performance
- Path
lint/performance/use-is-even-rather-than-modulo- Default
- Off, opt in
- Origin
- Dart lints
- Rule
- 493 of 512
Prefers isEven/isOdd to comparing x % 2 with 0 or 1.
For an int x, x % 2 == 0 is x.isEven and x % 2 == 1 is x.isOdd;
the getters say what is meant. The rule fires on == whose left operand is
<expr> % 2 with static type int and whose right operand is an integer
literal: 0 suggests isEven, any other literal isOdd (the official rule
does not check the value). Comparisons in a constant context (parameter
default values are not one), and in the assert initializers of a const
constructor, are exempt because isEven is not a constant expression.
Invalid
bool even(int x) => x % 2 == 0;
bool odd(int x) => x % 2 == 1;
void loop(List<int> values) {
for (final v in values) {
if (v % 2 == 0) print(v);
}
final int count = values.length;
print(count % 2 == 1);
}
class Grid {
Grid(this.size) : assert(size % 2 == 0);
final int size;
bool get centered => size % 2 == 1;
}
bool notTwo(int x) => x % 2 == 2;
void defaults([bool flag = 3 % 2 == 1]) {}
class Holder<T> {
Holder(this.test);
final bool Function(T value) test;
}
extension Filter<T> on List<T> {
List<T> keep(bool Function(T value) test) => this;
}
void take(bool Function(int value) test) {}
void closures(List<int> values) {
Holder<int>((i) => i % 2 == 0);
values.keep((i) => i % 2 == 0);
take((i) => i % 2 == 1);
}bool even(int x) => x % 2 == 0;bool even(int x) => x % 2 == 0;bool odd(int x) => x % 2 == 1; for (final v in values) { if (v % 2 == 0) print(v); final int count = values.length; print(count % 2 == 1);class Grid { Grid(this.size) : assert(size % 2 == 0); final int size; bool get centered => size % 2 == 1; bool notTwo(int x) => x % 2 == 2;bool notTwo(int x) => x % 2 == 2;void defaults([bool flag = 3 % 2 == 1]) {}void closures(List<int> values) { Holder<int>((i) => i % 2 == 0); Holder<int>((i) => i % 2 == 0); values.keep((i) => i % 2 == 0); values.keep((i) => i % 2 == 0); take((i) => i % 2 == 1);Valid
bool even(int x) => x.isEven;
bool other(int x) => x % 3 == 0;
bool negative(int x) => x % 2 == -1;
bool asDouble(double x) => x % 2 == 0;
bool asNum(num x) => x % 2 == 0;
bool notEqual(int x) => x % 2 != 0;
const isConstEven = 4 % 2 == 0;
class Grid {
const Grid(this.size) : assert(size % 2 == 0);
final int size;
static const half = 10 % 2 == 0;
}
bool groupedModulo(int i) => (i % 2) == 0;
bool groupedModuloWithComments(int i) => (/* group */ i % 2 /* group */) == 0;
bool groupedDivisor(int i) => i % (2) == 0;
bool groupedComparisonValue(int i) => i % 2 == (0);Configure use-is-even-rather-than-modulo
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"performance": {
"use-is-even-rather-than-modulo": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/performance/use-is-even-rather-than-modulo: <reason>More performance rules
- use-string-buffers
use_string_buffersrecommendedPrefers a
StringBufferto growing aStringwith+inside a loop. - avoid-border-allflutter
Avoids Flutter's non-const
Border.allfactory. - avoid-shrink-wrap-in-listsflutter
Avoids shrink-wrapped Flutter lists nested in rows, columns, or other lists.
- avoid-slow-async-io
avoid_slow_async_ioAvoid the slow asynchronous
dart:iofile-system queries. - avoid-slow-collection-methods
Reports filtered iterable searches that have a direct search-method alternative.
- avoid-unnecessary-collections
Avoid singleton collection literals where SDK APIs accept a single value.
- avoid-unnecessary-consumer-widgetsriverpod
Flags Riverpod consumer widgets whose build reference is unused.
- avoid-unnecessary-futures
Prefer synchronous declarations when an async Future only returns scalars.