golangci / golangci-lint

Fast linters runner for Go
https://golangci-lint.run
GNU General Public License v3.0
15.65k stars 1.39k forks source link

With the introduction of Go 1.22, it's possible to use "for" loops to range over integers. However, The "typecheck" linter warn me with "can't range over variable of type integer." #4459

Closed ashtishad closed 8 months ago

ashtishad commented 8 months ago

Welcome

Description of the problem

As of Go 1.22 "For" loops may now range over integers. Official Doc

package main

import "fmt"

func main() {
  for i := range 10 {
    fmt.Println(10 - i)
  }
  fmt.Println("go1.22 has lift-off!")
}

However, When I am running the linter it's giving me this error: "cannot range over n (variable of type int) (typecheck)"

// StartServers launches n number of HTTP servers and returns them for management.
func StartServers(startingPort int, n int) []*http.Server {
    var servers []*http.Server

        // ERROR HAPPENED HERE -> cannot range over n (variable of type int) (typecheck)
    for i := range n {
        server := &http.Server{
            Addr: net.JoinHostPort("", strconv.Itoa(startingPort+i)),
            Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
                _, _ = fmt.Fprintf(w, "Hello World from server on port %d!", startingPort+i)
            }),
            ReadTimeout:       5 * time.Second,
            WriteTimeout:      10 * time.Second,
            IdleTimeout:       15 * time.Second,
            ReadHeaderTimeout: 2 * time.Second,
        }

        servers = append(servers, server)

        go func(s *http.Server) {
            if err := s.ListenAndServe(); !errors.Is(err, http.ErrServerClosed) {
                log.Fatalf("failed to start server: %v", err)
            }
        }(server)

        log.Printf("Server-%d listening on port %s", i+1, server.Addr)
    }

    return servers
}
Screenshot 2024-03-05 at 4 40 41 PM

Version of golangci-lint

```console golangci-lint has version 1.55.2 built with go1.21.3 from e3c2265f on 2023-11-03T12:59:25Z ```

Configuration

```console run: concurrency: 4 timeout: 5m issues-exit-code: 2 tests: false output: print-issued-lines: false print-linter-name: true uniq-by-line: false path-prefix: "" sort-results: true linters: disable-all: true enable: - staticcheck - errcheck - gosimple - govet - ineffassign - revive - stylecheck - cyclop - prealloc - dupl - goimports - nilnil - tagalign - wrapcheck - gosec - asciicheck - bodyclose - nakedret - durationcheck - errname - exportloopref - forcetypeassert - goconst - gocritic - tagliatelle - lll - funlen linters-settings: cyclop: max-complexity: 14 package-average: 6.0 skip-tests: true nakedret: # Make an issue if func has more lines of code than this setting, and it has naked returns. max-func-lines: 30 wsl: allow-cuddle-declarations: true ```

Go environment

```console go version go1.22.0 darwin/arm64 GO111MODULE='on' GOARCH='arm64' GOBIN='/Users/ash/go/bin' GOCACHE='/Users/ash/Library/Caches/go-build' GOENV='/Users/ash/Library/Application Support/go/env' GOEXE='' GOEXPERIMENT='' GOFLAGS='' GOHOSTARCH='arm64' GOHOSTOS='darwin' GOINSECURE='' GOMODCACHE='/Users/ash/go/pkg/mod' GONOPROXY='' GONOSUMDB='' GOOS='darwin' GOPATH='/Users/ash/go' GOPRIVATE='' GOPROXY='https://proxy.golang.org,direct' GOROOT='/usr/local/go' GOSUMDB='sum.golang.org' GOTMPDIR='' GOTOOLCHAIN='auto' GOTOOLDIR='/usr/local/go/pkg/tool/darwin_arm64' GOVCS='' GOVERSION='go1.22.0' GCCGO='gccgo' AR='ar' CC='clang' CXX='clang++' CGO_ENABLED='1' GOMOD='/Users/ash/workspace/golift/go.mod' GOWORK='' CGO_CFLAGS='-O2 -g' CGO_CPPFLAGS='' CGO_CXXFLAGS='-O2 -g' CGO_FFLAGS='-O2 -g' CGO_LDFLAGS='-O2 -g' PKG_CONFIG='pkg-config' GOGCCFLAGS='-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/r3/jdcbk7294d9f5kjlf0zfc9ym0000gn/T/go-build3472031349=/tmp/go-build -gno-record-gcc-switches -fno-common' ```

