newline-before-return

Same as newline-before-return in Dart Code Metrics

Group
Style
Path
lint/style/newline-before-return
Default
Off, opt in
Origin
Dart Code Metrics
Rule
278 of 512

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. Switch-case statement lists are not blocks. A blank line contains two or more newlines and may appear above attached comments; 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;
}

final callback = () {
  print('callback');
  return 1;
};

int switchReturns(int value) {
  switch (value) {
    case 0:
      print(value);
      return value;
    default:
      return 1;
  }
}

void labeledReturn() {
  label: {
    print('label');
    return;
  }
}

class Operand {
  Operand operator +(Operand other) {
    print(other);
    return other;
  }
}

int commentedReturn() {
  final value = 1;
  // No separating blank line.
  return value;
}
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;
Add a blank line before return statement.
37 print('callback');
38 return 1;
Add a blank line before return statement.
53 print('label');
54 return;
Add a blank line before return statement.
60 print(other);
61 return other;
Add a blank line before return statement.
67 // No separating blank line.
68 return value;

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;
}

final callbacks = [
  () {
    return 1;
  },
];

int firstInEachList(int value) {
  switch (value) {
    case 0:
      return value;
    default:
      return 1;
  }
}

void firstInNestedLists() {
  try {
    return;
  } catch (error) {
    return;
  } finally {
    return;
  }
}

void singleStatementBranches(bool flag) {
  if (flag) return;
  while (flag) return;
  label: {
    return;
  }
}

int commentedReturn() {
  final value = 1;

  // Explain the return.
  return value;
}

Configure newline-before-return

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

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

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

example.dartdart
// falcon-ignore lint/style/newline-before-return: <reason>