Skip to content

Commit

Permalink
checkers: proper pkg/obj check for flagName (go-critic#786)
Browse files Browse the repository at this point in the history
The main problem came from the flagName trying
to check method calls while it should only handle
function calls.

Fixes go-critic#784.

Signed-off-by: Iskander Sharipov <[email protected]>
  • Loading branch information
quasilyte authored Feb 1, 2019
1 parent d19dbf1 commit d82b576
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
12 changes: 7 additions & 5 deletions checkers/flagName_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package checkers
import (
"go/ast"
"go/constant"
"go/types"
"strings"

"github.com/go-lintpack/lintpack"
Expand Down Expand Up @@ -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
}

Expand Down
6 changes: 6 additions & 0 deletions checkers/testdata/flagName/negative_tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, "")
Expand Down

0 comments on commit d82b576

Please sign in to comment.