unnecessary_async 진단: await가 없으면 async도 필요 없어요

unnecessary_async 진단: await가 없으면 async도 필요 없어요

unnecessary_async본문 안에서 await를 하지 않는데 async로 선언된 함수를 Dart 분석기가 알려주는 린트(lint) 규칙이에요. await가 없으면 async도, 보통은 Future 반환도 필요 없어요.

출처: unnecessary_async

본문

설명

await를 하지 않는 함수는 async일 필요가 없어요. 보통 그런 함수는 Future를 반환할 필요도 없어서, 호출하는 쪽에서 await를 피할 수 있게 돼요. 동기 코드는 일반적으로 더 빨리 실행되고, 이유를 따라가기도 더 쉬워요.

좋지 않은 코드 (BAD)

void f() async {
  // await Future.delayed(const Duration(seconds: 2));
  print(0);
}

좋은 코드 (GOOD)

void f() {
  // await Future.delayed(const Duration(seconds: 2));
  print(0);
}

규칙 활성화하기

analysis_options.yaml 파일의 linter > rules 아래에 unnecessary_async를 추가해서 활성화할 수 있어요.

linter:
  rules:
    - unnecessary_async

YAML 맵 문법으로 설정하고 있다면 linter > rules 아래에 unnecessary_async: true를 추가해요.

linter:
  rules:
    unnecessary_async: true

더 알아보기