Merge Variable Declaration
ID |
go.merge_variable_declaration |
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 var declaration that declares a single variable with a type but no initializer, when the
very next statement assigns to that same variable.
Rationale
A declaration with no value followed immediately by an assignment to the same name splits one idea across two lines. Combining them into a single short variable declaration keeps the name and its first value together and lets the compiler infer the type.
func fn() {
var x int // FLAW — merge with the next line into x := 1
x = 1
_ = x
var y = 2 // OK — already initialized at the declaration
_ = y
}