Skip to content

proposal: Go 2: ban calling append with a single argument #30040

@kaedys

Description

@kaedys

So, this is the second time I've ran into this issue in the last couple months. Ya, it's a silly mistake, but it definitely falls into the "probably incorrect" category that vet is designed to check for:

var list []myType
for v := range someChannel {
    list = append(list)
}

The append call is missing the actual thing to append. While this is a perfectly syntactically-valid function call, it also is completely useless, and I can't think of a single valid situation where append with no variadic arguments is valid (note that this is distinct from appending a defined but empty slice, which has valid use cases). In every situation I've ever seen this, it was a programmer mistake where they simply forgot to include what they actually wanted to append, and since a variadic argument can include 0 arguments, it doesn't generate a compile failure and is fairly annoying to debug, since the slice in question just seems to be empty for no readily apparent reason.

I think this definitely meets the correctness and precision requirements for vet features, and at least in my experience is at least as common a mistake as something like unreachable code, passing locks by value, or redundant boolean expressions. A previous similar issue (#15117) noted that this is mechanically a no-op, but this is a no-op that is essentially always a programming error, not simply a no-op expression, which I think is what lets it fulfill the correctness requirement.

Metadata

Metadata

Assignees

No one assigned

    Labels

    AnalysisIssues related to static analysis (vet, x/tools/go/analysis)FrozenDueToAgeNeedsDecisionFeedback is required from experts, contributors, and/or the community before a change can be made.Proposalv2An incompatible library change

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions