Skip to content

Handle deferred closure dispatch to avoid false positives#26

Draft
Codex wants to merge 2 commits intomainfrom
codex/fix-false-positive-defer
Draft

Handle deferred closure dispatch to avoid false positives#26
Codex wants to merge 2 commits intomainfrom
codex/fix-false-positive-defer

Conversation

@Codex
Copy link

@Codex Codex AI commented Feb 5, 2026

Deferred dispatch inside an Enabled guard was flagged as undispatched even though the closure called Msg on the captured event.

  • Problem: a defer func() { evt.Msg(...) }() guarded by if evt := log.Debug(); evt.Enabled() incorrectly triggered “must be dispatched” diagnostics.
  • Analyzer: track MakeClosure free vars and bindings, deleting captured zerolog events when dispatch occurs inside the closure; also unwrap unary ops when resolving event roots.
  • Tests: add regression covering the guarded-defer pattern to ensure the analyzer accepts it.

Example:

if evt := log.Debug(); evt.Enabled() {
	defer func() {
		evt.Msg("server stopped")
	}()
}
Original prompt

This section details on the original issue you should resolve

<issue_title>False positive with defer</issue_title>
<issue_description>```go
package chunkstore

import "github.com/rs/zerolog/log"

func main() {
if evt := log.Debug(); evt.Enabled() {
defer func() {
evt.Msg("Server stopped")
}()
}
}


```shell
$ golangci-lint run -Ezerologlint
main.go:6:21: must be dispatched by Msg or Send method (zerologlint)
	if evt := log.Debug(); evt.Enabled() {
	                   ^
```</issue_description>

## Comments on the Issue (you are @codex[agent] in this section)

<comments>
</comments>

@Codex Codex AI changed the title [WIP] Fix false positive with defer in logging Handle deferred closure dispatch to avoid false positives Feb 5, 2026
@Codex Codex AI requested a review from ykadowak February 5, 2026 14:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

False positive with defer

2 participants