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.
6
This file contains the code to check for useless function comparisons.
7
A useless comparison is one like f == nil as opposed to f() == nil.
16
"code.google.com/p/go.tools/go/types"
19
func (f *File) checkNilFuncComparison(e *ast.BinaryExpr) {
24
// Only want == or != comparisons.
25
if e.Op != token.EQL && e.Op != token.NEQ {
29
// Only want comparisons with a nil identifier on one side.
40
// Only want identifiers or selector expressions.
42
switch v := e2.(type) {
45
case *ast.SelectorExpr:
46
obj = f.pkg.uses[v.Sel]
51
// Only want functions.
52
if _, ok := obj.(*types.Func); !ok {
56
f.Badf(e.Pos(), "comparison of function %v %v nil is always %v", obj.Name(), e.Op, e.Op == token.NEQ)
59
// isNil reports whether the provided expression is the built-in nil
61
func (f *File) isNil(e ast.Expr) bool {
62
return f.pkg.types[e].Type == types.Typ[types.UntypedNil]