prefer-expression-function-bodies
Same as prefer_expression_function_bodies in Dart lints
- Group
- Complexity
- Path
lint/complexity/prefer-expression-function-bodies- Default
- Off, opt in
- Origin
- Dart lints
- Rule
- 352 of 512
Flags block function bodies that consist of a single return of a value.
A body like { return expr; } reads better as => expr. Every function
body is checked: top-level functions, methods, getters, operators, local
functions, and closures. Diagnostic ranges include an async/generator modifier.
int get width { return _width; } // flagged
int get width => _width; // ok
Invalid
int twiceOf(int x) {
return x * 2;
}
class Box {
int _value = 0;
int get value {
return _value;
}
int twice() {
return _value * 2;
}
Box operator +(Box other) {
return Box();
}
Future<int> load() async {
return _value;
}
}
void main() {
int local(int x) {
return x + 1;
}
final f = (int x) {
return x;
};
print(local(f(1)));
}int twiceOf(int x) { int _value = 0; int get value { int twice() { Box operator +(Box other) { Future<int> load() async {void main() { int local(int x) { final f = (int x) {Valid
int twiceOf(int x) => x * 2;
int clamp(int x) {
if (x < 0) return 0;
return x;
}
class Box {
int _value = 0;
int get value => _value;
void reset() {
return;
}
void bump() {
_value++;
}
int compute() {
final y = _value * 2;
return y;
}
Iterable<int> values() sync* {
yield _value;
}
}
void main() {
final f = (int x) {
print(x);
};
f(1);
}Configure prefer-expression-function-bodies
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"complexity": {
"prefer-expression-function-bodies": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/complexity/prefer-expression-function-bodies: <reason>More complexity rules
- prefer-extracting-callbacks
prefer-extracting-callbacksflutterFlags inline block-body callbacks passed to Widget constructors.
- prefer-for-elements-to-map-from-iterable
prefer_for_elements_to_map_fromIterablerecommendedFlags
Map.fromIterable(x, key: ..., value: ...). - prefer-if-elements-to-conditional-expressions
prefer_if_elements_to_conditional_expressionsPrefers collection
ifelements to conditional expressions in list and set literals. - prefer-if-null-operators
prefer_if_null_operatorsrecommendedFlags
x == null ? y : x(and the invertedx != null ? x : y). - prefer-immediate-return
prefer-immediate-returnFlags a local variable that is returned on the immediately following statement.
- prefer-iterable-any
prefer_iterable_anyrecommendedFlags
iterable.where(...).isNotEmpty. - prefer-iterable-every
prefer_iterable_everyrecommendedFlags
iterable.where(predicate).isEmptyand filtered-length comparisons. - prefer-moving-to-variable
prefer-moving-to-variableFlags repeated property/getter chains in local variable initializers.