No-Op Bitwise Operation
ID |
go.no_op_bitwise |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Code Smell |
Language |
Go |
Tags |
CWE:1164, code-style, readability |
Description
Reports a bitwise operation whose literal operand is the identity element for that operator, so the
operation has no effect: x | 0 and x ^ 0 both equal x, and x & -1 equals x.
Rationale
A bitwise OR or XOR with 0, or a bitwise AND with -1 (all bits set), leaves the other operand
unchanged. The expression is redundant noise and often hides a typo — a wrong operator, a constant
that should not have been zero, or a sign mistake. The check inspects both operands and fires only
for these exact identity literals.
func fn(x int) {
_ = x | 0 // FLAW — equals x
_ = x ^ 0 // FLAW — equals x
_ = x & -1 // FLAW — equals x
_ = x | 1 // OK — sets a bit
}