Rules / Style / newline-before-return

newline-before-return

lint/style/newline-before-return
recommended
Same as newline-before-return · Dart Code Metrics

Flags return statements that are not preceded by a blank line.

Separating a return from the statements above it with a blank line makes the exit point of a block visually distinct, so readers can spot where control leaves the function without parsing the surrounding code. The rule only checks returns that follow another statement in the same block — the first statement of a block is never flagged, since there is nothing to separate it from. A blank line is any run of whitespace containing two or more newlines, so intervening spaces and tabs do not matter.

Invalid

example.dartdart
// Test cases for newline-before-return rule
// violations: return statement not preceded by a blank line

int badFunction(int x) {
  final result = x * 2;
  return result;
}

String processName(String name) {
  final trimmed = name.trim();
  final upper = trimmed.toUpperCase();
  return upper;
}

class Calculator {
  int add(int a, int b) {
    final sum = a + b;
    return sum;
  }

  int multiply(int a, int b) {
    final product = a * b;
    return product;
  }
}

void nestedReturns() {
  if (true) {
    final value = 42;
    return value;
  }
  print('done');
  return;
}
Add a blank line before return statement
5 final result = x * 2;
6 return result;
Add a blank line before return statement
11 final upper = trimmed.toUpperCase();
12 return upper;
Add a blank line before return statement
17 final sum = a + b;
18 return sum;
Add a blank line before return statement
22 final product = a * b;
23 return product;
Add a blank line before return statement
29 final value = 42;
30 return value;
Add a blank line before return statement
32 print('done');
33 return;

Valid

example.dartdart
// Test cases for newline-before-return rule
// No violations: return preceded by blank line or is the only statement

int goodFunction(int x) {
  final result = x * 2;

  return result;
}

String processName(String name) {
  final trimmed = name.trim();
  final upper = trimmed.toUpperCase();

  return upper;
}

// Single-statement body: no preceding statement so no blank line needed
int identity(int x) {
  return x;
}

class Calculator {
  int add(int a, int b) {
    final sum = a + b;

    return sum;
  }

  int multiply(int a, int b) {
    final product = a * b;

    return product;
  }
}

// With blank line in nested blocks
void nestedCorrect() {
  if (true) {
    final value = 42;

    return value;
  }

  print('done');

  return;
}

// Arrow function: no block, no check needed
int arrow(int x) => x * 2;

// Conditional with proper spacing
bool check(int x) {
  if (x > 0) {

    return true;
  }

  return false;
}

How to configure

Set the severity of newline-before-return in your falcon.json:

falcon.jsonjson
{
  "linter": {
    "rules": {
      "style": {
        "newline-before-return": "error"
      }
    }
  }
}