go/analysis/passes: add doc and copyright comments
...and other trivial cleanups. Multi-line doc comments have been moved to exported Doc constants for the sake of godoc. Change-Id: Ib1cbec5806c699d51283c34685c4cd96953f5384 Reviewed-on: https://go-review.googlesource.com/c/142360 Reviewed-by: Michael Matloob <matloob@golang.org>
This commit is contained in:
parent
63d31665e3
commit
a0ecdcbec4
|
@ -15,8 +15,23 @@ import (
|
||||||
"golang.org/x/tools/go/analysis/internal/checker"
|
"golang.org/x/tools/go/analysis/internal/checker"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// TODO(adonovan): support tri-state enable flags so -printf.enable=true means
|
||||||
|
// "run only printf" and -printf.enable=false means "run all but printf"
|
||||||
|
|
||||||
|
// TODO(adonovan): document (and verify) the exit codes:
|
||||||
|
// "Vet's exit code is 2 for erroneous invocation of the tool, 1 if a
|
||||||
|
// problem was reported, and 0 otherwise. Note that the tool does not
|
||||||
|
// check every possible problem and depends on unreliable heuristics
|
||||||
|
// so it should be used as guidance only, not as a firm indicator of
|
||||||
|
// program correctness."
|
||||||
|
|
||||||
const usage = `Analyze is a tool for static analysis of Go programs.
|
const usage = `Analyze is a tool for static analysis of Go programs.
|
||||||
|
|
||||||
|
Analyze examines Go source code and reports suspicious constructs, such as Printf
|
||||||
|
calls whose arguments do not align with the format string. It uses heuristics
|
||||||
|
that do not guarantee all reports are genuine problems, but it can find errors
|
||||||
|
not caught by the compilers.
|
||||||
|
|
||||||
Usage: analyze [-flag] [package]
|
Usage: analyze [-flag] [package]
|
||||||
`
|
`
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,8 @@
|
||||||
// Use of this source code is governed by a BSD-style
|
// Use of this source code is governed by a BSD-style
|
||||||
// license that can be found in the LICENSE file.
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
// Package asmdecl defines an Analyzer that reports mismatches between
|
||||||
|
// assembly files and Go declarations.
|
||||||
package asmdecl
|
package asmdecl
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
|
|
@ -1,3 +1,7 @@
|
||||||
|
// Copyright 2018 The Go Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
package asmdecl_test
|
package asmdecl_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -7,7 +11,7 @@ import (
|
||||||
"golang.org/x/tools/go/analysis/passes/asmdecl"
|
"golang.org/x/tools/go/analysis/passes/asmdecl"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestFromFileSystem(t *testing.T) {
|
func Test(t *testing.T) {
|
||||||
testdata := analysistest.TestData()
|
testdata := analysistest.TestData()
|
||||||
analysistest.Run(t, testdata, asmdecl.Analyzer, "a")
|
analysistest.Run(t, testdata, asmdecl.Analyzer, "a")
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
// Use of this source code is governed by a BSD-style
|
// Use of this source code is governed by a BSD-style
|
||||||
// license that can be found in the LICENSE file.
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
// Package assign defines an Analyzer that detects useless assignments.
|
||||||
package assign
|
package assign
|
||||||
|
|
||||||
// TODO(adonovan): check also for assignments to struct fields inside
|
// TODO(adonovan): check also for assignments to struct fields inside
|
||||||
|
@ -18,13 +19,15 @@ import (
|
||||||
"golang.org/x/tools/go/ast/inspector"
|
"golang.org/x/tools/go/ast/inspector"
|
||||||
)
|
)
|
||||||
|
|
||||||
var Analyzer = &analysis.Analyzer{
|
const Doc = `check for useless assignments
|
||||||
Name: "assign",
|
|
||||||
Doc: `check for useless assignments
|
|
||||||
|
|
||||||
This checker reports assignments of the form x = x or a[i] = a[i].
|
This checker reports assignments of the form x = x or a[i] = a[i].
|
||||||
These are almost always useless, and even when they aren't they are
|
These are almost always useless, and even when they aren't they are
|
||||||
usually a mistake.`,
|
usually a mistake.`
|
||||||
|
|
||||||
|
var Analyzer = &analysis.Analyzer{
|
||||||
|
Name: "assign",
|
||||||
|
Doc: Doc,
|
||||||
Requires: []*analysis.Analyzer{inspect.Analyzer},
|
Requires: []*analysis.Analyzer{inspect.Analyzer},
|
||||||
Run: run,
|
Run: run,
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +1,7 @@
|
||||||
|
// Copyright 2018 The Go Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
package assign_test
|
package assign_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
|
|
@ -2,6 +2,8 @@
|
||||||
// Use of this source code is governed by a BSD-style
|
// Use of this source code is governed by a BSD-style
|
||||||
// license that can be found in the LICENSE file.
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
// Package atomic defines an Analyzer that checks for common mistakes
|
||||||
|
// using the sync/atomic package.
|
||||||
package atomic
|
package atomic
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -15,15 +17,17 @@ import (
|
||||||
"golang.org/x/tools/go/ast/inspector"
|
"golang.org/x/tools/go/ast/inspector"
|
||||||
)
|
)
|
||||||
|
|
||||||
var Analyzer = &analysis.Analyzer{
|
const Doc = `check for common mistakes using the sync/atomic package
|
||||||
Name: "atomic",
|
|
||||||
Doc: `check for common mistakes using the sync/atomic package
|
|
||||||
|
|
||||||
The atomic checker looks for assignment statements of the form:
|
The atomic checker looks for assignment statements of the form:
|
||||||
|
|
||||||
x = atomic.AddUint64(&x, 1)
|
x = atomic.AddUint64(&x, 1)
|
||||||
|
|
||||||
which are not atomic.`,
|
which are not atomic.`
|
||||||
|
|
||||||
|
var Analyzer = &analysis.Analyzer{
|
||||||
|
Name: "atomic",
|
||||||
|
Doc: Doc,
|
||||||
Requires: []*analysis.Analyzer{inspect.Analyzer},
|
Requires: []*analysis.Analyzer{inspect.Analyzer},
|
||||||
RunDespiteErrors: true,
|
RunDespiteErrors: true,
|
||||||
Run: run,
|
Run: run,
|
||||||
|
|
|
@ -1,3 +1,7 @@
|
||||||
|
// Copyright 2018 The Go Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
package atomic_test
|
package atomic_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -7,7 +11,7 @@ import (
|
||||||
"golang.org/x/tools/go/analysis/passes/atomic"
|
"golang.org/x/tools/go/analysis/passes/atomic"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestFromFileSystem(t *testing.T) {
|
func Test(t *testing.T) {
|
||||||
testdata := analysistest.TestData()
|
testdata := analysistest.TestData()
|
||||||
analysistest.Run(t, testdata, atomic.Analyzer, "a")
|
analysistest.Run(t, testdata, atomic.Analyzer, "a")
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,8 @@
|
||||||
// Use of this source code is governed by a BSD-style
|
// Use of this source code is governed by a BSD-style
|
||||||
// license that can be found in the LICENSE file.
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
// Package bools defines an Analyzer that detects common mistakes
|
||||||
|
// involving boolean operators.
|
||||||
package bools
|
package bools
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
|
|
@ -1,3 +1,7 @@
|
||||||
|
// Copyright 2018 The Go Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
package bools_test
|
package bools_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -7,7 +11,7 @@ import (
|
||||||
"golang.org/x/tools/go/analysis/passes/bools"
|
"golang.org/x/tools/go/analysis/passes/bools"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestFromFileSystem(t *testing.T) {
|
func Test(t *testing.T) {
|
||||||
testdata := analysistest.TestData()
|
testdata := analysistest.TestData()
|
||||||
analysistest.Run(t, testdata, bools.Analyzer, "a")
|
analysistest.Run(t, testdata, bools.Analyzer, "a")
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
// Use of this source code is governed by a BSD-style
|
// Use of this source code is governed by a BSD-style
|
||||||
// license that can be found in the LICENSE file.
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
// Package buildtag defines an Analyzer that checks build tags.
|
||||||
package buildtag
|
package buildtag
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
|
|
@ -1,3 +1,7 @@
|
||||||
|
// Copyright 2018 The Go Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
package buildtag_test
|
package buildtag_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -9,6 +13,5 @@ import (
|
||||||
|
|
||||||
func Test(t *testing.T) {
|
func Test(t *testing.T) {
|
||||||
testdata := analysistest.TestData()
|
testdata := analysistest.TestData()
|
||||||
// loads testdata/src/a/a.go
|
|
||||||
analysistest.Run(t, testdata, buildtag.Analyzer, "a")
|
analysistest.Run(t, testdata, buildtag.Analyzer, "a")
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,8 @@
|
||||||
// Use of this source code is governed by a BSD-style
|
// Use of this source code is governed by a BSD-style
|
||||||
// license that can be found in the LICENSE file.
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
// Package cgocall defines an Analyzer that detects some violations of
|
||||||
|
// the cgo pointer passing rules.
|
||||||
package cgocall
|
package cgocall
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -18,16 +20,18 @@ import (
|
||||||
"golang.org/x/tools/go/ast/inspector"
|
"golang.org/x/tools/go/ast/inspector"
|
||||||
)
|
)
|
||||||
|
|
||||||
var Analyzer = &analysis.Analyzer{
|
const Doc = `detect some violations of the cgo pointer passing rules
|
||||||
Name: "cgocall",
|
|
||||||
Doc: `detect some violations of the cgo pointer passing rules
|
|
||||||
|
|
||||||
Check for invalid cgo pointer passing.
|
Check for invalid cgo pointer passing.
|
||||||
This looks for code that uses cgo to call C code passing values
|
This looks for code that uses cgo to call C code passing values
|
||||||
whose types are almost always invalid according to the cgo pointer
|
whose types are almost always invalid according to the cgo pointer
|
||||||
sharing rules.
|
sharing rules.
|
||||||
Specifically, it warns about attempts to pass a Go chan, map, func,
|
Specifically, it warns about attempts to pass a Go chan, map, func,
|
||||||
or slice to C, either directly, or via a pointer, array, or struct.`,
|
or slice to C, either directly, or via a pointer, array, or struct.`
|
||||||
|
|
||||||
|
var Analyzer = &analysis.Analyzer{
|
||||||
|
Name: "cgocall",
|
||||||
|
Doc: Doc,
|
||||||
Requires: []*analysis.Analyzer{inspect.Analyzer},
|
Requires: []*analysis.Analyzer{inspect.Analyzer},
|
||||||
RunDespiteErrors: true,
|
RunDespiteErrors: true,
|
||||||
Run: run,
|
Run: run,
|
||||||
|
|
|
@ -1,3 +1,7 @@
|
||||||
|
// Copyright 2018 The Go Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
package cgocall_test
|
package cgocall_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -7,7 +11,7 @@ import (
|
||||||
"golang.org/x/tools/go/analysis/passes/cgocall"
|
"golang.org/x/tools/go/analysis/passes/cgocall"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestFromFileSystem(t *testing.T) {
|
func Test(t *testing.T) {
|
||||||
testdata := analysistest.TestData()
|
testdata := analysistest.TestData()
|
||||||
analysistest.Run(t, testdata, cgocall.Analyzer, "a", "b")
|
analysistest.Run(t, testdata, cgocall.Analyzer, "a", "b")
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,8 @@
|
||||||
// Use of this source code is governed by a BSD-style
|
// Use of this source code is governed by a BSD-style
|
||||||
// license that can be found in the LICENSE file.
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
// Package composite defines an Analyzer that checks for unkeyed
|
||||||
|
// composite literals.
|
||||||
package composite
|
package composite
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -14,14 +16,16 @@ import (
|
||||||
"golang.org/x/tools/go/ast/inspector"
|
"golang.org/x/tools/go/ast/inspector"
|
||||||
)
|
)
|
||||||
|
|
||||||
var Analyzer = &analysis.Analyzer{
|
const Doc = `checked for unkeyed composite literals
|
||||||
Name: "composites",
|
|
||||||
Doc: `checked for unkeyed composite literals
|
|
||||||
|
|
||||||
This analyzer reports a diagnostic for composite literals of struct
|
This analyzer reports a diagnostic for composite literals of struct
|
||||||
types imported from another package that do not use the field-keyed
|
types imported from another package that do not use the field-keyed
|
||||||
syntax. Such literals are fragile because the addition of a new field
|
syntax. Such literals are fragile because the addition of a new field
|
||||||
(even if unexported) to the struct will cause compilation to fail.`,
|
(even if unexported) to the struct will cause compilation to fail.`
|
||||||
|
|
||||||
|
var Analyzer = &analysis.Analyzer{
|
||||||
|
Name: "composites",
|
||||||
|
Doc: Doc,
|
||||||
Requires: []*analysis.Analyzer{inspect.Analyzer},
|
Requires: []*analysis.Analyzer{inspect.Analyzer},
|
||||||
RunDespiteErrors: true,
|
RunDespiteErrors: true,
|
||||||
Run: run,
|
Run: run,
|
||||||
|
|
|
@ -1,3 +1,7 @@
|
||||||
|
// Copyright 2018 The Go Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
package composite_test
|
package composite_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -7,7 +11,7 @@ import (
|
||||||
"golang.org/x/tools/go/analysis/passes/composite"
|
"golang.org/x/tools/go/analysis/passes/composite"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestFromFileSystem(t *testing.T) {
|
func Test(t *testing.T) {
|
||||||
testdata := analysistest.TestData()
|
testdata := analysistest.TestData()
|
||||||
analysistest.Run(t, testdata, composite.Analyzer, "a")
|
analysistest.Run(t, testdata, composite.Analyzer, "a")
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,8 @@
|
||||||
// Use of this source code is governed by a BSD-style
|
// Use of this source code is governed by a BSD-style
|
||||||
// license that can be found in the LICENSE file.
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
// Package copylock defines an Analyzer that checks for locks
|
||||||
|
// erroneously passed by value.
|
||||||
package copylock
|
package copylock
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -17,9 +19,15 @@ import (
|
||||||
"golang.org/x/tools/go/ast/inspector"
|
"golang.org/x/tools/go/ast/inspector"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const Doc = `check for locks erroneously passed by value
|
||||||
|
|
||||||
|
Inadvertently copying a value containing a lock, such as sync.Mutex or
|
||||||
|
sync.WaitGroup, may cause both copies to malfunction. Generally such
|
||||||
|
values should be referred to through a pointer.`
|
||||||
|
|
||||||
var Analyzer = &analysis.Analyzer{
|
var Analyzer = &analysis.Analyzer{
|
||||||
Name: "copylocks",
|
Name: "copylocks",
|
||||||
Doc: "check for locks erroneously passed by value",
|
Doc: Doc,
|
||||||
Requires: []*analysis.Analyzer{inspect.Analyzer},
|
Requires: []*analysis.Analyzer{inspect.Analyzer},
|
||||||
RunDespiteErrors: true,
|
RunDespiteErrors: true,
|
||||||
Run: run,
|
Run: run,
|
||||||
|
|
|
@ -1,3 +1,7 @@
|
||||||
|
// Copyright 2018 The Go Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
package copylock_test
|
package copylock_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
|
|
@ -1,3 +1,7 @@
|
||||||
|
// Copyright 2018 The Go Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
package ctrlflow_test
|
package ctrlflow_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
|
|
@ -1,6 +1,11 @@
|
||||||
// The findcall package is a trivial example and test of an analyzer of
|
// Copyright 2018 The Go Authors. All rights reserved.
|
||||||
// Go source code. It reports a diagnostic for every call to a function or
|
// Use of this source code is governed by a BSD-style
|
||||||
// method of the name specified by its --name flag.
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
// The findcall package defines an Analyzer that serves as a trivial
|
||||||
|
// example and test of the Analysis API. It reports a diagnostic for
|
||||||
|
// every call to a function or method of the name specified by its
|
||||||
|
// -name flag.
|
||||||
package findcall
|
package findcall
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -10,13 +15,15 @@ import (
|
||||||
"golang.org/x/tools/go/analysis"
|
"golang.org/x/tools/go/analysis"
|
||||||
)
|
)
|
||||||
|
|
||||||
var Analyzer = &analysis.Analyzer{
|
const Doc = `find calls to a particular function
|
||||||
Name: "findcall",
|
|
||||||
Doc: `find calls to a particular function
|
|
||||||
|
|
||||||
The findcall analysis reports calls to functions or methods
|
The findcall analysis reports calls to functions or methods
|
||||||
of a particular name.`,
|
of a particular name.`
|
||||||
Run: findcall,
|
|
||||||
|
var Analyzer = &analysis.Analyzer{
|
||||||
|
Name: "findcall",
|
||||||
|
Doc: Doc,
|
||||||
|
Run: run,
|
||||||
RunDespiteErrors: true,
|
RunDespiteErrors: true,
|
||||||
FactTypes: []analysis.Fact{new(foundFact)},
|
FactTypes: []analysis.Fact{new(foundFact)},
|
||||||
}
|
}
|
||||||
|
@ -27,7 +34,7 @@ func init() {
|
||||||
Analyzer.Flags.StringVar(&name, "name", name, "name of the function to find")
|
Analyzer.Flags.StringVar(&name, "name", name, "name of the function to find")
|
||||||
}
|
}
|
||||||
|
|
||||||
func findcall(pass *analysis.Pass) (interface{}, error) {
|
func run(pass *analysis.Pass) (interface{}, error) {
|
||||||
for _, f := range pass.Files {
|
for _, f := range pass.Files {
|
||||||
ast.Inspect(f, func(n ast.Node) bool {
|
ast.Inspect(f, func(n ast.Node) bool {
|
||||||
if call, ok := n.(*ast.CallExpr); ok {
|
if call, ok := n.(*ast.CallExpr); ok {
|
||||||
|
|
|
@ -1,3 +1,7 @@
|
||||||
|
// Copyright 2018 The Go Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
package findcall_test
|
package findcall_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
// Use of this source code is governed by a BSD-style
|
// Use of this source code is governed by a BSD-style
|
||||||
// license that can be found in the LICENSE file.
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
// Package inspect is an analysis that provides an AST inspector
|
// Package inspect defines an Analyzer that provides an AST inspector
|
||||||
// (golang.org/x/tools/go/ast/inspect.Inspect) for the syntax trees of a
|
// (golang.org/x/tools/go/ast/inspect.Inspect) for the syntax trees of a
|
||||||
// package. It is only a building block for other analyzers.
|
// package. It is only a building block for other analyzers.
|
||||||
//
|
//
|
||||||
|
|
|
@ -2,6 +2,8 @@
|
||||||
// Use of this source code is governed by a BSD-style
|
// Use of this source code is governed by a BSD-style
|
||||||
// license that can be found in the LICENSE file.
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
// Package loopclosure defines an Analyzer that checks for references to
|
||||||
|
// enclosing loop variables from within nested functions.
|
||||||
package loopclosure
|
package loopclosure
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -22,9 +24,7 @@ import (
|
||||||
// }()
|
// }()
|
||||||
// }
|
// }
|
||||||
|
|
||||||
var Analyzer = &analysis.Analyzer{
|
const Doc = `check references to loop variables from within nested functions
|
||||||
Name: "loopclosure",
|
|
||||||
Doc: `check references to loop variables from within nested functions
|
|
||||||
|
|
||||||
This analyzer checks for references to loop variables from within a
|
This analyzer checks for references to loop variables from within a
|
||||||
function literal inside the loop body. It checks only instances where
|
function literal inside the loop body. It checks only instances where
|
||||||
|
@ -40,7 +40,11 @@ For example:
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
See: https://golang.org/doc/go_faq.html#closures_and_goroutines`,
|
See: https://golang.org/doc/go_faq.html#closures_and_goroutines`
|
||||||
|
|
||||||
|
var Analyzer = &analysis.Analyzer{
|
||||||
|
Name: "loopclosure",
|
||||||
|
Doc: Doc,
|
||||||
Requires: []*analysis.Analyzer{inspect.Analyzer},
|
Requires: []*analysis.Analyzer{inspect.Analyzer},
|
||||||
Run: run,
|
Run: run,
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +1,7 @@
|
||||||
|
// Copyright 2018 The Go Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
package loopclosure_test
|
package loopclosure_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
|
|
@ -2,6 +2,8 @@
|
||||||
// Use of this source code is governed by a BSD-style
|
// Use of this source code is governed by a BSD-style
|
||||||
// license that can be found in the LICENSE file.
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
// Package lostcancel defines an Analyzer that checks for failure to
|
||||||
|
// call a context cancelation function.
|
||||||
package lostcancel
|
package lostcancel
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -16,7 +18,7 @@ import (
|
||||||
"golang.org/x/tools/go/cfg"
|
"golang.org/x/tools/go/cfg"
|
||||||
)
|
)
|
||||||
|
|
||||||
const doc = `check cancel func returned by context.WithCancel is called
|
const Doc = `check cancel func returned by context.WithCancel is called
|
||||||
|
|
||||||
The cancelation function returned by context.WithCancel, WithTimeout,
|
The cancelation function returned by context.WithCancel, WithTimeout,
|
||||||
and WithDeadline must be called or the new context will remain live
|
and WithDeadline must be called or the new context will remain live
|
||||||
|
@ -25,7 +27,7 @@ until its parent context is cancelled.
|
||||||
|
|
||||||
var Analyzer = &analysis.Analyzer{
|
var Analyzer = &analysis.Analyzer{
|
||||||
Name: "lostcancel",
|
Name: "lostcancel",
|
||||||
Doc: doc,
|
Doc: Doc,
|
||||||
Run: run,
|
Run: run,
|
||||||
Requires: []*analysis.Analyzer{
|
Requires: []*analysis.Analyzer{
|
||||||
inspect.Analyzer,
|
inspect.Analyzer,
|
||||||
|
|
|
@ -1,3 +1,7 @@
|
||||||
|
// Copyright 2018 The Go Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
package lostcancel_test
|
package lostcancel_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -9,5 +13,5 @@ import (
|
||||||
|
|
||||||
func Test(t *testing.T) {
|
func Test(t *testing.T) {
|
||||||
testdata := analysistest.TestData()
|
testdata := analysistest.TestData()
|
||||||
analysistest.Run(t, testdata, lostcancel.Analyzer, "a") // load testdata/src/a/a.go
|
analysistest.Run(t, testdata, lostcancel.Analyzer, "a")
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,8 @@
|
||||||
// Use of this source code is governed by a BSD-style
|
// Use of this source code is governed by a BSD-style
|
||||||
// license that can be found in the LICENSE file.
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
// Package nilfunc defines an Analyzer that checks for useless
|
||||||
|
// comparisons against nil.
|
||||||
package nilfunc
|
package nilfunc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -14,11 +16,13 @@ import (
|
||||||
"golang.org/x/tools/go/ast/inspector"
|
"golang.org/x/tools/go/ast/inspector"
|
||||||
)
|
)
|
||||||
|
|
||||||
var Analyzer = &analysis.Analyzer{
|
const Doc = `check for useless comparisons between functions and nil
|
||||||
Name: "nilfunc",
|
|
||||||
Doc: `check for useless comparisons between functions and nil
|
|
||||||
|
|
||||||
A useless comparison is one like f == nil as opposed to f() == nil.`,
|
A useless comparison is one like f == nil as opposed to f() == nil.`
|
||||||
|
|
||||||
|
var Analyzer = &analysis.Analyzer{
|
||||||
|
Name: "nilfunc",
|
||||||
|
Doc: Doc,
|
||||||
Requires: []*analysis.Analyzer{inspect.Analyzer},
|
Requires: []*analysis.Analyzer{inspect.Analyzer},
|
||||||
Run: run,
|
Run: run,
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +1,7 @@
|
||||||
|
// Copyright 2018 The Go Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
package nilfunc_test
|
package nilfunc_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
|
|
@ -1,3 +1,7 @@
|
||||||
|
// Copyright 2018 The Go Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
// The pkgfact package is a demonstration and test of the package fact
|
// The pkgfact package is a demonstration and test of the package fact
|
||||||
// mechanism.
|
// mechanism.
|
||||||
//
|
//
|
||||||
|
|
|
@ -1,3 +1,7 @@
|
||||||
|
// Copyright 2018 The Go Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
package pkgfact_test
|
package pkgfact_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -9,5 +13,5 @@ import (
|
||||||
|
|
||||||
func Test(t *testing.T) {
|
func Test(t *testing.T) {
|
||||||
testdata := analysistest.TestData()
|
testdata := analysistest.TestData()
|
||||||
analysistest.Run(t, testdata, pkgfact.Analyzer, "c") // load testdata/src/c/c.go
|
analysistest.Run(t, testdata, pkgfact.Analyzer, "c")
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,8 @@
|
||||||
// Use of this source code is governed by a BSD-style
|
// Use of this source code is governed by a BSD-style
|
||||||
// license that can be found in the LICENSE file.
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
// Package shift defines an Analyzer that checks for shifts that exceed
|
||||||
|
// the width of an integer.
|
||||||
package shift
|
package shift
|
||||||
|
|
||||||
// TODO(adonovan): integrate with ctrflow (CFG-based) dead code analysis. May
|
// TODO(adonovan): integrate with ctrflow (CFG-based) dead code analysis. May
|
||||||
|
|
|
@ -1,3 +1,7 @@
|
||||||
|
// Copyright 2018 The Go Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
package shift_test
|
package shift_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -7,7 +11,7 @@ import (
|
||||||
"golang.org/x/tools/go/analysis/passes/shift"
|
"golang.org/x/tools/go/analysis/passes/shift"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestFromFileSystem(t *testing.T) {
|
func Test(t *testing.T) {
|
||||||
testdata := analysistest.TestData()
|
testdata := analysistest.TestData()
|
||||||
analysistest.Run(t, testdata, shift.Analyzer, "a")
|
analysistest.Run(t, testdata, shift.Analyzer, "a")
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,8 @@
|
||||||
// Use of this source code is governed by a BSD-style
|
// Use of this source code is governed by a BSD-style
|
||||||
// license that can be found in the LICENSE file.
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
// Package stdmethods defines an Analyzer that checks for misspellings
|
||||||
|
// in the signatures of methods similar to well-known interfaces.
|
||||||
package stdmethods
|
package stdmethods
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -18,9 +20,7 @@ import (
|
||||||
"golang.org/x/tools/go/ast/inspector"
|
"golang.org/x/tools/go/ast/inspector"
|
||||||
)
|
)
|
||||||
|
|
||||||
var Analyzer = &analysis.Analyzer{
|
const Doc = `check signature of methods of well-known interfaces
|
||||||
Name: "stdmethods",
|
|
||||||
Doc: `check signature of methods of well-known interfaces
|
|
||||||
|
|
||||||
Sometimes a type may be intended to satisfy an interface but may fail to
|
Sometimes a type may be intended to satisfy an interface but may fail to
|
||||||
do so because of a mistake in its method signature.
|
do so because of a mistake in its method signature.
|
||||||
|
@ -39,7 +39,11 @@ Checked method names include:
|
||||||
Peek ReadByte ReadFrom ReadRune Scan Seek
|
Peek ReadByte ReadFrom ReadRune Scan Seek
|
||||||
UnmarshalJSON UnreadByte UnreadRune WriteByte
|
UnmarshalJSON UnreadByte UnreadRune WriteByte
|
||||||
WriteTo
|
WriteTo
|
||||||
`,
|
`
|
||||||
|
|
||||||
|
var Analyzer = &analysis.Analyzer{
|
||||||
|
Name: "stdmethods",
|
||||||
|
Doc: Doc,
|
||||||
Requires: []*analysis.Analyzer{inspect.Analyzer},
|
Requires: []*analysis.Analyzer{inspect.Analyzer},
|
||||||
Run: run,
|
Run: run,
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +1,7 @@
|
||||||
|
// Copyright 2018 The Go Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
package stdmethods_test
|
package stdmethods_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
|
|
@ -2,6 +2,8 @@
|
||||||
// Use of this source code is governed by a BSD-style
|
// Use of this source code is governed by a BSD-style
|
||||||
// license that can be found in the LICENSE file.
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
// Package structtag defines an Analyzer that checks struct field tags
|
||||||
|
// are well formed.
|
||||||
package structtag
|
package structtag
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -19,11 +21,13 @@ import (
|
||||||
"golang.org/x/tools/go/ast/inspector"
|
"golang.org/x/tools/go/ast/inspector"
|
||||||
)
|
)
|
||||||
|
|
||||||
var Analyzer = &analysis.Analyzer{
|
const Doc = `check that struct field tags conform to reflect.StructTag.Get
|
||||||
Name: "structtag",
|
|
||||||
Doc: `check that struct field tags conform to reflect.StructTag.Get
|
|
||||||
|
|
||||||
Also report certain struct tags (json, xml) used with unexported fields.`,
|
Also report certain struct tags (json, xml) used with unexported fields.`
|
||||||
|
|
||||||
|
var Analyzer = &analysis.Analyzer{
|
||||||
|
Name: "structtag",
|
||||||
|
Doc: Doc,
|
||||||
Requires: []*analysis.Analyzer{inspect.Analyzer},
|
Requires: []*analysis.Analyzer{inspect.Analyzer},
|
||||||
RunDespiteErrors: true,
|
RunDespiteErrors: true,
|
||||||
Run: run,
|
Run: run,
|
||||||
|
|
|
@ -1,3 +1,7 @@
|
||||||
|
// Copyright 2018 The Go Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
package structtag_test
|
package structtag_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -7,7 +11,7 @@ import (
|
||||||
"golang.org/x/tools/go/analysis/passes/structtag"
|
"golang.org/x/tools/go/analysis/passes/structtag"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestFromFileSystem(t *testing.T) {
|
func Test(t *testing.T) {
|
||||||
testdata := analysistest.TestData()
|
testdata := analysistest.TestData()
|
||||||
analysistest.Run(t, testdata, structtag.Analyzer, "a")
|
analysistest.Run(t, testdata, structtag.Analyzer, "a")
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,8 @@
|
||||||
// Use of this source code is governed by a BSD-style
|
// Use of this source code is governed by a BSD-style
|
||||||
// license that can be found in the LICENSE file.
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
// Package tests defines an Analyzer that checks for common mistaken
|
||||||
|
// usages of tests and examples.
|
||||||
package tests
|
package tests
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -14,14 +16,16 @@ import (
|
||||||
"golang.org/x/tools/go/analysis"
|
"golang.org/x/tools/go/analysis"
|
||||||
)
|
)
|
||||||
|
|
||||||
var Analyzer = &analysis.Analyzer{
|
const Doc = `check for common mistaken usages of tests and examples
|
||||||
Name: "tests",
|
|
||||||
Doc: `check for common mistaken usages of tests and examples
|
|
||||||
|
|
||||||
The tests checker walks Test, Benchmark and Example functions checking
|
The tests checker walks Test, Benchmark and Example functions checking
|
||||||
malformed names, wrong signatures and examples documenting non-existent
|
malformed names, wrong signatures and examples documenting non-existent
|
||||||
identifiers.`,
|
identifiers.`
|
||||||
Run: run,
|
|
||||||
|
var Analyzer = &analysis.Analyzer{
|
||||||
|
Name: "tests",
|
||||||
|
Doc: Doc,
|
||||||
|
Run: run,
|
||||||
}
|
}
|
||||||
|
|
||||||
func run(pass *analysis.Pass) (interface{}, error) {
|
func run(pass *analysis.Pass) (interface{}, error) {
|
||||||
|
|
|
@ -1,3 +1,7 @@
|
||||||
|
// Copyright 2018 The Go Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
package tests_test
|
package tests_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
// Use of this source code is governed by a BSD-style
|
// Use of this source code is governed by a BSD-style
|
||||||
// license that can be found in the LICENSE file.
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
// Package unreachable defines an Analyzer that checks for unreachable code.
|
||||||
package unreachable
|
package unreachable
|
||||||
|
|
||||||
// TODO(adonovan): use the new cfg package, which is more precise.
|
// TODO(adonovan): use the new cfg package, which is more precise.
|
||||||
|
@ -16,13 +17,15 @@ import (
|
||||||
"golang.org/x/tools/go/ast/inspector"
|
"golang.org/x/tools/go/ast/inspector"
|
||||||
)
|
)
|
||||||
|
|
||||||
var Analyzer = &analysis.Analyzer{
|
const Doc = `check for unreachable code
|
||||||
Name: "unreachable",
|
|
||||||
Doc: `check for unreachable code
|
|
||||||
|
|
||||||
The unreachable analyzer finds statements that execution can never reach
|
The unreachable analyzer finds statements that execution can never reach
|
||||||
because they are preceded by an return statement, a call to panic, an
|
because they are preceded by an return statement, a call to panic, an
|
||||||
infinite loop, or similar constructs.`,
|
infinite loop, or similar constructs.`
|
||||||
|
|
||||||
|
var Analyzer = &analysis.Analyzer{
|
||||||
|
Name: "unreachable",
|
||||||
|
Doc: Doc,
|
||||||
Requires: []*analysis.Analyzer{inspect.Analyzer},
|
Requires: []*analysis.Analyzer{inspect.Analyzer},
|
||||||
RunDespiteErrors: true,
|
RunDespiteErrors: true,
|
||||||
Run: run,
|
Run: run,
|
||||||
|
|
|
@ -1,3 +1,7 @@
|
||||||
|
// Copyright 2018 The Go Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
package unreachable_test
|
package unreachable_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
|
|
@ -2,6 +2,8 @@
|
||||||
// Use of this source code is governed by a BSD-style
|
// Use of this source code is governed by a BSD-style
|
||||||
// license that can be found in the LICENSE file.
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
// Package unsafeptr defines an Analyzer that checks for invalid
|
||||||
|
// conversions of uintptr to unsafe.Pointer.
|
||||||
package unsafeptr
|
package unsafeptr
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -14,9 +16,17 @@ import (
|
||||||
"golang.org/x/tools/go/ast/inspector"
|
"golang.org/x/tools/go/ast/inspector"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const Doc = `check for invalid conversions of uintptr to unsafe.Pointer
|
||||||
|
|
||||||
|
The unsafeptr analyzer reports likely incorrect uses of unsafe.Pointer
|
||||||
|
to convert integers to pointers. A conversion from uintptr to
|
||||||
|
unsafe.Pointer is invalid if it implies that there is a uintptr-typed
|
||||||
|
word in memory that holds a pointer value, because that word will be
|
||||||
|
invisible to stack copying and to the garbage collector.`
|
||||||
|
|
||||||
var Analyzer = &analysis.Analyzer{
|
var Analyzer = &analysis.Analyzer{
|
||||||
Name: "unsafeptr",
|
Name: "unsafeptr",
|
||||||
Doc: "check for invalid conversions of uintptr to unsafe.Pointer",
|
Doc: Doc,
|
||||||
Requires: []*analysis.Analyzer{inspect.Analyzer},
|
Requires: []*analysis.Analyzer{inspect.Analyzer},
|
||||||
Run: run,
|
Run: run,
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +1,7 @@
|
||||||
|
// Copyright 2018 The Go Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
package unsafeptr_test
|
package unsafeptr_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -7,7 +11,7 @@ import (
|
||||||
"golang.org/x/tools/go/analysis/passes/unsafeptr"
|
"golang.org/x/tools/go/analysis/passes/unsafeptr"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestFromFileSystem(t *testing.T) {
|
func Test(t *testing.T) {
|
||||||
testdata := analysistest.TestData()
|
testdata := analysistest.TestData()
|
||||||
analysistest.Run(t, testdata, unsafeptr.Analyzer, "a")
|
analysistest.Run(t, testdata, unsafeptr.Analyzer, "a")
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
// Use of this source code is governed by a BSD-style
|
// Use of this source code is governed by a BSD-style
|
||||||
// license that can be found in the LICENSE file.
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
// Package unusedresult defines an analyer that checks for unused
|
// Package unusedresult defines an analyzer that checks for unused
|
||||||
// results of calls to certain pure functions.
|
// results of calls to certain pure functions.
|
||||||
package unusedresult
|
package unusedresult
|
||||||
|
|
||||||
|
@ -23,15 +23,17 @@ import (
|
||||||
// fact for each function that tail-calls one of the functions that we
|
// fact for each function that tail-calls one of the functions that we
|
||||||
// check, and check those functions too.
|
// check, and check those functions too.
|
||||||
|
|
||||||
var Analyzer = &analysis.Analyzer{
|
const Doc = `check for unused results of calls to some functions
|
||||||
Name: "unusedresult",
|
|
||||||
Doc: `check for unused results of calls to some functions
|
|
||||||
|
|
||||||
Some functions like fmt.Errorf return a result and have no side effects,
|
Some functions like fmt.Errorf return a result and have no side effects,
|
||||||
so it is always a mistake to discard the result. This analyzer reports
|
so it is always a mistake to discard the result. This analyzer reports
|
||||||
calls to certain functions in which the result of the call is ignored.
|
calls to certain functions in which the result of the call is ignored.
|
||||||
|
|
||||||
The set of functions may be controlled using flags.`,
|
The set of functions may be controlled using flags.`
|
||||||
|
|
||||||
|
var Analyzer = &analysis.Analyzer{
|
||||||
|
Name: "unusedresult",
|
||||||
|
Doc: Doc,
|
||||||
Requires: []*analysis.Analyzer{inspect.Analyzer},
|
Requires: []*analysis.Analyzer{inspect.Analyzer},
|
||||||
Run: run,
|
Run: run,
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +1,7 @@
|
||||||
|
// Copyright 2018 The Go Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
package unusedresult_test
|
package unusedresult_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
|
Loading…
Reference in New Issue