← 返回首页
Identical operands — CodeQL query help documentation CodeQL docs
CodeQL documentation
CodeQL resources

Identical operands

ID: go/redundant-operation Kind: problem Security severity: Severity: warning Precision: very-high Tags: - quality - reliability - correctness - external/cwe/cwe-480 - external/cwe/cwe-561 Query suites: - go-code-quality.qls - 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.

Recommendation

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

Example

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 }