CodeQL documentation

Identical operands

ID: go/redundant-operation
Kind: problem
Severity: warning
Precision: very-high
   - correctness
   - external/cwe/cwe-480
   - external/cwe/cwe-561
Query suites:
   - go-security-and-quality.qls

Click to see the query in the CodeQL repository

Many arithmetic or logical operators yield a trivial result when applied to identical operands: for instance, x-x is zero if x is a number, and NaN otherwise; x&x is always equal to x. Code like this is often the result of a typo, such as misspelling a variable name.


Carefully inspect the expression to ensure it is not a symptom of a bug.


In the example below, the function avg is intended to compute the average of two numbers x and y. However, the programmer accidentally used x twice, so the function just returns x:

package main

func avg(x, y float64) float64 {
	return (x + x) / 2

This problem can be fixed by correcting the typo:

package main

func avgGood(x, y float64) float64 {
	return (x + y) / 2