Skip to content

simple: S1011 doesn't fire if the for i := range s form is used #881

@ainar-g

Description

@ainar-g

Synopsis:

package main

import "fmt"

func main() {
	var a = []int{1, 2, 3}
	var b = []int{4, 5, 6}
	var c = []int{7, 8, 9}

	for _, n := range b { /* Line 10. */
		a = append(a, n)
	}

	for i := range c { /* Line 14. */
		n := c[i]
		a = append(a, n)
	}

	fmt.Println(a)
}
$ staticcheck --debug.version
staticcheck (devel, v0.0.1-2020.1.0.20201124073330-56b7c78ddcd8)

Compiled with Go version: go1.15.4
Main module:
	honnef.co/go/tools@v0.0.1-2020.1.0.20201124073330-56b7c78ddcd8 (sum: h1:FkcSm40GciJwcT9dunF0X8Zv336LFcXaHt0k97IBG4Q=)
Dependencies:
	github.com/BurntSushi/toml@v0.3.1 (sum: h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=)
	golang.org/x/mod@v0.3.0 (sum: h1:RM4zey1++hCTbCVQfnWeKs9/IEsaBLA8vTkd0WVtmH4=)
	golang.org/x/tools@v0.0.0-20201201064407-fd09bd90d85c (sum: h1:D/mVYXCk6gUcyr7WuGlAk/ShHqgARUXc2VQxo27Hmws=)
	golang.org/x/xerrors@v0.0.0-20200804184101-5ec99f83aff1 (sum: h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=)
$ staticcheck --checks=all ./go/tmp/main.go
go/tmp/main.go:10:2: should replace loop with a = append(a, b...) (S1011)

Run 'staticcheck -explain ' or visit https://staticcheck.io/docs/checks for documentation on checks.

Line 14 should be marked as well.

(Found by @EugeneOne1.)

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions