1
0
mirror of https://github.com/golang/go synced 2024-11-18 16:14:46 -07:00

go/analysis: handle common nil pointers

Updates golang/go#33727, golang/go#33689

Change-Id: Ie32ac4efc9fe0d7b08fcff3feb44b28d83df942d
Reviewed-on: https://go-review.googlesource.com/c/tools/+/190908
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Michael Matloob <matloob@golang.org>
This commit is contained in:
Rebecca Stambler 2019-08-20 16:36:06 -04:00
parent c4a336ef6a
commit c2567a2209
4 changed files with 12 additions and 7 deletions

View File

@ -102,10 +102,11 @@ func run(pass *analysis.Pass) (interface{}, error) {
inspect.Preorder(nodeFilter, func(n ast.Node) {
switch n := n.(type) {
case *ast.FuncDecl:
fn := pass.TypesInfo.Defs[n.Name].(*types.Func)
funcDecls[fn] = &declInfo{decl: n}
decls = append(decls, fn)
// Type information may be incomplete.
if fn, ok := pass.TypesInfo.Defs[n.Name].(*types.Func); ok {
funcDecls[fn] = &declInfo{decl: n}
decls = append(decls, fn)
}
case *ast.FuncLit:
funcLits[n] = new(litInfo)
lits = append(lits, n)

View File

@ -40,7 +40,11 @@ func run(pass *analysis.Pass) (interface{}, error) {
(*ast.StructType)(nil),
}
inspect.Preorder(nodeFilter, func(n ast.Node) {
styp := pass.TypesInfo.Types[n.(*ast.StructType)].Type.(*types.Struct)
styp, ok := pass.TypesInfo.Types[n.(*ast.StructType)].Type.(*types.Struct)
// Type information may be incomplete.
if !ok {
return
}
var seen namesSeen
for i := 0; i < styp.NumFields(); i++ {
field := styp.Field(i)

View File

@ -177,7 +177,7 @@ func (pkg *pkg) GetTypesSizes() types.Sizes {
}
func (pkg *pkg) IsIllTyped() bool {
return pkg.types == nil && pkg.typesInfo == nil
return pkg.types == nil || pkg.typesInfo == nil || pkg.typesSizes == nil
}
func (pkg *pkg) SetDiagnostics(diags []source.Diagnostic) {

View File

@ -168,7 +168,7 @@ func (act *Action) execOnce(ctx context.Context, fset *token.FileSet) error {
}
act.pass = pass
if act.Pkg.IsIllTyped() && !pass.Analyzer.RunDespiteErrors {
if act.Pkg.IsIllTyped() {
act.err = errors.Errorf("analysis skipped due to errors in package: %v", act.Pkg.GetErrors())
} else {
act.result, act.err = pass.Analyzer.Run(pass)