dart-lang / linter

Linter for Dart.
https://dart.dev/tools/linter-rules
BSD 3-Clause "New" or "Revised" License
628 stars 172 forks source link

False positive for `use_is_even_rather_than_modulo` #4915

Closed JaffaKetchup closed 2 months ago

JaffaKetchup commented 3 months ago

Describe the issue In a constant expression, the use of x % 2 == 0 is a valid constant expression (assuming x is const), however .isEven is not.

To Reproduce

Example 1:

void main() {
    const x = 1;
    const e = x % 2 == 0; // Working, lint thrown
}

void main() {
    const x = 1;
    const e = x.isEven; // Compilation error, lint not thrown
}

Example 2:

class Example {
    const Example(int x) : assert(x % 2 == 0); // Working, lint thrown
}

class Example {
    const Example(int x) : assert(x.isEven); // Compilation error, lint not thrown
}

Expected behavior use_is_even_rather_than_modulo should not be thrown when the surroundings are const.

This may also apply to other lints that apply similar suggestions: they should all be const-aware.

srawlins commented 2 months ago

Fixed with https://github.com/dart-lang/sdk/commit/4c888ab54cfb6be257351a225c9d157b12cfe692