File tree Expand file tree Collapse file tree
passes/chbatchclose/testdata/src/testcases Expand file tree Collapse file tree Original file line number Diff line number Diff 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.
250272func invalidNotDefensive () error {
You can’t perform that action at this time.
0 commit comments