You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+12-3Lines changed: 12 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -185,10 +185,19 @@ If a clickhouse driver `Batch` is instantiated and is not part of the values ret
185
185
Also, assigning a `Batch` to the blank identifier `_` is flagged.
186
186
Variable re-assignments and intertwined variable are supported. See [testcases.go](passes/chbatchclose/testdata/src/testcases/testcases.go).
187
187
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
+
188
192
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.
192
201
-`defer batch.Close()` must be called after checking that the `PrepareBatch` call returned no error.
0 commit comments