no-self-comparisons
Same as no_self_comparisons in Pyramid Lint
- Group
- Suspicious
- Path
lint/suspicious/no-self-comparisons- Default
- On (recommended)
- Origin
- Pyramid Lint
- Rule
- 297 of 512
Flags proven self-comparisons of side-effect-free scalar values.
Getter reads, index operations, calls, and user-defined operator receivers are intentionally excluded: evaluating the same syntax twice need not produce the same value, and equality/ordering may be overloaded. A scalar read type does not establish stability: unqualified top-level and inherited getter names still execute a getter on each evaluation.
Invalid
bool a(int x) => x == x;
bool b(int x) => x != x;
bool c(int x) => x < x;
class Foo {
int value = 0;
bool check() => value >= value;
bool nested(List<int> a) => a[0] == a[0];
}
void d(int x) {
if (x > x) {
print('never');
}
}
// A local binding shadows the effectful top-level getter.
int get changing => 0;
bool shadowedGetter(int changing) => changing == changing;bool a(int x) => x == x; bool b(int x) => x != x; bool c(int x) => x < x; bool check() => value >= value;void d(int x) { if (x > x) {int get changing => 0;bool shadowedGetter(int changing) => changing == changing;Valid
bool a(int x, int y) => x == y;
bool b(int x) => x == x + 1;
bool c(int x) => x < x + 1;
class Foo {
int a = 0;
int b = 0;
bool check() => a == b;
bool indexed(List<int> l) => l[0] == l[1];
}
void d(int x, int y) {
if (x > y) {
print('maybe');
}
}
bool e(int x) => -x == x;
// Side-effecting calls evaluate to different values, so identical text is
// not a self-comparison.
bool popEqual(List<int> l) => l.removeLast() == l.removeLast();
bool nowEqual() => DateTime.now() == DateTime.now();
int nextId = 0;
int gen() => nextId++;
bool genEqual() => gen() == gen();
// Prefix ++/-- mutate the operand, so the two sides differ.
bool preIncrement(int x) => ++x == ++x;
bool preDecrement(int x) => --x == --x;
int get changing => nextId++;
bool getterEqual() => changing == changing;
bool sameIndex(List<int> values) => values[0] == values[0];
class WeirdEquality {
@override
bool operator ==(Object other) => false;
bool compare() => this == this;
}
// Resolving a getter's scalar type must not make repeated reads stable.
bool negatedGetterEqual() => -changing == -changing;
class ChangingValue {
int get value => nextId++;
bool equal() => value == value;
}
class InheritedChangingValue extends ChangingValue {
bool inheritedEqual() => value == value;
}Configure no-self-comparisons
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"suspicious": {
"no-self-comparisons": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/suspicious/no-self-comparisons: <reason>More suspicious rules
- no-wildcard-variable-uses
no_wildcard_variable_usesrecommendedFlags references to a wildcard variable or parameter (a name made solely of underscores, such as
_or__). - overridden-fields
overridden_fieldsrecommendedFlags instance fields that redeclare an inherited field.
- prefer-unique-test-namestest
Reports duplicate test descriptions within a group and constant names in loops.
- recursive-getters
recursive_gettersrecommendedFlags a getter that unconditionally returns itself.
- unnecessary-null-aware-assignments
unnecessary_null_aware_assignmentsrecommendedFlags
x ??= null, which can never have an effect. - unnecessary-null-in-if-null-operators
unnecessary_null_in_if_null_operatorsrecommendedFlags
x ?? nullandnull ?? x, where thenulloperand is redundant. - use-rethrow-when-possible
use_rethrow_when_possiblerecommendedFlags
throw einside acatch (e)by matching the exception parameter's name. - avoid-accessing-other-classes-private-members
Enforces class-level encapsulation even inside one Dart library.