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
// 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;
} final result = x * 2; return result; final upper = trimmed.toUpperCase(); return upper; final sum = a + b; return sum; final product = a * b; return product; final value = 42; return value; print('done'); return; print('callback'); return 1; print('label'); return; print(other); return other; // No separating blank line. return value;Valid
// 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.
{
"linter": {
"rules": {
"style": {
"newline-before-return": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/style/newline-before-return: <reason>More style rules
- newline-before-throw
Requires a blank line before a throw that follows another statement.
- no-default-cases
no_default_casesFlags
default:in switch statements over enums and enum-like classes. - no-leading-underscores-for-library-prefixes
no_leading_underscores_for_library_prefixesrecommendedFlags import prefixes that begin with an underscore.
- no-leading-underscores-for-local-identifiers
no_leading_underscores_for_local_identifiersrecommendedFlags local variables and parameters whose names begin with an underscore.
- no-literal-bool-comparisons
no_literal_bool_comparisonsFlags boolean literals compared or combined with a non-nullable
bool. - no-magic-number
no-magic-numberFlags unnamed numeric literals (magic numbers) used inline in expressions.
- no-object-declaration
no-object-declarationFlags class members declared with the type
Object(orObject?). - no-runtime-type-to-string
no_runtimeType_toStringAvoid calling
toString()onruntimeType.