noop_primitive_operations 린트: 무의미한(no-op) 기본 타입 연산 제거
noop_primitive_operations 린트: 무의미한(no-op) 기본 타입 연산 제거
기본 타입(primitive type)에 대해 아무 효과가 없는 무의미한 연산(no-op)을 잡아주는 린트(lint) 규칙이에요. 이런 연산은 멱등(idempotent)해서 제거해도 결과가 같기 때문에, 코드를 깔끔하게 정리해 주는 규칙이랍니다.
본문
설명
기본 타입에 대한 일부 연산은 멱등적이라 제거할 수 있어요.
BAD:
doubleValue.toDouble();
intValue.toInt();
intValue.round();
intValue.ceil();
intValue.floor();
intValue.truncate();
string.toString();
string = 'hello\n'
''
'world';
'string with ${x.toString()}';
double 값에 toDouble()을, int 값에 toInt(), round(), ceil(), floor(), truncate()를 호출하는 건 결과가 같기 때문에 불필요해요. 또 String에 toString()을 호출하거나, 문자열 보간에서 x.toString()을 쓰는 것도 제거할 수 있어요.
다만 문자열 리터럴의 맨 앞이나 맨 뒤에 있는 빈 문자열 리터럴은 허용돼요. 보통 여러 줄에 걸쳐 문자열 리터럴을 정렬하는 용도로 쓰이기 때문이에요.
// OK
string = ''
'hello\n'
'world\n';
// OK
string = 'hello\n'
'world\n'
'';
이렇게 앞뒤의 빈 문자열은 줄 정렬을 위해 흔히 쓰이므로 예외로 허용된답니다.
활성화 방법
noop_primitive_operations 규칙을 활성화하려면 analysis_options.yaml 파일의 linter > rules 아래에 noop_primitive_operations를 추가해요.
analysis_options.yaml:
linter:
rules:
- noop_primitive_operations
대신 YAML 맵 문법으로 린트 규칙을 설정한다면, linter > rules 아래에 noop_primitive_operations: true라고 적어요.
analysis_options.yaml:
linter:
rules:
noop_primitive_operations: true