unnecessary_statements 진단: 불필요한 문장
unnecessary_statements 진단: 불필요한 문장
Dart 분석기가 내보내는 진단에는 이름이 있어요. unnecessary_statements는 표현식 문장(expression statement)이 명확한 효과를 내지 않을 때 알려주는 린트(lint) 진단이에요.
본문
설명
분석기는 표현식 문장이 명확한 효과가 없을 때 이 진단을 만들어요.
예시
다음 코드는 두 호출에서 반환된 값을 더하는 것이 명확한 효과가 없기 때문에 이 진단을 만들어요.
void f(int Function() first, int Function() second) {
first() + second();
}
흔한 해결 방법
표현식을 계산할 필요가 없다면 제거해요.
void f(int Function() first, int Function() second) {}
표현식의 값이 필요하다면 그 값을 활용해요. 우선 지역 변수에 할당해도 좋아요.
void f(int Function() first, int Function() second) {
print(first() + second());
}
표현식의 일부만 실행해야 한다면 불필요한 부분을 제거해요.
void f(int Function() first, int Function() second) {
first();
second();
}