이 페이지는 아직 한국어로 제공되지 않습니다. 번역 작업 중입니다. 현재 번역 프로젝트에 대한 질문이나 피드백이 있으신 경우 언제든지 연락주시기 바랍니다.
Metadata
ID:go-best-practices/useless-bitwise-operation
Language: Go
Severity: Info
Category: Best Practices
Description
The expressions x|0, x^0, and x&0 are bitwise operations that involve the number 0. In these specific cases, the operation with 0 doesn’t have any effect on the value x.
x|0: The bitwise OR (|) operation between x and 0 will result in x itself. This is because any bit OR’ed with 0 will retain its original value. Therefore, x|0 can be simplified and replaced with just x.
x^0: The bitwise XOR (^) operation between x and 0 is similar to the OR operation. XOR’ing any bit with 0 will preserve its original value. As a result, x^0 is equivalent to x. Thus, the expression x^0 can be simplified and replaced by just x.
x&0: The bitwise AND (&) operation between x and 0 will always yield 0. This is because any bit AND’ed with 0 will always result in 0. Therefore, x&0 can be simplified and replaced by just 0.
Simplifying these expressions by replacing them with their respective values (x or 0) can enhance code readability and reduce unnecessary operations. However, it’s important to note that these simplifications only hold true when being operated on with the constant 0. If the constant value changes or the expressions involve variables, the behavior and result may vary.
By avoiding unnecessary operations and writing code that clearly exprresses your intent, you can make your code more maintainable and readable.
Non-Compliant Code Examples
funcmain(){println(x|0)println(x&0)println(x^0)}
Compliant Code Examples
funcmain(){println(x)println(0)println(x)}
원활한 통합. Datadog Code Security 사용해 보기
Datadog Code Security
이 규칙을 사용해 Datadog Code Security로 코드 분석해 보기
이 규칙의 사용법
1
2
rulesets:- go-best-practices # Rules to enforce Go best practices.
리포지토리 루트에서 위의 콘텐츠를 사용해 static-analysis.datadog.yml 생성
Datadog의 무료 IDE Plugins를 사용하거나 Code Security 스캔을 CI 파이프라인에 추가