Rules / Suspicious / avoid-returning-null-for-void
avoid-returning-null-for-void
Flags return null; (and => null bodies) in a function whose declared
return type is void or Future<void>.
Returning an explicit null from a void function is contradictory: the
signature promises no value, so the null is dead and usually reflects
confusion about the function's contract or a copy-paste from a nullable one.
Drop the value and write a bare return;, or change the return type if the
function is really meant to produce a value. Each nested function is judged by
its own return type, so an inner closure is not tainted by an enclosing void
frame.
Invalid
example.dartdart
void f() {
return null;
}
void g() => null;
Future<void> h() async {
return null;
}
class C {
void method() {
return null;
}
set value(int v) {
return null;
}
}
void outer() {
void inner() {
return null;
}
inner();
}
Don't return null from a function with a void return type.
1void f() {
2 return null;
∙
Don't return null from a function with a void return type.
Don't return null from a function with a void return type.
7Future<void> h() async {
8 return null;
∙
Don't return null from a function with a void return type.
12 void method() {
13 return null;
∙
Don't return null from a function with a void return type.
16 set value(int v) {
17 return null;
∙
Don't return null from a function with a void return type.
22 void inner() {
23 return null;
∙
Valid
example.dartdart
void f() {
return;
}
int g() {
return null;
}
int? h() => null;
Future<void> i() async {
await Future<void>.value();
}
class C {
int _v = 0;
void method() {
print('no return');
}
int compute() => null;
set value(int v) {
_v = v;
}
}
void outer() {
int inner() {
return null;
}
inner();
}
How to configure
Set the severity of avoid-returning-null-for-void in your falcon.json:
falcon.jsonjson
{
"linter": {
"rules": {
"suspicious": {
"avoid-returning-null-for-void": "error"
}
}
}
}