loopcheck

command module
v0.0.0-...-344ed63 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 10, 2018 License: BSD-3-Clause Imports: 3 Imported by: 0

README

Loop Check: Check go loops for escaping variables

Analyzes Go source code to find places where a pointer to a loop variable is created. This is a common bug. See my blog post for details.

Usage

  1. go install github.com/evanj/loopcheck
  2. $GOPATH/bin/loopcheck (source code or package)

Example

Code (go playground):

package main

import "fmt"

func main() {
  values := []MyStruct{MyStruct{1}, MyStruct{2}, MyStruct{3}}
  output := []*MyStruct{}
  for _, v := range values {
    output = append(output, &v)
  }
  fmt.Println("output:", output)
}

type MyStruct struct {
  number int
}

func (m *MyStruct) String() string {
  return fmt.Sprintf("MyStruct{%d}", m.number)
}

Output:

example1.go:9: takes address of loop variable: &v
  range at line 8: for _, v := range values

See Also

Inspired by errcheck: https://github.com/kisielk/errcheck

Also inspired by the range check code in go vet: https://github.com/golang/tools/blob/master/cmd/vet/rangeloop.go

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL