use-is-even-rather-than-modulo

Same as use_is_even_rather_than_modulo in Dart lints

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

example.dartdart
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);
}
Use `isEven` rather than `% 2`.
1bool even(int x) => x % 2 == 0;
Use `isOdd` rather than `% 2`.
1bool even(int x) => x % 2 == 0;
2bool odd(int x) => x % 2 == 1;
Use `isEven` rather than `% 2`.
5 for (final v in values) {
6 if (v % 2 == 0) print(v);
Use `isOdd` rather than `% 2`.
8 final int count = values.length;
9 print(count % 2 == 1);
Use `isEven` rather than `% 2`.
12class Grid {
13 Grid(this.size) : assert(size % 2 == 0);
Use `isOdd` rather than `% 2`.
14 final int size;
15 bool get centered => size % 2 == 1;
Use `isOdd` rather than `% 2`.
17
18bool notTwo(int x) => x % 2 == 2;
Use `isOdd` rather than `% 2`.
18bool notTwo(int x) => x % 2 == 2;
19void defaults([bool flag = 3 % 2 == 1]) {}
Use `isEven` rather than `% 2`.
32void closures(List<int> values) {
33 Holder<int>((i) => i % 2 == 0);
Use `isEven` rather than `% 2`.
33 Holder<int>((i) => i % 2 == 0);
34 values.keep((i) => i % 2 == 0);
Use `isOdd` rather than `% 2`.
34 values.keep((i) => i % 2 == 0);
35 take((i) => i % 2 == 1);

Valid

example.dartdart
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.

falcon.jsonjson
{
  "linter": {
    "rules": {
      "performance": {
        "use-is-even-rather-than-modulo": "error"
      }
    }
  }
}

Or silence one occurrence with a suppression comment; the reason is required.

example.dartdart
// falcon-ignore lint/performance/use-is-even-rather-than-modulo: <reason>