Description
Proposal Details
The following code exists in many projects, and developers actually want [0 1 2], but due to the initialization error of slice, the final result is [0 0 0 0 1 2]
package main
import "fmt"
func main() {
sli := make([]int, 3)
for i := range 3 {
sli = append(sli, i)
}
fmt.Println(sli) // the result is [0 0 0 0 1 2]
}
The online demo: https://go.dev/play/p/q1BcVCmvidW
Over the past few months, I have conducted extensive research and analysis, and also submitted pull requests to fix issues in many well-known Go projects such as prometheus, zap, vitess. Below are some pull requests submitted by me and others related to this problem.
Due to limitations in search skills and time, I only checked records of such issues in the past few months. The history of more such issues has not been traced back. But I think it's already enough
I would like to propose adding a new analyzer to go vet that can detect such situations, thereby avoiding these issues in the future.
Now I have already completed an initial version of the code, and if the proposal is approved, I would be happy to refine it and add the necessary test cases.
The merged pr:
prometheus/prometheus#14702 (comment)
external-secrets/external-secrets#3964
superseriousbusiness/gotosocial#3382
apache/rocketmq-client-go#1171
Altinity/clickhouse-backup#1019
GoogleCloudPlatform/magic-modules#11919
https://github.com/hashicorp/nomad/pull/24109/files
VictoriaMetrics/VictoriaMetrics#6897
cosmos/cosmos-sdk#21494 (review)
https://github.com/ethereum-optimism/optimism/pull/11542/files
https://github.com/libp2p/go-libp2p/pull/2938/files
trufflesecurity/trufflehog#3293
VictoriaMetrics/VictoriaMetrics#7161
Altinity/clickhouse-operator#1523
ane more in review process.
Metadata
Metadata
Assignees
Type
Projects
Status
Hold