Skip to content

Commit d6538bb

Browse files
committed
add other unsupported case
1 parent 613657f commit d6538bb

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

passes/chbatchclose/testdata/src/testcases/testcases.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,28 @@ func invalidDeferAbortInClosure() {
245245
defer func() { _ = batch.Abort() }()
246246
}
247247

248+
// known limitation / false negative: a deferred closure shadows the outer `batch` name
249+
// with a non-Batch local that is .Close()'d. The outer Batch is never closed, but the
250+
// linter currently credits the inner .Close() to the outer name because tracking is
251+
// name-based and does not consult type info inside the deferred closure body.
252+
// In practice this is a contrived pattern that any IDE / shadow linter would flag.
253+
func deferCloseInClosureShadowed() {
254+
batch, err := conn.PrepareBatch(ctx, "INSERT INTO t")
255+
if err != nil {
256+
return
257+
}
258+
// no defer batch.Close() on the outer Batch — should ideally be reported.
259+
defer func() {
260+
batch := fakeCloser{} // shadows outer `batch`
261+
_ = batch.Close()
262+
}()
263+
_ = batch.Send()
264+
}
265+
266+
type fakeCloser struct{}
267+
268+
func (fakeCloser) Close() error { return nil }
269+
248270
// the code below is in theory correct as all error cases are handled and result in a batch.Abort().
249271
// we still mark this as an error as it's not defensive. A defer batch.Close() would not change the code correctness and is easy to add.
250272
func invalidNotDefensive() error {

0 commit comments

Comments
 (0)