From d82b576a9e95fb0d76a8a2831b18e018ddd8c6dd Mon Sep 17 00:00:00 2001 From: "Iskander (Alex) Sharipov" Date: Fri, 1 Feb 2019 22:59:37 +0300 Subject: [PATCH] checkers: proper pkg/obj check for flagName (#786) The main problem came from the flagName trying to check method calls while it should only handle function calls. Fixes #784. Signed-off-by: Iskander Sharipov --- checkers/flagName_checker.go | 12 +++++++----- checkers/testdata/flagName/negative_tests.go | 6 ++++++ 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/checkers/flagName_checker.go b/checkers/flagName_checker.go index f8d8fc677..e40efb524 100644 --- a/checkers/flagName_checker.go +++ b/checkers/flagName_checker.go @@ -3,6 +3,7 @@ package checkers import ( "go/ast" "go/constant" + "go/types" "strings" "github.com/go-lintpack/lintpack" @@ -30,13 +31,14 @@ type flagNameChecker struct { func (c *flagNameChecker) VisitExpr(expr ast.Expr) { call := astcast.ToCallExpr(expr) - sym := astcast.ToIdent(astcast.ToSelectorExpr(call.Fun).Sel) - obj := c.ctx.TypesInfo.ObjectOf(sym) - if obj == nil { + calledExpr := astcast.ToSelectorExpr(call.Fun) + obj, ok := c.ctx.TypesInfo.ObjectOf(astcast.ToIdent(calledExpr.X)).(*types.PkgName) + if !ok { return } - pkg := obj.Pkg() - if !isStdlibPkg(pkg) || pkg.Name() != "flag" { + sym := calledExpr.Sel + pkg := obj.Imported() + if !isStdlibPkg(pkg) || obj.Name() != "flag" { return } diff --git a/checkers/testdata/flagName/negative_tests.go b/checkers/testdata/flagName/negative_tests.go index dd50f2082..3ff14587d 100644 --- a/checkers/testdata/flagName/negative_tests.go +++ b/checkers/testdata/flagName/negative_tests.go @@ -4,6 +4,12 @@ import ( "flag" ) +func methodCall() { + // See #784. + var getter flag.Getter + _ = getter.String() +} + func noWhitespace() { _ = flag.Bool("name", false, "") _ = flag.Duration("name", 0, "")