Skip to content

Commit

Permalink
Fix case where ignored func is in same package
Browse files Browse the repository at this point in the history
  • Loading branch information
jjti committed Dec 30, 2023
1 parent 3c3cc21 commit 6f797fe
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 15 deletions.
11 changes: 1 addition & 10 deletions go.work.sum
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
golang.org/x/sync v0.5.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
14 changes: 11 additions & 3 deletions spancheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func runFunc(pass *analysis.Pass, node ast.Node, config *Config) {
return // missing type information
}

// Check for missing Ends().
// Check for missing calls.
for _, sv := range spanVars {
if !config.DisableEndCheck || config.EnableAll {
// Check if there's no End to the span.
Expand Down Expand Up @@ -246,11 +246,19 @@ func missingSpanCalls(
for _, subStmt := range stmts {
stack := []ast.Node{}
ast.Inspect(subStmt, func(n ast.Node) bool {
switch n.(type) {
switch n := n.(type) {
case *ast.FuncLit:
if len(stack) > 0 {
return false // don't stray into nested functions
}
case *ast.CallExpr:
if ident, ok := n.Fun.(*ast.Ident); ok {
fnSig := pass.TypesInfo.ObjectOf(ident).String()
if ignoreCheckSig != nil && ignoreCheckSig.MatchString(fnSig) {
found = true
return false
}
}
case nil:
stack = stack[:len(stack)-1] // pop
return true
Expand Down Expand Up @@ -319,7 +327,7 @@ outer:
return nil
}

// Does the defining block return without using v.End()?
// Does the defining block return without making the call?
if ret := defBlock.Return(); ret != nil {
return checkErr(pass, ret)
}
Expand Down
4 changes: 2 additions & 2 deletions spancheck_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ func Test(t *testing.T) {
"base": spancheck.NewConfigFromFlags(),
"disableerrorchecks": {
EnableSetStatusCheck: true,
IgnoreSetStatusCheckSignaturesSlice: []string{"telemetry.Record"},
IgnoreSetStatusCheckSignaturesSlice: []string{"telemetry.Record", "recordErr"},
EnableRecordErrorCheck: true,
IgnoreRecordErrorCheckSignaturesSlice: []string{"telemetry.Record"},
IgnoreRecordErrorCheckSignaturesSlice: []string{"telemetry.Record", "recordErr"},
},
"enableall": {
EnableAll: true,
Expand Down
21 changes: 21 additions & 0 deletions testdata/disableerrorchecks/disable_error_checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/jjti/go-spancheck/testdata/disableerrorchecks/telemetry"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/trace"
)

// incorrect
Expand Down Expand Up @@ -35,3 +36,23 @@ func _() error {

return nil
}

func _() error {
_, span := otel.Tracer("foo").Start(context.Background(), "bar")
defer span.End()

err := errors.New("foo")
err = telemetry.Record(span, err)
return err
}

func _() error {
_, span := otel.Tracer("foo").Start(context.Background(), "bar")
defer span.End()

err := errors.New("foo")
recordErr(span, err)
return err
}

func recordErr(span trace.Span, err error) {}

0 comments on commit 6f797fe

Please sign in to comment.