Skip to content

Commit 5756759

Browse files
committed
update README
1 parent d6538bb commit 5756759

2 files changed

Lines changed: 12 additions & 6 deletions

File tree

README.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,10 +185,19 @@ If a clickhouse driver `Batch` is instantiated and is not part of the values ret
185185
Also, assigning a `Batch` to the blank identifier `_` is flagged.
186186
Variable re-assignments and intertwined variable are supported. See [testcases.go](passes/chbatchclose/testdata/src/testcases/testcases.go).
187187

188+
The linter recognizes two forms of `defer`:
189+
- direct call: `defer batch.Close()`
190+
- immediately-invoked closure: `defer func() { ... batch.Close() ... }()` (useful for wrapping the `Close` error).
191+
188192
There are some limitations:
189-
- except for looking into defer blocks;, the linter does not cross function block boundaries. If a `Batch` variable is instantiated and `batch.Close()` is called in a
190-
closure inside the defer call, the linter will not be able to associate the `batch.Close()` to the variable.
191-
(note: in most cases such pattern is a bad idea). See `deferCloseIsInClosure` test case.
193+
- the linter does not cross function call boundaries. In particular:
194+
- a closure passed as an argument (e.g. `defer func(b driver.Batch) { b.Close() }(batch)`) is not recognized.
195+
- a helper function that closes the batch (e.g. `defer closeWithLog(batch)`) is not recognized.
196+
- inside a deferred closure, the linter does not descend into nested closures or goroutines.
197+
`defer func() { go func() { batch.Close() }() }()` is not recognized as a valid close.
198+
- inside a deferred closure, tracking is name-based and does not consult type information, so shadowing the
199+
outer batch name with another `Close`-able local can hide a missing close. See `deferCloseInClosureShadowed`
200+
test case. We expect IDE / shadow linter to flag this as a bad pattern.
192201
- `defer batch.Close()` must be called after checking that the `PrepareBatch` call returned no error.
193202
incorrect:
194203
```

passes/chbatchclose/chbatchclose.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,6 @@ func handleDefer(deferStmt *ast.DeferStmt, usages map[string]*batchUsage) {
172172
if fun.Type.Params != nil && len(fun.Type.Params.List) > 0 {
173173
return
174174
}
175-
if fun.Body == nil {
176-
return
177-
}
178175
handleDeferredClosure(fun.Body, usages)
179176
}
180177
}

0 commit comments

Comments
 (0)