Skip to content

Commit e234cf6

Browse files
committed
Fix: ignore errs returned outside block (#25)
1 parent efe1f72 commit e234cf6

File tree

4 files changed

+58
-7
lines changed

4 files changed

+58
-7
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,5 @@
1717
# Dependency directories (remove the comment below to include it)
1818
# vendor/
1919
src/
20+
21+
.vscode

.golangci.yml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ linters:
1717
- errcheck
1818
- errname
1919
- errorlint
20-
- exhaustive # checks exhaustiveness of enum switch statements
2120
- exportloopref # checks for pointers to enclosing loop variables
2221
- gci
2322
- gochecknoinits # checks that no init functions are present in Go code
@@ -59,12 +58,6 @@ linters-settings:
5958
- standard # Standard section: captures all standard packages.
6059
- default # Default section: contains all imports that could not be matched to another section type.
6160
- prefix(github.com/jjti)
62-
exhaustive:
63-
# Program elements to check for exhaustiveness.
64-
# Default: [ switch ]
65-
check:
66-
- switch
67-
- map
6861
gocritic:
6962
settings:
7063
captLocal:

spancheck.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,11 @@ outer:
309309
}
310310
seen[b] = true
311311

312+
// Skip successors that are not nested within this current block.
313+
if _, ok := nestedBlockTypes[b.Kind]; !ok {
314+
continue
315+
}
316+
312317
// Prune the search if the block uses v.
313318
if blockUses(pass, b) {
314319
continue
@@ -330,6 +335,21 @@ outer:
330335
return search(defBlock.Succs)
331336
}
332337

338+
var nestedBlockTypes = map[cfg.BlockKind]struct{}{
339+
cfg.KindBody: {},
340+
cfg.KindForBody: {},
341+
cfg.KindForLoop: {},
342+
cfg.KindIfElse: {},
343+
cfg.KindIfThen: {},
344+
cfg.KindLabel: {},
345+
cfg.KindRangeBody: {},
346+
cfg.KindRangeLoop: {},
347+
cfg.KindSelectCaseBody: {},
348+
cfg.KindSelectAfterCase: {},
349+
cfg.KindSwitchCaseBody: {},
350+
cfg.KindSwitchNextCase: {},
351+
}
352+
333353
// usesCall reports whether stmts contain a use of the selName call on variable v.
334354
func usesCall(
335355
pass *analysis.Pass,

testdata/enableall/enable_all.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,3 +213,39 @@ func testStartTrace() *trace.Span { // no error expected because this is in extr
213213
_, span := trace.StartSpan(context.Background(), "bar")
214214
return span
215215
}
216+
217+
// https://github.com/jjti/go-spancheck/issues/25
218+
func _() error {
219+
if true {
220+
_, span := otel.Tracer("foo").Start(context.Background(), "bar")
221+
span.End()
222+
}
223+
224+
return errors.New("test")
225+
}
226+
227+
func _() error {
228+
if true {
229+
_, span := otel.Tracer("foo").Start(context.Background(), "bar") // want "span.SetStatus is not called on all paths"
230+
defer span.End()
231+
232+
if true {
233+
span.RecordError(errors.New("test"))
234+
return errors.New("test") // want "return can be reached without calling span.SetStatus"
235+
}
236+
}
237+
238+
return errors.New("test")
239+
}
240+
241+
// TODO: the check below now fails after the change to fix issue 25 above.
242+
// func _() error {
243+
// var span *trace.Span
244+
245+
// if true {
246+
// _, span = trace.StartSpan(context.Background(), "foo")
247+
// defer span.End()
248+
// }
249+
250+
// return errors.New("test")
251+
// }

0 commit comments

Comments
 (0)