prefer-returning-condition

Group
Style
Path
lint/style/prefer-returning-condition
Default
Off, opt in
Origin
Falcon
Rule
400 of 512

Flags an if returning a boolean literal followed by the opposite literal.

Return the condition (or its negation) instead of spelling out both boolean outcomes. Only adjacent statements with a plain condition, no else, and one return in the then-branch are checked. Pattern conditions, extra branch work, non-literal results, and equal results are left alone. Explicit else branches are left to prefer-conditional-expressions. The fallback return is highlighted; evaluation order and short-circuit behavior are unchanged.

Invalid

example.dartdart
bool direct(bool value) {
  if (value) return true;
  return false;
}
bool inverted(bool value) {
  if (value) { return false; }
  return true;
}
bool parenthesized(bool value) {
  if ((value)) { return (true); }
  return (false);
}
class Worker {
  bool check(int value) {
    if (value > 0) { return true; }
    return false;
  }
}
void closure() {
  final check = (bool value) {
    if (value) return true;
    return false;
  };
  print(check(true));
}
bool nested(bool value, bool other) {
  if (other) {
    if (value) return false;
    return true;
  }
  return false;
}
bool nestedBlocks(bool value) {
  if (value) {{ return true; }}
  return false;
}
Return the condition or its negation instead of boolean literals.
2 if (value) return true;
3 return false;
Return the condition or its negation instead of boolean literals.
6 if (value) { return false; }
7 return true;
Return the condition or its negation instead of boolean literals.
10 if ((value)) { return (true); }
11 return (false);
Return the condition or its negation instead of boolean literals.
15 if (value > 0) { return true; }
16 return false;
Return the condition or its negation instead of boolean literals.
21 if (value) return true;
22 return false;
Return the condition or its negation instead of boolean literals.
28 if (value) return false;
29 return true;
Return the condition or its negation instead of boolean literals.
34 if (value) {{ return true; }}
35 return false;

Valid

example.dartdart
bool direct(bool value) { return value; }
bool inverted(bool value) { return !value; }
bool extraWork(bool value) {
  if (value) { print('work'); return true; }
  return false;
}
bool same(bool value) {
  if (value) return true;
  return true;
}
bool intervening(bool value) {
  if (value) return true;
  print('other');
  return false;
}
bool withElse(bool value) {
  if (value) return true;
  else return false;
}
bool pattern(Object value) {
  if (value case int number when number > 0) return true;
  return false;
}
int nonBoolean(bool value) {
  if (value) return 1;
  return 0;
}
bool arbitrary(bool value, bool result) {
  if (value) return result;
  return false;
}
bool separateBlock(bool value) {
  if (value) return true;
  { return false; }
}

Configure prefer-returning-condition

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

falcon.jsonjson
{
  "linter": {
    "rules": {
      "style": {
        "prefer-returning-condition": "error"
      }
    }
  }
}

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

example.dartdart
// falcon-ignore lint/style/prefer-returning-condition: <reason>
  • prefer-riverpod-notifier-suffixriverpod

    Requires a Notifier suffix on classes extending resolved Riverpod notifiers.

  • prefer-riverpod-provider-suffixriverpod

    Requires a Provider suffix on resolved Riverpod top-level providers and static fields.

  • prefer-single-quotesprefer_single_quotes

    Flags double-quoted string literals that could use single quotes without needing an escape.

  • prefer-sized-box-squareflutter

    Prefers SizedBox.square for an unnamed Flutter SizedBox whose explicit width and height are stable variable reads or numeric literals with identical token spelling.

  • prefer-spread-collectionsprefer_spread_collectionsrecommended

    Flags an addAll cascade on a collection literal that a spread would express.

  • prefer-switch-expression

    Prefer switch expressions for exhaustive switches that only return or assign a value.

  • prefer-test-matcherstest

    Recommends Matcher expectations and direct type, equality, or ordering matchers.

  • prefer-trailing-commaprefer-trailing-comma

    Flags multi-line argument lists that are missing a trailing comma.