Skip to content

Commit

Permalink
Fix: ignore errs returned outside block (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
jjti committed Nov 30, 2024
1 parent efe1f72 commit e234cf6
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@
# Dependency directories (remove the comment below to include it)
# vendor/
src/

.vscode
7 changes: 0 additions & 7 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ linters:
- errcheck
- errname
- errorlint
- exhaustive # checks exhaustiveness of enum switch statements
- exportloopref # checks for pointers to enclosing loop variables
- gci
- gochecknoinits # checks that no init functions are present in Go code
Expand Down Expand Up @@ -59,12 +58,6 @@ linters-settings:
- standard # Standard section: captures all standard packages.
- default # Default section: contains all imports that could not be matched to another section type.
- prefix(github.com/jjti)
exhaustive:
# Program elements to check for exhaustiveness.
# Default: [ switch ]
check:
- switch
- map
gocritic:
settings:
captLocal:
Expand Down
20 changes: 20 additions & 0 deletions spancheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,11 @@ outer:
}
seen[b] = true

// Skip successors that are not nested within this current block.
if _, ok := nestedBlockTypes[b.Kind]; !ok {
continue
}

// Prune the search if the block uses v.
if blockUses(pass, b) {
continue
Expand All @@ -330,6 +335,21 @@ outer:
return search(defBlock.Succs)
}

var nestedBlockTypes = map[cfg.BlockKind]struct{}{
cfg.KindBody: {},
cfg.KindForBody: {},
cfg.KindForLoop: {},
cfg.KindIfElse: {},
cfg.KindIfThen: {},
cfg.KindLabel: {},
cfg.KindRangeBody: {},
cfg.KindRangeLoop: {},
cfg.KindSelectCaseBody: {},
cfg.KindSelectAfterCase: {},
cfg.KindSwitchCaseBody: {},
cfg.KindSwitchNextCase: {},
}

// usesCall reports whether stmts contain a use of the selName call on variable v.
func usesCall(
pass *analysis.Pass,
Expand Down
36 changes: 36 additions & 0 deletions testdata/enableall/enable_all.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,3 +213,39 @@ func testStartTrace() *trace.Span { // no error expected because this is in extr
_, span := trace.StartSpan(context.Background(), "bar")
return span
}

// https://github.com/jjti/go-spancheck/issues/25
func _() error {
if true {
_, span := otel.Tracer("foo").Start(context.Background(), "bar")
span.End()
}

return errors.New("test")
}

func _() error {
if true {
_, span := otel.Tracer("foo").Start(context.Background(), "bar") // want "span.SetStatus is not called on all paths"
defer span.End()

if true {
span.RecordError(errors.New("test"))
return errors.New("test") // want "return can be reached without calling span.SetStatus"
}
}

return errors.New("test")
}

// TODO: the check below now fails after the change to fix issue 25 above.
// func _() error {
// var span *trace.Span

// if true {
// _, span = trace.StartSpan(context.Background(), "foo")
// defer span.End()
// }

// return errors.New("test")
// }

0 comments on commit e234cf6

Please sign in to comment.