Rules / Style / avoid-return-types-on-setters
avoid-return-types-on-setters
Flags setters declared with an explicit return type.
A setter never returns a value, so annotating one with a return type — even
void — is redundant and can mislead readers into thinking the result is
usable. Drop the return type and write set foo(int v). Both top-level
setters and setters declared in classes, mixins, enums, and extensions are
checked.
Invalid
example.dartdart
// Setters never return a value, so any return type is redundant.
class A {
int _v = 0;
void set value(int v) { _v = v; }
int _w = 0;
int set width(int v) { _w = v; }
}
class B {
String _n = '';
String set name(String v) { _n = v; }
static void set flag(bool v) {}
external void set ext(int v);
}
mixin M {
void set data(int v) {}
}
Avoid return types on setters.
3 int _v = 0;
4 void set value(int v) { _v = v; }
∙
Avoid return types on setters.
5 int _w = 0;
6 int set width(int v) { _w = v; }
∙
Avoid return types on setters.
10 String _n = '';
11 String set name(String v) { _n = v; }
∙
Avoid return types on setters.
11 String set name(String v) { _n = v; }
12 static void set flag(bool v) {}
∙
Avoid return types on setters.
12 static void set flag(bool v) {}
13 external void set ext(int v);
∙
Avoid return types on setters.
16mixin M {
17 void set data(int v) {}
∙
Valid
example.dartdart
// Setters declared without a return type — the correct form.
class A {
int _v = 0;
set value(int v) { _v = v; }
int _w = 0;
set width(int v) => _w = v;
static set flag(bool v) {}
external set ext(int v);
}
mixin M {
set data(int v) {}
}
class B {
int getValue() => 1;
void doThing() {}
}
How to configure
Set the severity of avoid-return-types-on-setters in your falcon.json:
falcon.jsonjson
{
"linter": {
"rules": {
"style": {
"avoid-return-types-on-setters": "error"
}
}
}
}