avoid-returning-null-for-void
Same as avoid_returning_null_for_void in Dart lints
- Group
- Suspicious
- Path
lint/suspicious/avoid-returning-null-for-void- Default
- On (recommended)
- Origin
- Dart lints
- Rule
- 131 of 512
Flags return null; (and => null bodies) in synchronous void
functions and asynchronous Future<void> functions. A nullable future
permits a null result only in a synchronous body; void async is exempt.
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
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();
}void f() { return null; void g() => null;Future<void> h() async { return null; void method() { return null; set value(int v) { return null; void inner() { return null;Valid
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();
}
void parenthesizedReturn() {
return (null);
}
void parenthesizedArrow() => (null);Configure avoid-returning-null-for-void
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"suspicious": {
"avoid-returning-null-for-void": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/suspicious/avoid-returning-null-for-void: <reason>More suspicious rules
- avoid-shadowed-extension-methods
Flags extension instance methods and accessors hidden by their target's members.
- avoid-shadowing
Flags local bindings and parameters that hide an enclosing type's member.
- avoid-shadowing-type-parameters
avoid_shadowing_type_parametersrecommendedFlags a type parameter that shadows a type parameter of an enclosing declaration.
- avoid-suspicious-super-overrides
Finds getters hiding fields supplied via the immediate super constructor's
this.formals or declaring primary-constructor parameters. - avoid-throw-in-catch-block
avoid-throw-in-catch-blockFlags
throwstatements inside acatchblock. - avoid-top-level-members-in-teststest
Requires public declarations in test files, except main, to be private.
- avoid-types-as-parameter-names
avoid_types_as_parameter_namesrecommendedFlags implicit formal parameters, catch variables, and type parameters that shadow a type in their enclosing scope.
- avoid-unrelated-type-assertions
avoid-unrelated-type-assertionsrecommendedFlags an
ischeck proven impossible by canonical resolved types.