Skip to main content

prefer_conditional_expressions

Highlights simple "if" statements that can be replaced with conditional expressions.

Example config:​

solid_lints:
diagnostics:
prefer_conditional_expressions:
ignore_nested: true

Example​

BAD:​

// LINT
if (x > 0) {
x = 1;
} else {
x = 2;
}

// LINT
if (x > 0) x = 1;
else x = 2;

int fn() {
// LINT
if (x > 0) {
return 1;
} else {
return 2;
}
}

GOOD:​

x = x > 0 ? 1 : 2;

int fn() {
return x > 0 ? 1 : 2;
}

Parameters​

ignore_nested (bool)​

Determines whether to ignore nested if statements:

// Allowed if ignore_nested flag is enabled
if (1 > 0) {
_result = 1 > 2 ? 2 : 1;
} else {
_result = 0;
}