From 5355ef481da4808af784de23ff54c2c0f520db1a Mon Sep 17 00:00:00 2001 From: gram Date: Fri, 17 Nov 2023 10:57:18 +0100 Subject: [PATCH] fix possible nil panic, load packages faster --- contracts/analyzer.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/contracts/analyzer.go b/contracts/analyzer.go index d7c675f..d129766 100644 --- a/contracts/analyzer.go +++ b/contracts/analyzer.go @@ -70,6 +70,11 @@ func analyzeImports(facts Result, pass *analysis.Pass) { pkg, err := loadPackageInfo(importPath) if err != nil { pass.Reportf(nImport.Pos(), "load package info: %v", err) + continue + } + if pkg.TypesInfo == nil { + pass.Reportf(nImport.Pos(), "package loaded without NeedTypesInfo flag") + continue } exportFacts(facts, pkg.TypesInfo, pkg.Syntax) } @@ -77,11 +82,7 @@ func analyzeImports(facts Result, pass *analysis.Pass) { } func loadPackageInfo(pkgName string) (*packages.Package, error) { - loadMode := (packages.NeedName | - packages.NeedSyntax | - packages.NeedDeps | - packages.NeedTypes | - packages.NeedTypesInfo) + loadMode := packages.NeedName | packages.NeedTypes | packages.NeedTypesInfo cfg := &packages.Config{Mode: loadMode} pkgs, err := packages.Load(cfg, string(pkgName)) if err != nil {