Verbose output of running

```console INFO [config_reader] Config search paths: [./ /Users/ash/workspace/golift /Users/ash/workspace /Users/ash /Users /] INFO [config_reader] Used config file .golangci.yaml INFO [lintersdb] Active 27 linters: [asciicheck bodyclose cyclop dupl durationcheck errcheck errname exportloopref forcetypeassert funlen goconst gocritic goimports gosec gosimple govet ineffassign lll nakedret nilnil prealloc revive staticcheck stylecheck tagalign tagliatelle wrapcheck] INFO [loader] Go packages loading at mode 575 (exports_file|imports|deps|files|name|types_sizes|compiled_files) took 537.878208ms INFO [runner/filename_unadjuster] Pre-built 0 adjustments in 626.083µs INFO [linters_context/goanalysis] analyzers took 3.660576806s with top 10 stages: buildir: 2.015311832s, the_only_name: 920.549958ms, inspect: 138.671887ms, buildssa: 90.879875ms, printf: 68.017077ms, fact_deprecated: 64.637502ms, fact_purity: 64.407258ms, SA5012: 58.035294ms, ctrlflow: 52.807042ms, dupl: 48.163707ms INFO [runner] Issues before processing: 25, after processing: 0 INFO [runner] Processors filtering stat (out/in): filename_unadjuster: 25/25, exclude: 25/25, exclude-rules: 10/25, cgo: 25/25, nolint: 0/10, autogenerated_exclude: 25/25, skip_files: 25/25, skip_dirs: 25/25, identifier_marker: 25/25, path_prettifier: 25/25 INFO [runner] processing took 1.070333ms with stages: exclude-rules: 324.459µs, identifier_marker: 273.291µs, autogenerated_exclude: 178.125µs, nolint: 134.084µs, path_prettifier: 122.666µs, skip_dirs: 29.083µs, cgo: 4.75µs, filename_unadjuster: 834ns, sort_results: 499ns, fixer: 375ns, severity-rules: 334ns, uniq_by_line: 334ns, max_same_issues: 334ns, diff: 251ns, skip_files: 249ns, exclude: 209ns, max_per_file_from_linter: 167ns, path_shortener: 83ns, path_prefixer: 83ns, max_from_linter: 82ns, source_code: 41ns INFO [runner] linters took 2.089528875s with stages: goanalysis_metalinter: 2.088406542s INFO File cache stats: 0 entries of total size 0B INFO Memory: 28 samples, avg is 184.8MB, max is 298.3MB INFO Execution took 2.635618166s ```

A minimal reproducible example or link to a public repository

[Public Repository Link](https://github.com/ashtishad/golift) ```go // StartServers launches n number of HTTP servers and returns them for management. func StartServers(startingPort int, n int) []*http.Server { var servers []*http.Server // ERROR HAPPENED HERE -> cannot range over n (variable of type int) (typecheck) for i := range n { server := &http.Server{ Addr: net.JoinHostPort("", strconv.Itoa(startingPort+i)), Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { _, _ = fmt.Fprintf(w, "Hello World from server on port %d!", startingPort+i) }), ReadTimeout: 5 * time.Second, WriteTimeout: 10 * time.Second, IdleTimeout: 15 * time.Second, ReadHeaderTimeout: 2 * time.Second, } servers = append(servers, server) go func(s *http.Server) { if err := s.ListenAndServe(); !errors.Is(err, http.ErrServerClosed) { log.Fatalf("failed to start server: %v", err) } }(server) log.Printf("Server-%d listening on port %s", i+1, server.Addr) } return servers } ```

Validation

boring-cyborg[bot] commented 8 months ago

Hey, thank you for opening your first Issue ! 🙂 If you would like to contribute we have a guide for contributors.

ldez commented 8 months ago

Hello,

To be able to use golangci-lint with go 1.22 you should use a version built go1.22.

Our official binaries and Docker images are built with go1.22:

$ golangci-lint version        
golangci-lint has version 1.56.2 built with go1.22.0 from 58a724a0 on 2024-02-15T18:01:51Z

Also the first version that support go1.22 is golangci-lint v1.56.0.

From your description:

golangci-lint has version 1.55.2 built with go1.21.3 from e3c2265f on 2023-11-03T12:59:25Z

This means that you have built golangci-lint v1.55.2 built with go1.21.

You should use golangci-lint v1.56.0 built with go1.22.