Skip to content

False hint for preallocation #991

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
3 tasks done
Feresey opened this issue Mar 15, 2020 · 3 comments · Fixed by #1694
Closed
3 tasks done

False hint for preallocation #991

Feresey opened this issue Mar 15, 2020 · 3 comments · Fixed by #1694
Labels
dependencies Relates to an upstream dependency false positive An error is reported when one does not exist

Comments

@Feresey
Copy link

Feresey commented Mar 15, 2020

Thank you for creating the issue!

  • Yes, I'm using a binary release within 2 latest major releases. Only such installations are supported.
  • Yes, I've searched similar issues on GitHub and didn't find any.
  • Yes, I've included all information below (version, config, etc).

Please include the following information:

Version of golangci-lint
$ golangci-lint --version
golangci-lint has version v1.23.6 built from (unknown, mod sum: "h1:dxnT1QFIpTeVoFUPaVDeFJJ+To++8ANYsQ2JIxJY02s=") on (unknown)
no configs
Config file
$ cat .golangci.yml
no file
Go environment
$ go version && go env
go version go1.14 linux/amd64
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/lol/.cache/go-build"
GOENV="/home/lol/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/lol/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/lib/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build142025697=/tmp/go-build -gno-record-gcc-switches"
Verbose output of running
$ golangci-lint run -v --enable-all
INFO [config_reader] Config search paths: [./ /dev/shm /dev /]
INFO [lintersdb] Active 41 linters: [bodyclose deadcode depguard dogsled dupl errcheck funlen gochecknoglobals gochecknoinits gocognit goconst gocritic gocyclo godox gofmt goimports golint gomnd goprintffuncname gosec gosimple govet ineffassign interfacer lll maligned misspell nakedret prealloc rowserrcheck scopelint staticcheck structcheck stylecheck typecheck unconvert unparam unused varcheck whitespace wsl]
INFO [lintersdb] Active 41 linters: [bodyclose deadcode depguard dogsled dupl errcheck funlen gochecknoglobals gochecknoinits gocognit goconst gocritic gocyclo godox gofmt goimports golint gomnd goprintffuncname gosec gosimple govet ineffassign interfacer lll maligned misspell nakedret prealloc rowserrcheck scopelint staticcheck structcheck stylecheck typecheck unconvert unparam unused varcheck whitespace wsl]
INFO [loader] Go packages loading at mode 575 (deps|exports_file|files|imports|compiled_files|types_sizes|name) took 79.000942ms
INFO [runner/filename_unadjuster] Pre-built 0 adjustments in 227.914µs
INFO [runner/goanalysis_metalinter/goanalysis] analyzers took 0s with no stages
INFO [runner/unused/goanalysis] analyzers took 0s with no stages
INFO [runner] Processors filtering stat (out/in): diff: 1/1, max_same_issues: 1/1, identifier_marker: 1/1, filename_unadjuster: 1/1, path_prettifier: 1/1, skip_dirs: 1/1, max_from_linter: 1/1, path_shortener: 1/1, cgo: 1/1, skip_files: 1/1, uniq_by_line: 1/1, nolint: 1/1, max_per_file_from_linter: 1/1, source_code: 1/1, autogenerated_exclude: 1/1, exclude: 1/1, exclude-rules: 1/1
INFO [runner] processing took 223.729µs with stages: nolint: 62.805µs, exclude: 60.52µs, identifier_marker: 35.976µs, autogenerated_exclude: 17.415µs, source_code: 15.787µs, path_prettifier: 14.253µs, skip_dirs: 6.434µs, uniq_by_line: 2.908µs, max_same_issues: 1.875µs, cgo: 1.313µs, max_from_linter: 1.038µs, path_shortener: 995ns, max_per_file_from_linter: 765ns, filename_unadjuster: 728ns, diff: 405ns, exclude-rules: 303ns, skip_files: 209ns
INFO [runner] linters took 40.375115ms with stages: goanalysis_metalinter: 39.761763ms, unused: 322.148µs
main.go:15:2: Consider preallocating `a` (prealloc)
	var (
	^
INFO File cache stats: 1 entries of total size 288B
INFO Memory: 3 samples, avg is 68.9MB, max is 68.9MB
INFO Execution took 128.038057ms

source code

package main

import (
	"fmt"
	"math/rand"
)

const maxNew = 100

func other() []int {
	return make([]int, rand.Int63()%maxNew)
}

func main() {
	var (
		a []int
		b = []int{1, 2, 3, 4, 5}
	)

	for range b {
		a = append(a, other()...)
	}

	fmt.Printf("a: %d\nb: %d\n", len(a), len(b))
}
@ernado ernado added dependencies Relates to an upstream dependency false positive An error is reported when one does not exist labels Apr 21, 2020
@ernado
Copy link
Member

ernado commented Apr 21, 2020

This is related to prealloc linter.

@alexkohler
Copy link

This should be fixed in master as of alexkohler/prealloc#18.

@ernado
Copy link
Member

ernado commented Apr 21, 2020

Got it.
This should be upstreamed to https://github.com/golangci/prealloc, but seems like I have no push rights.

The other way is to expose API in https://github.com/alexkohler/prealloc itself, so we can use it directly. Seems like the general reason of fork is that package is a command, not a library.

@ernado ernado added the blocked Need's direct action from maintainer label Apr 21, 2020
rliebz added a commit to rliebz/golangci-lint that referenced this issue Feb 6, 2021
The version of prealloc at github.com/golangci/prealloc has not been
kept up to date with the latest changes. The upstream has recently been
updated to allow importing, which means there is no longer a need for a
fork.

Fixes golangci#991
rliebz added a commit to rliebz/golangci-lint that referenced this issue Feb 6, 2021
The forked version of prealloc at github.com/golangci/prealloc has not
been kept up to date with the latest changes from the upstream
repository, github.com/alexkohler/prealloc. This takes advantage of a
recent refactor in the upstream to allow importing the logic directly as
a package, which allows golangci to remove the overhead of maintaining a
fork while effectively bumping the version of prealloc used.

Fixes golangci#991
@ldez ldez removed the blocked Need's direct action from maintainer label Feb 6, 2021
@ldez ldez closed this as completed in #1694 Feb 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dependencies Relates to an upstream dependency false positive An error is reported when one does not exist
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants