cmd/vet: switch to go/types from std repo
Depends on https://golang.org/cl/8767/. With this change, cmd/vet does not depend on x/tools anymore and could be moved into the std repo if so desired. Change-Id: Ia205c6e1a6a63eebb27776064e5c24491043b683 Reviewed-on: https://go-review.googlesource.com/8791 Reviewed-by: Rob Pike <r@golang.org>
This commit is contained in:
parent
069d2f3bcb
commit
5b72e8e9bb
|
@ -11,8 +11,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"go/ast"
|
"go/ast"
|
||||||
"go/token"
|
"go/token"
|
||||||
|
"go/types"
|
||||||
"golang.org/x/tools/go/types"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
|
|
@ -15,14 +15,12 @@ import (
|
||||||
"go/parser"
|
"go/parser"
|
||||||
"go/printer"
|
"go/printer"
|
||||||
"go/token"
|
"go/token"
|
||||||
|
"go/types"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
_ "golang.org/x/tools/go/gcimporter"
|
|
||||||
"golang.org/x/tools/go/types"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// TODO: Need a flag to set build tags when parsing the package.
|
// TODO: Need a flag to set build tags when parsing the package.
|
||||||
|
|
|
@ -12,8 +12,7 @@ package main
|
||||||
import (
|
import (
|
||||||
"go/ast"
|
"go/ast"
|
||||||
"go/token"
|
"go/token"
|
||||||
|
"go/types"
|
||||||
"golang.org/x/tools/go/types"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
|
|
@ -10,13 +10,12 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"flag"
|
"flag"
|
||||||
"go/ast"
|
"go/ast"
|
||||||
|
"go/exact"
|
||||||
"go/token"
|
"go/token"
|
||||||
|
"go/types"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"unicode/utf8"
|
"unicode/utf8"
|
||||||
|
|
||||||
"golang.org/x/tools/go/exact"
|
|
||||||
"golang.org/x/tools/go/types"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var printfuncs = flag.String("printfuncs", "", "comma-separated list of print function names to check")
|
var printfuncs = flag.String("printfuncs", "", "comma-separated list of print function names to check")
|
||||||
|
|
|
@ -34,8 +34,7 @@ import (
|
||||||
"flag"
|
"flag"
|
||||||
"go/ast"
|
"go/ast"
|
||||||
"go/token"
|
"go/token"
|
||||||
|
"go/types"
|
||||||
"golang.org/x/tools/go/types"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var strictShadowing = flag.Bool("shadowstrict", false, "whether to be strict about shadowing; can be noisy")
|
var strictShadowing = flag.Bool("shadowstrict", false, "whether to be strict about shadowing; can be noisy")
|
||||||
|
|
|
@ -10,10 +10,9 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"go/ast"
|
"go/ast"
|
||||||
|
"go/exact"
|
||||||
"go/token"
|
"go/token"
|
||||||
|
"go/types"
|
||||||
"golang.org/x/tools/go/exact"
|
|
||||||
"golang.org/x/tools/go/types"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
|
|
@ -8,14 +8,14 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"go/ast"
|
"go/ast"
|
||||||
|
"go/importer"
|
||||||
"go/token"
|
"go/token"
|
||||||
|
"go/types"
|
||||||
"golang.org/x/tools/go/types"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// imports is the canonical map of imported packages we need for typechecking.
|
// The stdImporter implements importing from package files created by
|
||||||
// It is created during initialization.
|
// the compiler that built this binary.
|
||||||
var imports = make(map[string]*types.Package)
|
var stdImporter = importer.Default()
|
||||||
|
|
||||||
var (
|
var (
|
||||||
stringerMethodType = types.New("func() string")
|
stringerMethodType = types.New("func() string")
|
||||||
|
@ -35,7 +35,7 @@ func init() {
|
||||||
// path.name, and adds the respective package to the imports map
|
// path.name, and adds the respective package to the imports map
|
||||||
// as a side effect.
|
// as a side effect.
|
||||||
func importType(path, name string) types.Type {
|
func importType(path, name string) types.Type {
|
||||||
pkg, err := types.DefaultImport(imports, path)
|
pkg, err := stdImporter.Import(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// This can happen if fmt hasn't been compiled yet.
|
// This can happen if fmt hasn't been compiled yet.
|
||||||
// Since nothing uses formatterType anyway, don't complain.
|
// Since nothing uses formatterType anyway, don't complain.
|
||||||
|
@ -56,12 +56,12 @@ func (pkg *Package) check(fs *token.FileSet, astFiles []*ast.File) error {
|
||||||
pkg.spans = make(map[types.Object]Span)
|
pkg.spans = make(map[types.Object]Span)
|
||||||
pkg.types = make(map[ast.Expr]types.TypeAndValue)
|
pkg.types = make(map[ast.Expr]types.TypeAndValue)
|
||||||
config := types.Config{
|
config := types.Config{
|
||||||
// We provide the same packages map for all imports to ensure
|
|
||||||
// that everybody sees identical packages for the given paths.
|
|
||||||
Packages: imports,
|
|
||||||
// By providing a Config with our own error function, it will continue
|
// By providing a Config with our own error function, it will continue
|
||||||
// past the first error. There is no need for that function to do anything.
|
// past the first error. There is no need for that function to do anything.
|
||||||
Error: func(error) {},
|
Error: func(error) {},
|
||||||
|
// We use the same importer for all imports to ensure
|
||||||
|
// that everybody sees identical packages for the given paths.
|
||||||
|
Importer: stdImporter,
|
||||||
}
|
}
|
||||||
info := &types.Info{
|
info := &types.Info{
|
||||||
Selections: pkg.selectors,
|
Selections: pkg.selectors,
|
||||||
|
|
|
@ -9,8 +9,7 @@ package main
|
||||||
import (
|
import (
|
||||||
"go/ast"
|
"go/ast"
|
||||||
"go/token"
|
"go/token"
|
||||||
|
"go/types"
|
||||||
"golang.org/x/tools/go/types"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
|
|
@ -11,10 +11,8 @@ import (
|
||||||
"flag"
|
"flag"
|
||||||
"go/ast"
|
"go/ast"
|
||||||
"go/token"
|
"go/token"
|
||||||
|
"go/types"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"golang.org/x/tools/go/ast/astutil"
|
|
||||||
"golang.org/x/tools/go/types"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var unusedFuncsFlag = flag.String("unusedfuncs",
|
var unusedFuncsFlag = flag.String("unusedfuncs",
|
||||||
|
@ -56,11 +54,11 @@ func initUnusedFlags() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkUnusedResult(f *File, n ast.Node) {
|
func checkUnusedResult(f *File, n ast.Node) {
|
||||||
call, ok := astutil.Unparen(n.(*ast.ExprStmt).X).(*ast.CallExpr)
|
call, ok := unparen(n.(*ast.ExprStmt).X).(*ast.CallExpr)
|
||||||
if !ok {
|
if !ok {
|
||||||
return // not a call statement
|
return // not a call statement
|
||||||
}
|
}
|
||||||
fun := astutil.Unparen(call.Fun)
|
fun := unparen(call.Fun)
|
||||||
|
|
||||||
if f.pkg.types[fun].IsType() {
|
if f.pkg.types[fun].IsType() {
|
||||||
return // a conversion, not a call
|
return // a conversion, not a call
|
||||||
|
|
Loading…
Reference in New Issue