prefer-if-elements-to-conditional-expressions

Same as prefer_if_elements_to_conditional_expressions in Dart lints

Path
lint/complexity/prefer-if-elements-to-conditional-expressions
Default
Off, opt in
Origin
Dart lints
Rule
366 of 512

Prefers collection if elements to conditional expressions in list and set literals.

Since Dart 2.3 a list or set literal can choose an element with an if element, so [a, b ? c : d] reads better as [a, if (b) c else d]. The rule flags a conditional expression that is itself a list or set element, or the then/else element of a collection if, or the body of a collection for. Conditionals nested deeper inside an element ([f(b ? c : d)]) and map entries are left alone, as the official lint does.

Invalid

example.dartdart
List<int> lists(bool flag, int a, int b) {
  return [
    1,
    flag ? a : b,
  ];
}

Set<String> sets(bool flag) {
  return {
    'x',
    flag ? 'y' : 'z',
  };
}

List<int> nestedIf(bool a, bool b) => [
  if (a) b ? 1 : 2,
];

List<int> nestedElse(bool a, bool b) => [
  if (a) 0 else b ? 1 : 2,
];

List<int> forBody(List<int> values) => [
  for (final v in values) v.isEven ? v : -v,
];

const typed = <int>[
  true ? 1 : 2,
];

List<int> nestedElseIf(bool a, bool b, bool c, bool d, bool e) => [
  if (a)
    b ? 1 : 2
  else if (c)
    d ? 3 : 4
  else
    e ? 5 : 6,
];

List<int> ifInsideFor(List<int> values, bool a, bool b, bool c) => [
  for (final value in values)
    if (a)
      b ? value : -value
    else
      c ? value : 0,
];
Use an `if` element to conditionally add elements.
3 1,
4 flag ? a : b,
Use an `if` element to conditionally add elements.
10 'x',
11 flag ? 'y' : 'z',
Use an `if` element to conditionally add elements.
15List<int> nestedIf(bool a, bool b) => [
16 if (a) b ? 1 : 2,
Use an `if` element to conditionally add elements.
19List<int> nestedElse(bool a, bool b) => [
20 if (a) 0 else b ? 1 : 2,
Use an `if` element to conditionally add elements.
23List<int> forBody(List<int> values) => [
24 for (final v in values) v.isEven ? v : -v,
Use an `if` element to conditionally add elements.
27const typed = <int>[
28 true ? 1 : 2,
Use an `if` element to conditionally add elements.
32 if (a)
33 b ? 1 : 2
Use an `if` element to conditionally add elements.
34 else if (c)
35 d ? 3 : 4
Use an `if` element to conditionally add elements.
36 else
37 e ? 5 : 6,
Use an `if` element to conditionally add elements.
42 if (a)
43 b ? value : -value
Use an `if` element to conditionally add elements.
44 else
45 c ? value : 0,

Valid

example.dartdart
List<int> lists(bool flag, int a, int b) {
  return [
    1,
    if (flag) a else b,
  ];
}

int pick(bool flag, int a, int b) => flag ? a : b;

List<int> nestedArgument(bool flag, int a, int b) => [pick(flag, a, b), (flag ? a : b) + 1];

Map<String, int> maps(bool flag) => {'k': flag ? 1 : 2};

List<int> spread(bool flag, List<int> a, List<int> b) => [...flag ? a : b];

List<int> nullAware(bool flag, int? a) => [?a];

void call(bool flag) => print([1, 2].map((v) => flag ? v : -v));

Configure prefer-if-elements-to-conditional-expressions

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

falcon.jsonjson
{
  "linter": {
    "rules": {
      "complexity": {
        "prefer-if-elements-to-conditional-expressions": "error"
      }
    }
  }
}

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

example.dartdart
// falcon-ignore lint/complexity/prefer-if-elements-to-conditional-expressions: <reason>
  • prefer-if-null-operatorsprefer_if_null_operatorsrecommended

    Flags x == null ? y : x (and the inverted x != null ? x : y).

  • prefer-immediate-returnprefer-immediate-return

    Flags a local variable that is returned on the immediately following statement.

  • prefer-iterable-anyprefer_iterable_anyrecommended

    Flags iterable.where(...).isNotEmpty.

  • prefer-iterable-everyprefer_iterable_everyrecommended

    Flags iterable.where(predicate).isEmpty and filtered-length comparisons.

  • prefer-moving-to-variableprefer-moving-to-variable

    Flags repeated property/getter chains in local variable initializers.

  • prefer-null-aware-operatorsprefer_null_aware_operatorsrecommended

    Flags x == null ? null : x.y (and the inverted x != null ? x.y : null).

  • unnecessary-constunnecessary_constrecommended

    Flags a redundant const keyword inside an already-const context.

  • unnecessary-getters-settersunnecessary_getters_settersrecommended

    Flags a trivial getter/setter pair that only reads and writes a private field.