Skip to content

Commit

Permalink
commit latest changes and a gitignore
Browse files Browse the repository at this point in the history
  • Loading branch information
matloob committed Aug 22, 2019
1 parent f17f628 commit 959307e
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 28 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.DS_Store
38 changes: 11 additions & 27 deletions cmpequal/cmpequal.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,11 @@ func run(pass *analysis.Pass) (interface{}, error) {
if !types.Identical(typ0, typ1) {
var fixes []analysis.SuggestedFix
if isPointerTo(typ0, typ1) {
pass.Reportf(call.Pos(), "arg0 is a pointer to arg1")
fixes = append(fixes, fixDereference(pass, call.Args[0]))
fixes = fixDereference(pass, call.Args[0])
} else if isPointerTo(typ1, typ0) {
pass.Reportf(call.Pos(), "arg1 is a pointer to arg0")
fixes = append(fixes, fixDereference(pass, call.Args[1]))
} else {
pass.Reportf(call.Pos(), "isPointerTo(typ0, arg1): %v, isPointerTo(arg1, arg0): %v", isPointerTo(typ0, typ1), isPointerTo(typ1, typ0))
pass.Reportf(call.Pos(), "isPointer(typ0): %v, isPointer(typ1): %v", isPointer(typ0), isPointer(typ1))
if t, ok := typ1.(*types.Pointer); ok {
pass.Reportf(call.Pos(), ".elem: %s", t.Elem())
}
fixes = fixDereference(pass, call.Args[1])
}
pass.Report(analysis.Diagnostic{
Pos: call.Pos(), End: call.End(),
Message: fmt.Sprintf("cmp.Equal's arguments must have the same type; "+
"is called with %v and %v", typ0, typ1),
SuggestedFixes: fixes,
})
reportWithFixes(pass, call, fixes, "cmp.Equal's arguments must have the same type; is called with %v and %v", typ0, typ1)
}
}
inspect.Preorder(
Expand All @@ -64,27 +51,24 @@ func run(pass *analysis.Pass) (interface{}, error) {
return nil, nil
}

func reportWithFixes(pass *analysis.Pass, node ast.Node, fixes []analysis.SuggestedFix, format string, formatArgs ...interface{}) {
pass.Report(analysis.Diagnostic{Pos: node.Pos(), End: node.End(), Message: fmt.Sprintf(format, formatArgs...), SuggestedFixes: fixes})
}

func isPointerTo(a, b types.Type) bool {
if ptr, ok := a.(*types.Pointer); ok {
return types.Identical(ptr.Elem(), b)
}
return false
}

func isPointer(a types.Type) bool {
_, ok := a.(*types.Pointer)
return ok
}

func fixDereference(pass *analysis.Pass, expr ast.Expr) analysis.SuggestedFix {
func fixDereference(pass *analysis.Pass, expr ast.Expr) []analysis.SuggestedFix {
// dereference typ0
var buf bytes.Buffer
if err := format.Node(&buf, pass.Fset, &ast.StarExpr{X: expr}); err != nil {
buf.WriteString(err.Error())
}
format.Node(&buf, pass.Fset, &ast.StarExpr{X: expr})
fix := analysis.SuggestedFix{
Message: "derefenence pointer",
Message: "dereference pointer",
TextEdits: []analysis.TextEdit{{expr.Pos(), expr.End(), buf.Bytes()}},
}
return fix
return []analysis.SuggestedFix{fix}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ go 1.12

require (
github.com/google/go-cmp v0.2.0
golang.org/x/tools v0.0.0-20190424031103-cb2dda6eabdf
golang.org/x/tools v0.0.0-20190816183240-caa95bb40b63
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ github.com/google/go-cmp v0.2.0 h1:+dTQ8DZQJz0Mb/HjFlkptS1FeQ4cWSnN941F8aEG4SQ=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20190424031103-cb2dda6eabdf h1:Yv3pKbXQqpdhrt53r+Yr1XveoqVgIFTCQdaamSalWwM=
golang.org/x/tools v0.0.0-20190424031103-cb2dda6eabdf/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
golang.org/x/tools v0.0.0-20190816183240-caa95bb40b63 h1:w//3yKt/hJYZ8laT2BB5r7uIgxLE859kTdrn1J6lp5w=
golang.org/x/tools v0.0.0-20190816183240-caa95bb40b63/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Binary file modified presentations/london.key
Binary file not shown.

0 comments on commit 959307e

Please sign in to comment.