Simplify Math Pow
ID |
go.simplify_math_pow |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Code Smell |
Language |
Go |
Tags |
CWE:1164, code-style, readability |
Description
Reports math.Pow(x, k) with a small non-negative integer literal exponent k in the range 0..3.
Rationale
Such calls are equivalent to a constant or a short multiplication and avoid the runtime cost and
rounding of a general math.Pow. The base must be side-effect free; a base containing a call is left
alone, since expanding it would evaluate the base more than once. Larger or non-literal exponents are
not reported.
import "math"
func fn(x float64) {
_ = math.Pow(x, 2) // FLAW — equals x * x
_ = x * x // OK
_ = math.Pow(x, 6) // OK — too large to expand readably
}