-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
133 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package sametype | ||
|
||
import ( | ||
"go/ast" | ||
"go/types" | ||
"golang.org/x/tools/go/analysis" | ||
"golang.org/x/tools/go/analysis/passes/inspect" | ||
"golang.org/x/tools/go/ast/inspector" | ||
"golang.org/x/tools/go/types/typeutil" | ||
) | ||
|
||
var Analyzer = &analysis.Analyzer { | ||
Name: "cmpequal", | ||
Doc: "Check arg types of cmp.Equal", | ||
Requires: | ||
[]*analysis.Analyzer{inspect.Analyzer}, | ||
FactTypes: []analysis.Fact{(*SameType)(nil)}, | ||
Run: run, | ||
|
||
} | ||
|
||
type SameType struct{} | ||
|
||
func (s *SameType) AFact() {} | ||
|
||
func run(pass *analysis.Pass) (interface{}, error) { | ||
inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) | ||
checkForFact := func(n ast.Node) { | ||
call := n.(*ast.CallExpr) | ||
fn, _ := typeutil.Callee(pass.TypesInfo, call).(*types.Func) | ||
if fn == nil { | ||
return // not a function call | ||
} | ||
|
||
var sameType SameType | ||
if !pass.ImportObjectFact(fn, &sameType) { | ||
return | ||
} | ||
|
||
typ0 := pass.TypesInfo.Types[call.Args[0]].Type | ||
typ1 := pass.TypesInfo.Types[call.Args[1]].Type | ||
if !types.Identical(typ0, typ1) { | ||
pass.Reportf(call.Pos(), | ||
"Calls to %v must have arguments of the same type; "+ | ||
"is called with %v and %v", | ||
fn.Name(), typ0, typ1) | ||
}} | ||
maybeAddFact := func(n ast.Node, push bool, stack []ast.Node) bool { | ||
if !push { | ||
return true | ||
} | ||
|
||
call := n.(*ast.CallExpr) | ||
fn, _ := typeutil.Callee(pass.TypesInfo, call).(*types.Func) | ||
if fn == nil { | ||
return false // not a function call | ||
} | ||
|
||
if fn.FullName() != "annotate.SameType" { | ||
return false // not an annotation | ||
} | ||
|
||
var enclosingFunc *ast.FuncDecl | ||
for _, node := range stack { | ||
if v, ok := node.(*ast.FuncDecl); ok { | ||
enclosingFunc = v | ||
break | ||
} | ||
} | ||
if enclosingFunc == nil { | ||
return false // we didn't find the enclosing call | ||
} else if len(enclosingFunc.Type.Params.List) != 2 { | ||
pass.Reportf(call.Pos(), "SameType annotation can only be added to funcs with two arguments") | ||
} | ||
|
||
obj := pass.TypesInfo.Defs[enclosingFunc.Name] | ||
pass.ExportObjectFact(obj, &SameType{}) | ||
return false | ||
} | ||
inspect.WithStack([]ast.Node{(*ast.CallExpr)(nil)}, maybeAddFact) | ||
inspect.Preorder( | ||
[]ast.Node{(*ast.CallExpr)(nil)}, | ||
checkForFact) | ||
return nil, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Copyright 2018 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package sametype_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/matloob/analysistalk/sametype" | ||
"golang.org/x/tools/go/analysis/analysistest" | ||
) | ||
|
||
func Test(t *testing.T) { | ||
testdata := analysistest.TestData() | ||
analysistest.Run(t, testdata, sametype.Analyzer, "a") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package a | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"mycmp" | ||
) | ||
|
||
func TestSomething(t *testing.T) { | ||
var x int = 0 | ||
var y int = 0 | ||
if !mycmp.Equal(x, &y) { // want "Calls to Equal must have arguments of the same type" | ||
fmt.Println("but they're not equal!") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package annotate | ||
|
||
func SameType() { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package mycmp | ||
|
||
import ( | ||
"annotate" | ||
"reflect" | ||
) | ||
|
||
func Equal(a, b interface{}) bool { | ||
annotate.SameType() | ||
return reflect.DeepEqual(a, b) | ||
} |