Address Of Dereference

ID

go.address_of_deref

Severity

low

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Code Smell

Language

Go

Tags

CWE:1164, code-style, readability

Description

Reports the redundant pointer round-trip &*x (take the address of a dereference) and *&x (dereference the address of a value). Both cancel out and simplify to x.

Rationale

&*x is exactly x and *&x is exactly x — the address-of and dereference operators undo each other. The extra operators add noise and sometimes hide a misunderstanding about whether a value is being copied (it is not). The check fires only when one unary operator is applied directly to its complement.

func fn(x *int, y int) {
    p := &*x // FLAW — &*x is just x
    v := *&y // FLAW — *&y is just y
    _, _ = p, v
}

Remediation

Remove the cancelling operator pair and use the value directly.