avoid-negated-conditions

Path
lint/suspicious/avoid-negated-conditions
Default
Off, opt in
Origin
Falcon
Rule
96 of 512

Prefer positive conditions when both branches can be swapped. Checks if statements, conditional expressions, and collection/map if/else elements. ignore-null-checks (default false) exempts comparisons with null.

Invalid

example.dartdart
void f(bool flag, dynamic value) {
  if (!flag) {} else {}
  if (value != 1) {} else {}
  if (value is! String) {} else {}
  final a = !flag ? 1 : 2;
  final b = value != null ? 1 : 2;
  final c = value is! int ? 1 : 2;
  final list = [if (!flag) 1 else 2];
  final set = {if (value != 1) 1 else 2};
  final map = {if (value is! int) 1: 2 else 3: 4};
  final chained = [if (!flag) 1 else if (value != 1) 2 else 3];
}
Prefer a positive condition and swap the branches.
1void f(bool flag, dynamic value) {
2 if (!flag) {} else {}
Prefer a positive condition and swap the branches.
2 if (!flag) {} else {}
3 if (value != 1) {} else {}
Prefer a positive condition and swap the branches.
3 if (value != 1) {} else {}
4 if (value is! String) {} else {}
Prefer a positive condition and swap the branches.
4 if (value is! String) {} else {}
5 final a = !flag ? 1 : 2;
Prefer a positive condition and swap the branches.
5 final a = !flag ? 1 : 2;
6 final b = value != null ? 1 : 2;
Prefer a positive condition and swap the branches.
6 final b = value != null ? 1 : 2;
7 final c = value is! int ? 1 : 2;
Prefer a positive condition and swap the branches.
7 final c = value is! int ? 1 : 2;
8 final list = [if (!flag) 1 else 2];
Prefer a positive condition and swap the branches.
8 final list = [if (!flag) 1 else 2];
9 final set = {if (value != 1) 1 else 2};
Prefer a positive condition and swap the branches.
9 final set = {if (value != 1) 1 else 2};
10 final map = {if (value is! int) 1: 2 else 3: 4};
Prefer a positive condition and swap the branches.
10 final map = {if (value is! int) 1: 2 else 3: 4};
11 final chained = [if (!flag) 1 else if (value != 1) 2 else 3];

Valid

example.dartdart
void f(bool flag, dynamic value) {
  if (flag) {} else {}
  if (!flag) return;
  if (value == 1) {} else {}
  if (value is String) {} else {}
  final a = flag ? 1 : 2;
  if (!flag) {} else if (value == 1) {}
  final list = [if (flag) 1 else 2];
  final set = {if (!flag) 1};
  final map = {if (flag) 1: 2 else 3: 4};
  final mapWithoutElse = {if (value != 1) 1: 2};
  final chained = [if (!flag) 1 else if (value == 1) 2];
  final chainedMap = {if (!flag) 1: 2 else if (value == 1) 3: 4};
}

Configure avoid-negated-conditions

Set its severity in falcon.json. See the configuration guide.

falcon.jsonjson
{
  "linter": {
    "rules": {
      "suspicious": {
        "avoid-negated-conditions": "error"
      }
    }
  }
}

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

example.dartdart
// falcon-ignore lint/suspicious/avoid-negated-conditions: <reason>