Source file src/cmd/vendor/golang.org/x/tools/go/analysis/passes/nilfunc/nilfunc.go

     1  // Copyright 2013 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Package nilfunc defines an Analyzer that checks for useless
     6  // comparisons against nil.
     7  package nilfunc
     8  
     9  import (
    10  	_ "embed"
    11  	"go/ast"
    12  	"go/token"
    13  	"go/types"
    14  
    15  	"golang.org/x/tools/go/analysis"
    16  	"golang.org/x/tools/go/analysis/passes/inspect"
    17  	"golang.org/x/tools/go/analysis/passes/internal/analysisutil"
    18  	"golang.org/x/tools/go/ast/inspector"
    19  	"golang.org/x/tools/internal/typesinternal"
    20  )
    21  
    22  //go:embed doc.go
    23  var doc string
    24  
    25  var Analyzer = &analysis.Analyzer{
    26  	Name:     "nilfunc",
    27  	Doc:      analysisutil.MustExtractDoc(doc, "nilfunc"),
    28  	URL:      "https://pkg.go.dev/golang.org/x/tools/go/analysis/passes/nilfunc",
    29  	Requires: []*analysis.Analyzer{inspect.Analyzer},
    30  	Run:      run,
    31  }
    32  
    33  func run(pass *analysis.Pass) (any, error) {
    34  	inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
    35  
    36  	nodeFilter := []ast.Node{
    37  		(*ast.BinaryExpr)(nil),
    38  	}
    39  	inspect.Preorder(nodeFilter, func(n ast.Node) {
    40  		e := n.(*ast.BinaryExpr)
    41  
    42  		// Only want == or != comparisons.
    43  		if e.Op != token.EQL && e.Op != token.NEQ {
    44  			return
    45  		}
    46  
    47  		// Only want comparisons with a nil identifier on one side.
    48  		var e2 ast.Expr
    49  		switch {
    50  		case pass.TypesInfo.Types[e.X].IsNil():
    51  			e2 = e.Y
    52  		case pass.TypesInfo.Types[e.Y].IsNil():
    53  			e2 = e.X
    54  		default:
    55  			return
    56  		}
    57  
    58  		// Only want functions.
    59  		obj := pass.TypesInfo.Uses[typesinternal.UsedIdent(pass.TypesInfo, e2)]
    60  		if _, ok := obj.(*types.Func); !ok {
    61  			return
    62  		}
    63  
    64  		pass.ReportRangef(e, "comparison of function %v %v nil is always %v", obj.Name(), e.Op, e.Op == token.NEQ)
    65  	})
    66  	return nil, nil
    67  }
    68  

View as plain text