Repository navigation
[linter-miner] Add unchecked-deferred-close linter #67481
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
pelikhan
merged 3 commits into
main
from
linter-miner/unchecked-deferred-close-d23cdf3b8bd27b8e
Oct 10, 2026
+199
−5
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
pkg/linters/unchecked-deferred-close/testdata/src/basic/bad.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| package basic | ||
|
|
||
| import ( | ||
| "io" | ||
| "os" | ||
| ) | ||
|
|
||
| type NamedCloser interface { | ||
| io.Closer | ||
| } | ||
|
|
||
| type EmbeddedCloser struct { | ||
| io.Closer | ||
| } | ||
|
|
||
| type GenericCloser interface { | ||
| Close() error | ||
| } | ||
|
|
||
| func bad(filename string) error { | ||
| f, err := os.Open(filename) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| defer f.Close() // want "defer close\\(\\) call ignores error return value" | ||
| // ... use f | ||
| return nil | ||
| } | ||
|
|
||
| func badIOCloser(c io.Closer) { | ||
| defer c.Close() // want "defer close\\(\\) call ignores error return value" | ||
| } | ||
|
|
||
| func badNamedInterface(c NamedCloser) { | ||
| defer c.Close() // want "defer close\\(\\) call ignores error return value" | ||
| } | ||
|
|
||
| func badEmbeddedCloser(c EmbeddedCloser) { | ||
| defer c.Close() // want "defer close\\(\\) call ignores error return value" | ||
| } | ||
|
|
||
| func badTypeParameter[T GenericCloser](c T) { | ||
| defer c.Close() // want "defer close\\(\\) call ignores error return value" | ||
| } |
30 changes: 30 additions & 0 deletions
30
pkg/linters/unchecked-deferred-close/testdata/src/basic/good.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| package basic | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| ) | ||
|
|
||
| func good(filename string) error { | ||
| f, err := os.Open(filename) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| defer func() { | ||
| if err := f.Close(); err != nil { | ||
| fmt.Fprintf(os.Stderr, "close error: %v\n", err) | ||
| } | ||
| }() | ||
| // ... use f | ||
| return nil | ||
| } | ||
|
|
||
| // NoReturnClose is a mock type that has Close() with no error return | ||
| type NoReturnClose struct{} | ||
|
|
||
| func (n *NoReturnClose) Close() {} | ||
|
|
||
| func goodNoError() { | ||
| n := &NoReturnClose{} | ||
| defer n.Close() // OK: Close() returns nothing | ||
| } |
94 changes: 94 additions & 0 deletions
94
pkg/linters/unchecked-deferred-close/unchecked_deferredclose.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| // Package unchecked deferredclose implements a Go analysis linter that flags | ||
| // defer close() calls without error checking, which can hide resource cleanup failures. | ||
| package unchecked_deferredclose | ||
|
|
||
| import ( | ||
| "go/ast" | ||
| "go/types" | ||
|
|
||
| "golang.org/x/tools/go/analysis" | ||
|
|
||
| "github.com/github/gh-aw/pkg/linters/internal/analyzerutil" | ||
| "github.com/github/gh-aw/pkg/linters/internal/filecheck" | ||
| "github.com/github/gh-aw/pkg/linters/internal/nolint" | ||
| ) | ||
|
|
||
| // Analyzer is the unchecked-deferred-close analysis pass. | ||
| var Analyzer = analyzerutil.New("unchecked_deferredclose", "reports defer close() calls without error checking that may hide resource cleanup failures", run) | ||
|
|
||
| var builtinErrorType = types.Universe.Lookup("error").Type() | ||
|
|
||
| func run(pass *analysis.Pass) (any, error) { | ||
| noLintIndex, generatedFiles, err := analyzerutil.Indexes(pass) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| nodeFilter := []ast.Node{(*ast.DeferStmt)(nil)} | ||
| return analyzerutil.Preorder(pass, nodeFilter, func(n ast.Node) { | ||
| analyzeDeferClose(pass, n, generatedFiles, noLintIndex) | ||
| }) | ||
| } | ||
|
|
||
| // analyzeDeferClose checks for defer close() calls that don't handle errors. | ||
| func analyzeDeferClose(pass *analysis.Pass, n ast.Node, generatedFiles filecheck.GeneratedIndex, noLintIndex nolint.DirectiveIndex) { | ||
| deferStmt, ok := n.(*ast.DeferStmt) | ||
| if !ok { | ||
| return | ||
| } | ||
|
|
||
| // The defer statement's Call is already a *CallExpr | ||
| callExpr := deferStmt.Call | ||
| if callExpr == nil { | ||
| return | ||
| } | ||
|
|
||
| // Check if the call is to a Close() method | ||
| selector, ok := callExpr.Fun.(*ast.SelectorExpr) | ||
| if !ok || selector.Sel.Name != "Close" { | ||
| return | ||
| } | ||
|
|
||
| // Ensure it has no arguments | ||
| if len(callExpr.Args) != 0 { | ||
| return | ||
| } | ||
|
|
||
| position := pass.Fset.PositionFor(deferStmt.Pos(), false) | ||
| if filecheck.ShouldSkipFilename(position.Filename, generatedFiles) { | ||
| return | ||
| } | ||
|
|
||
| if nolint.HasDirectiveForLinter(position, noLintIndex, "unchecked_deferredclose") { | ||
| return | ||
| } | ||
|
|
||
| if pass.TypesInfo == nil { | ||
| return | ||
| } | ||
|
|
||
| selection := pass.TypesInfo.Selections[selector] | ||
| if selection == nil || selection.Kind() != types.MethodVal { | ||
| return | ||
| } | ||
|
|
||
| sig, ok := selection.Obj().Type().(*types.Signature) | ||
| if !ok || !closeReturnsError(sig) { | ||
| return | ||
| } | ||
|
|
||
| // Report: defer close() ignores error return | ||
| pass.Reportf( | ||
| deferStmt.Pos(), | ||
| "defer close() call ignores error return value; consider handling errors explicitly", | ||
| ) | ||
| } | ||
|
|
||
| func closeReturnsError(sig *types.Signature) bool { | ||
| if sig.Results() == nil || sig.Results().Len() == 0 { | ||
| return false | ||
| } | ||
|
|
||
| lastResult := sig.Results().At(sig.Results().Len() - 1) | ||
| return lastResult.Type() == builtinErrorType | ||
| } |
17 changes: 17 additions & 0 deletions
17
pkg/linters/unchecked-deferred-close/unchecked_deferredclose_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| //go:build !integration | ||
|
|
||
| // Package unchecked_deferredclose_test provides tests for the unchecked_deferredclose analyzer. | ||
| package unchecked_deferredclose_test | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "golang.org/x/tools/go/analysis/analysistest" | ||
|
|
||
| unchecked_deferredclose "github.com/github/gh-aw/pkg/linters/unchecked-deferred-close" | ||
| ) | ||
|
|
||
| func TestUncheckedDeferredClose(t *testing.T) { | ||
| t.Parallel() | ||
| analysistest.Run(t, analysistest.TestData(), unchecked_deferredclose.Analyzer, "basic") | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated the analyzer inventory in
doc.go, both README lists,documentedAnalyzers(), and the doc-sync enforcement tracking. It is documented as not yet enforced pending remediation of existing findings. Committed in 31de3ea.