Parity tested with modulo two instead of isEven or isOdd

ID

dart.performance.use_is_even_rather_than_modulo

Severity

info

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Idiom

Language

Dart

Description

Reports a comparison of x % 2 against 0 or 1, in either operand order, where the int getters isEven and isOdd express the same test.

Rationale

The modulo form makes the reader work out what is being asked and which side of the comparison is the remainder, and it is easy to get wrong: the sign convention of % differs between languages, so x % 2 == 1 reads as a bug to anyone who expects a negative remainder for a negative operand. isEven and isOdd say the intent directly, are compiled as a single bit test, and cannot be written the wrong way round. The modulo form is also silently wrong the moment the divisor is edited to something other than 2 while the comparison stays at 1.

The following code illustrates the pattern detected by this rule:

class Rows {
  // FLAGGED: Parity tested with modulo two instead of isEven or isOdd
  bool isEvenRow(int index) => index % 2 == 0;
}

Remediation

Replace x % 2 == 0 and x % 2 != 1 with x.isEven, and x % 2 == 1 and x % 2 != 0 with x.isOdd. Both getters are defined on int; when the operand is a double, keep the arithmetic and compare against a remainder explicitly, since neither getter exists there.

Configuration

This detector does not need any configuration.

© 2026 Xygeni. All rights reserved