avoid-redundant-async
Same as avoid-redundant-async in Dart Code Metrics
- Group
- Complexity
- Path
lint/complexity/avoid-redundant-async- Default
- On (recommended)
- Origin
- Dart Code Metrics
- Rule
- 121 of 512
Flags an async function whose body is a single return await ...;.
Returning the Future directly can simplify these wrappers, but synchronous
throws then escape instead of completing the returned future with an error.
Only top-level functions and methods with a single return await statement
are checked, not local functions or anonymous closures.
Invalid
// Bad: async with only one await and no error handling
Future<String> getName() async {
return await db.getName();
}
Future<int> getValue() async {
return await compute();
}
class Service {
Future<User> getUser(String id) async {
return await repository.fetchUser(id);
}
Future<List<String>> getNames() async {
return await api.names();
}
}
Future<dynamic> fetch(String url) async {
return await http.get(url);
}
Future<String> transform(String input) async {
return await processString(input);
}// Bad: async with only one await and no error handlingFuture<String> getName() async { Future<int> getValue() async {class Service { Future<User> getUser(String id) async { Future<List<String>> getNames() async { Future<dynamic> fetch(String url) async { Future<String> transform(String input) async {Valid
// Good: removing unnecessary async/await
Future<String> getName() => db.getName();
Future<int> getValue() => compute();
class Service {
Future<User> getUser(String id) => repository.fetchUser(id);
Future<List<String>> getNames() => api.names();
}
Future<dynamic> fetch(String url) => http.get(url);
Future<String> transform(String input) => processString(input);
// OK: async with error handling
Future<String> safeGetName() async {
try {
return await db.getName();
} catch (e) {
return 'Default';
}
}
// OK: async with multiple awaits
Future<User> buildUser(String id) async {
final userData = await repository.fetchUser(id);
final preferences = await repository.fetchPreferences(id);
return User(userData, preferences);
}
// OK: async with additional logic
Future<String> processName() async {
final name = await db.getName();
print('Loaded: $name');
return name.toUpperCase();
}Configure avoid-redundant-async
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"complexity": {
"avoid-redundant-async": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/complexity/avoid-redundant-async: <reason>More complexity rules
- avoid-unnecessary-containers
avoid_unnecessary_containersrecommendedflutterFlags a
Containerwhose only argument is achild. - avoid-unnecessary-type-assertions
avoid-unnecessary-type-assertionsrecommendedFlags an
ischeck whose result is already guaranteed by the resolved static type. - avoid-unnecessary-type-casts
avoid-unnecessary-type-castsrecommendedFlags an
ascast to the operand's resolved static type. - cascade-invocations
cascade_invocationsFlags consecutive statements that invoke members on the same variable.
- cognitive-complexity
Reports function bodies with excessive nesting-weighted control flow.
- cyclomatic-complexity
cyclomatic_complexityFlags a function whose cyclomatic complexity exceeds the configured threshold.
- join-return-with-assignment
join_return_with_assignmentFlags an assignment immediately followed by
returnof the same variable. - max-lines-for-file
max_lines_for_fileFlags a file longer than the configured line limit.