prefer-if-elements-to-conditional-expressions
Same as prefer_if_elements_to_conditional_expressions in Dart lints
- Group
- Complexity
- 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
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,
]; 1, flag ? a : b, '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, if (a) b ? 1 : 2 else if (c) d ? 3 : 4 else e ? 5 : 6, if (a) b ? value : -value else c ? value : 0,Valid
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.
{
"linter": {
"rules": {
"complexity": {
"prefer-if-elements-to-conditional-expressions": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/complexity/prefer-if-elements-to-conditional-expressions: <reason>More complexity rules
- prefer-if-null-operators
prefer_if_null_operatorsrecommendedFlags
x == null ? y : x(and the invertedx != null ? x : y). - prefer-immediate-return
prefer-immediate-returnFlags a local variable that is returned on the immediately following statement.
- prefer-iterable-any
prefer_iterable_anyrecommendedFlags
iterable.where(...).isNotEmpty. - prefer-iterable-every
prefer_iterable_everyrecommendedFlags
iterable.where(predicate).isEmptyand filtered-length comparisons. - prefer-moving-to-variable
prefer-moving-to-variableFlags repeated property/getter chains in local variable initializers.
- prefer-null-aware-operators
prefer_null_aware_operatorsrecommendedFlags
x == null ? null : x.y(and the invertedx != null ? x.y : null). - unnecessary-const
unnecessary_constrecommendedFlags a redundant
constkeyword inside an already-const context. - unnecessary-getters-setters
unnecessary_getters_settersrecommendedFlags a trivial getter/setter pair that only reads and writes a private field.